1 Replies - 2044 Views - Last Post: 13 December 2010 - 10:28 AM Rate Topic: -----

#1 nya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 10

Connect C# to MySQL DB

Posted 13 December 2010 - 10:16 AM

hi
please does someone knows the source of how to connect mysql server with c sharp 2010
because i m actually trying to connect my program with c sharp but it shows
me an error here is the code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection(" Data Source=GILO-PC\\NEWINSTANCE; Initial Catalog=school;Integrated Security=TRUE");
        SqlDataAdapter da = new SqlDataAdapter();

        BindingSource tblContactsBS = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            da.InsertCommand = new SqlCommand("Insert Into tblContacts Values(@Firstname,@Lastname)", cs);
            da.InsertCommand.Parameters.Add("@Firstname", SqlDbType.VarChar).Value = txtFirstname.Text;
            da.InsertCommand.Parameters.Add("@Lastname", SqlDbType.VarChar).Value = txtLastname.Text;

          cs.Open();
            da.InsertCommand.ExecuteNonQuery() ;
            cs.Close()
        }

    }
}


This post has been edited by macosxnerd101: 13 December 2010 - 10:17 AM
Reason for edit:: Added code tags and moved to C#.


Is This A Good Question/Topic? 0
  • +

Replies To: Connect C# to MySQL DB

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,220
  • Joined: 23-August 08

Re: Connect C# to MySQL DB

Posted 13 December 2010 - 10:28 AM

You can't use a Microsoft SQL Server connection string to connect to a MySQL server. Here are some example connection strings for MySQL.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1