If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > ODBC-connection to MySQL with C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-17-06, 08:08
EnricoW EnricoW is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
ODBC-connection to MySQL with C#

Hi,
i want creat a programm with the help of c# for manipulation a data mysql database.

My code shows in these way:
Code:
 private void insert_Click(object sender, EventArgs e)
        {
            OdbcConnection con = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;" +
                    "Database=knowledge;UID=C_Sharp;PWD=c_sharp;");
            con.Open();
            
            DataTable datatable = new DataTable();
            OdbcCommand com = new OdbcCommand();
            OdbcDataAdapter adapter = new OdbcDataAdapter();

            com.Connection = con;
            datatable.TableName = "product";
            com.CommandText = "SELECT * FROM " + datatable.TableName + ";";
            adapter.SelectCommand = com;
            adapter.Fill(datatable);
            com.CommandText = "INSERT INTO product(product) VALUES(\"" + Product.Text + "\");";
            adapter.InsertCommand = com;
            adapter.Update(datatable);
              
            /*Testausgabe in eine zweites Textfeld des Formulares*/
            text.Clear();
            text.AppendText(adapter.InsertCommand.CommandText);
My probelm is, the sql-string for insert data in the database will build correctly. but after the run of the program no data was insert into the table.

I hope someone can help me to find my mistake.
Reply With Quote
  #2 (permalink)  
Old 09-17-06, 10:46
EnricoW EnricoW is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
OK i have fond the solution of my problem.
Now i give you then solution:
Code:
private void insert_Click(object sender, EventArgs e)
        {
            OdbcConnection con = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;" +
                    "Database=knowledge;UID=C_Sharp;PWD=c_sharp;");
            String SqlCommand = "INSERT INTO product(product) VALUES( \""+ Product.Text + "\" );";
            OdbcCommand cmd = new OdbcCommand(SqlCommand, con);
            try
            {
                /*SQL-Command ausführen*/
                cmd.ExecuteNonQuery();

                /*Testausgabe in eine zweites Textfeld des Formulares*/
                text.Clear();
                text.AppendText(cmd.CommandText);
            }
            catch (Exception ex)
            {
                /*Ausgabe des Fehlers*/
                text.Clear();
                text.AppendText(ex.Message);
            }
        }
Reply With Quote
  #3 (permalink)  
Old 09-19-06, 03:33
EnricoW EnricoW is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
For all people how have the same Problem, i give you a superior solution for my described problem:

Code:
private void insert_Click(object sender, EventArgs e)
        {
            OdbcCommand cmd = new OdbcCommand();

            /*Festlegen der Parameter für die Verbindung zur Datenbank mit Hilfe von ODBC*/
            OdbcConnection con = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;" +
                        "Database=knowledge;UID=C_Sharp;PWD=c_sharp;");

            /*Datenbank öffnen*/            
            con.Open();
            text.AppendText("Datenbank \"" + con.Database + "\" ist offen\n");
            
            /*Erstellen einen SQL-Anweisung und Übergabe an ODBC-Treiber*/
            cmd.Connection = con;
            cmd.CommandText = "INSERT INTO product(product) VALUES(?);";
            cmd.Parameters.AddWithValue("@pr", Product.Text);  
            
            /*SQL-Command ausführen*/
             cmd.ExecuteNonQuery();
            /*Testausgabe in eine zweites Textfeld des Formulares*/
            text.Clear();
            text.AppendText(cmd.CommandText);
            /*Eingabefeld löschen*/
            Product.Clear();
            
            /*Verbindung zur Datenbank beenden*/
            con.Close();   
        }
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On