C#連接MySQL的兩個(gè)簡(jiǎn)單代碼示例
實(shí)現(xiàn)代碼一、使用的是mysql自帶的驅(qū)動(dòng)安裝一下即可
這是一個(gè)簡(jiǎn)單的例子。
在這里有個(gè)問題:dataset如果沒設(shè)主鍵的話,可能會(huì)引起一些對(duì)數(shù)庫(kù)操作的問題,比如會(huì)造成updata出現(xiàn)錯(cuò)誤。
static void Main(string[] args) { string sqlstr = "select * from manavatar"; MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString); DBConn.Open(); //MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn); MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn); MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx(); MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn); DataSet myDataSet = new DataSet(); DTAdapter.Fill(myDataSet,"manavatar"); try { while (DBReader.Read()) { //Console.WriteLine("11"); Console.WriteLine("DBReader:{0},\t\t\tddddd:{1},\t\t {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3)); } Console.WriteLine("0000"); } catch (Exception e) { Console.WriteLine("讀入失??!"+e.ToString()); } finally { Console.WriteLine("DBReader關(guān)閉"); Console.WriteLine("DBConn關(guān)閉"); DBReader.Close(); //DBConn.Close(); } for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++) { Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]); } }
方法二、
貼一份示例代碼。非常適合于初學(xué)者使用。
C#訪問mysql
using System; using System.Collections.Generic; using System.Text; using MySql.Data.MySqlClient; using System.Data; using System.Data.Common; namespace SybaseUtilTest { class Program { // http://bugs.mysql.com/47422, 有興趣的朋友,可以看看這個(gè)bug是怎么回事 static void testDataAdapter() { try { MySqlClientFactory factory = MySqlClientFactory.Instance; DbConnection conn = factory.CreateConnection(); conn.ConnectionString = string.Format("server={0};user id={1}; password={2}; database={3}; port={4}; pooling=false", "localhost", "root", "passwd", "test", 3306); conn.Open(); DbDataAdapter da = factory.CreateDataAdapter(); da.SelectCommand = conn.CreateCommand(); da.SelectCommand.CommandText = "select * from t12345"; da.DeleteCommand = conn.CreateCommand(); da.DeleteCommand.CommandText = "delete from t12345 where id = @id"; DbParameter param = factory.CreateParameter(); param.ParameterName = "@id"; param.DbType = DbType.Int32; param.SourceColumn = "id"; param.SourceVersion = DataRowVersion.Current; da.DeleteCommand.Parameters.Add(param); da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None; DataTable dt = new DataTable("t12345"); da.Fill(dt); int index = 0; foreach ( DataRow o in dt.Rows ) { if (o["id"].Equals(4)) { Console.WriteLine(String.Format("index={0}, to delete id = 4, col2 = {1}" , index, o["col2"])); break; } index++; } dt.Rows[index].Delete(); da.Update(dt); dt.AcceptChanges(); da.Dispose(); conn.Close(); } catch (Exception ex) { Console.WriteLine(ex.Source + " " + ex.Message + " " + ex.StackTrace); } } static void Main(string[] args) { testDataAdapter(); } } }
以上就是腳本之家小編為大家整理的c#連接mysql數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考一下。
相關(guān)文章
C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
這篇文章主要給大家介紹了關(guān)于C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#使用Dictionary<string, string>拆分字符串與記錄log方法
這篇文章介紹了Dictionary<string, string>拆分字符串與記錄log的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04unity實(shí)現(xiàn)場(chǎng)景切換進(jìn)度條顯示
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景切換進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11C#中調(diào)用Windows API的技術(shù)要點(diǎn)說(shuō)明
本篇文章主要是對(duì)C#中調(diào)用Windows API的技術(shù)要點(diǎn)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#動(dòng)態(tài)創(chuàng)建button的方法
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button的方法,涉及C#按鈕屬性動(dòng)態(tài)設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08