C#ODBC连接MYSQL

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Drawing; using System.Windows.Forms; //MySQL Connector/ODBC 5.1ドライバが必要です namespace myTestCPP { public class DBConnection { public DataSet getConnection() { //接続文字列 string MyConString = "Driver={MySQL ODBC 5.1 Driver};" + "Server=192.168.24.110;" + "Database=magicmirror;" + "User=root;" + "Password=@@@@;" + "Option=3"; //接続 OdbcConnection MyConnection = new OdbcConnection(MyConString); //接続を開く MyConnection.Open(); //SQL文 string sql = "insert into doll values( 'xx', 16, null)"; //命令 OdbcCommand cmd = new OdbcCommand(sql, MyConnection); try { //変更系の命令を実行 cmd.ExecuteNonQuery(); } catch(Exception e) { Console.WriteLine("record duplicate."); } finally { cmd.Dispose(); } //用read方法絜数据到textbox********************** string tmp1 = null; string tmp2 = null; string tmp3 = null; string query = "select * from doll "; OdbcCommand cmd2 = new OdbcCommand(query, MyConnection); OdbcDataReader reader = cmd2.ExecuteReader(); while (reader.Read()) { tmp1 = reader[0].ToString(); tmp2 = reader[1].ToString(); tmp3 = reader[2].ToString(); } this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3; //用datagridview控件>f示数据表************************** /* string MyConString = "Driver={MySQL ODBC 5.1 Driver};" + "Server=192.168.24.110;" + "Database=magicmirror;" + "User=root;" + "Password=@@@@;" + "Option=3"; OdbcConnection MyConnection = new OdbcConnection(MyConString); OdbcDataAdapter oda = new OdbcDataAdapter("select * from doll ", MyConnection); DataSet ds = new DataSet(); oda.Fill(ds, "doll"); this.dataGridView1.DataSource = ds.Tables["doll"]; */ MyConnection.Close(); } } }

你可能感兴趣的:(.NET,dataset,string,database,null,mysql,textbox)