日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#連接數(shù)據(jù)庫的兩種方法

兩種C#連接數(shù)據(jù)庫方法包括用MySQL DriverCS連接MySQL數(shù)據(jù)庫和通過ODBC訪問MySQL數(shù)據(jù)庫。希望這些能對大家有所幫助。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了睢寧縣免費(fèi)建站歡迎大家使用!

C#連接數(shù)據(jù)庫1、用MySQL DriverCS連接MySQL數(shù)據(jù)庫

先下載和安裝MySQL DriverCS,地址:

http://sourceforge.net/projects/mysqldrivercs/

在安裝文件夾下面找到MySQLDriver.dll,然后將MySQLDriver.dll添加引用到項(xiàng)目中

注:我下載的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Data.Odbc;  
  6. using System.Drawing;  
  7. using System.Linq;  
  8. using System.Text;  
  9. using System.Windows.Forms;  
  10. using MySQLDriverCS;  
  11. namespace mysql  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void Form1_Load(object sender, EventArgs e)  
  20.         {  
  21.             MySQLConnection conn = null;  
  22.             conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);  
  23.             conn.Open();  
  24.             MySQLCommand commn = new MySQLCommand("set names gb2312", conn);  
  25.             commn.ExecuteNonQuery();  
  26.             string sql = "select * from exchange ";  
  27.             MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);  
  28.             DataSet ds = new DataSet();  
  29.            mda.Fill(ds, "table1");  
  30.             this.dataGrid1.DataSource = ds.Tables["table1"];  
  31.             conn.Close();  
  32.        }  
  33.    }  

C#連接數(shù)據(jù)庫2、通過ODBC訪問mysql數(shù)據(jù)庫:

參考:http://www.microsoft.com/china/community/Column/63.mspx

1.      安裝Microsoft ODBC.net:我安裝的是mysql-connector-odbc-3.51.22-win32.msi

2.      安裝MDAC 2.7或者更高版本:我安裝的是mdac_typ.exe 2.7簡體中文版

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;

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Linq;   //vs2005好像沒有這個(gè)命名空間,在c#2008下測試自動(dòng)生成的  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using Microsoft.Data.Odbc;  
  9. namespace mysql  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void Form1_Load(object sender, EventArgs e)  
  18.         {  
  19.             string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +  
  20.                                  "SERVER=localhost;" +  
  21.                                  "DATABASE=inv;" +  
  22.                                  "UID=root;" +  
  23.                                  "PASSWORD=831025;" +  
  24.                                  "OPTION=3";  
  25.             OdbcConnection MyConnection = new OdbcConnection(MyConString);  
  26.             MyConnection.Open();  
  27.             Console.WriteLine(""n success, connected successfully !"n");  
  28.             string query = "insert into test values( 'hello', 'lucas', 'liu')";  
  29.             OdbcCommand cmd = new OdbcCommand(query, MyConnection);  
  30.                   //處理異常:插入重復(fù)記錄有異常  
  31. try{  
  32.   cmd.ExecuteNonQuery();  
  33. }  
  34. catch(Exception ex){  
  35.                  Console.WriteLine("record duplicate.");  
  36. }finally{  
  37.                  cmd.Dispose();  
  38. }  
  39. //***********************用read方法讀數(shù)據(jù)到textbox**********************  
  40.             string tmp1 = null;  
  41.             string tmp2 = null;  
  42.             string tmp3 = null;  
  43.            query = "select * from test ";  
  44.             OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);  
  45.             OdbcDataReader reader = cmd2.ExecuteReader();  
  46.             while (reader.Read())  
  47.             {  
  48.                 tmp1 = reader[0].ToString();  
  49.                 tmp2 = reader[1].ToString();  
  50.                 tmp3 = reader[2].ToString();  
  51.            }  
  52.             this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;  
  53.             */  
  54. //************************用datagridview控件顯示數(shù)據(jù)表**************************  
  55. string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +  
  56.                                  "SERVER=localhost;" +  
  57.                                  "DATABASE=inv;" +  
  58.                                  "UID=root;" +  
  59.                                  "PASSWORD=831025;" +  
  60.                                  "OPTION=3";  
  61.           OdbcConnection MyConnection = new OdbcConnection(MyConString);  
  62. OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);  
  63. DataSet ds = new DataSet();  
  64.           oda.Fill(ds, "employee");  
  65.           this.dataGridView1.DataSource = ds.Tables["employee"];  
  66. */  
  67.            MyConnection.Close();  
  68.         }  
  69.     }  

標(biāo)題名稱:C#連接數(shù)據(jù)庫的兩種方法
網(wǎng)頁路徑:http://www.dlmjj.cn/article/dpigshd.html