Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    3

    Unanswered: 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.

  2. #2
    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);
                }
            }

  3. #3
    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();   
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •