您好,登錄后才能下訂單哦!
C#中有哪些連接數(shù)據(jù)庫(kù)的方法,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
C#連接數(shù)據(jù)庫(kù)1、用MySQL DriverCS連接MySQL數(shù)據(jù)庫(kù)
在安裝文件夾下面找到MySQLDriver.dll,然后將MySQLDriver.dll添加引用到項(xiàng)目中
注:我下載的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySQLDriverCS; namespace mysql { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MySQLConnection conn = null; conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString); conn.Open(); MySQLCommand commn = new MySQLCommand("set names gb2312", conn); commn.ExecuteNonQuery(); string sql = "select * from exchange "; MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn); DataSet ds = new DataSet(); mda.Fill(ds, "table1"); this.dataGrid1.DataSource = ds.Tables["table1"]; conn.Close(); } } }
C#連接數(shù)據(jù)庫(kù)2、通過(guò)ODBC訪問(wèn)mysql數(shù)據(jù)庫(kù):
1. 安裝Microsoft ODBC.net:我安裝的是mysql-connector-odbc-3.51.22-win32.msi
2. 安裝MDAC 2.7或者更高版本:我安裝的是mdac_typ.exe 2.7簡(jiǎn)體中文版
3. 安裝MySQL的ODBC驅(qū)動(dòng)程序:我安裝的是 odbc_net.msi
4. 管理工具 -> 數(shù)據(jù)源ODBC –>配置DSN…
5. 解決方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)
6. 代碼中增加引用 using Microsoft.Data.Odbc;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; //vs2005好像沒(méi)有這個(gè)命名空間,在c#2008下測(cè)試自動(dòng)生成的 using System.Text; using System.Windows.Forms; using Microsoft.Data.Odbc; namespace mysql { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=inv;" + "UID=root;" + "PASSWORD=831025;" + "OPTION=3"; OdbcConnection MyConnection = new OdbcConnection(MyConString); MyConnection.Open(); Console.WriteLine(""n success, connected successfully !"n"); string query = "insert into test values( 'hello', 'lucas', 'liu')"; OdbcCommand cmd = new OdbcCommand(query, MyConnection); //處理異常:插入重復(fù)記錄有異常 try{ cmd.ExecuteNonQuery(); } catch(Exception ex){ Console.WriteLine("record duplicate."); }finally{ cmd.Dispose(); } //***********************用read方法讀數(shù)據(jù)到textbox********************** string tmp1 = null; string tmp2 = null; string tmp3 = null; query = "select * from test "; 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控件顯示數(shù)據(jù)表************************** string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=inv;" + "UID=root;" + "PASSWORD=831025;" + "OPTION=3"; OdbcConnection MyConnection = new OdbcConnection(MyConString); OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection); DataSet ds = new DataSet(); oda.Fill(ds, "employee"); this.dataGridView1.DataSource = ds.Tables["employee"]; */ MyConnection.Close(); } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。