自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#連接數(shù)據(jù)庫的兩種方法

開發(fā) 后端
這里將介紹C#連接數(shù)據(jù)庫的兩種方法,這兩個方法主要是針對MySQL數(shù)據(jù)庫。MySQL數(shù)據(jù)庫在日常開發(fā)中因其免費(fèi)和開源性而備受歡迎,連接MySQL數(shù)據(jù)庫也是常見的手段。

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

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ū)動程序:我安裝的是 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好像沒有這個命名空間,在c#2008下測試自動生成的  
  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.     }  

【編輯推薦】

  1. C#與VB7比較詳解
  2. C#連接Access淺析
  3. C#創(chuàng)建XML Web services學(xué)習(xí)經(jīng)驗(yàn)
  4. C# Windows應(yīng)用程序概述
  5. C# SmartPhone程序?qū)W習(xí)筆記
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2011-05-24 09:18:59

C++連接mysql數(shù)據(jù)庫

2011-04-25 09:53:31

C++mysql

2009-08-18 11:23:11

2009-04-20 14:29:41

Oracle連接創(chuàng)建連接

2010-03-05 16:03:30

Python連接數(shù)據(jù)庫

2009-08-25 14:59:36

ASP.NET和C#連

2009-08-05 13:34:18

C#日期相減

2009-08-25 14:05:06

C#連接數(shù)據(jù)庫代碼

2019-03-27 14:41:41

Python數(shù)據(jù)庫Windows

2009-02-12 10:32:35

C#數(shù)據(jù)庫C#連接數(shù)據(jù)庫

2009-03-19 10:08:09

C#數(shù)據(jù)庫查詢

2009-09-04 17:49:34

C#連接數(shù)據(jù)庫

2009-08-17 08:29:00

c#刪除指定文件

2009-09-02 16:21:02

C#解析Html

2009-08-21 18:02:41

C#快捷鍵

2009-08-12 14:23:01

C#連接MySql數(shù)據(jù)

2009-08-03 17:53:11

XML數(shù)據(jù)

2011-07-14 17:50:50

domino數(shù)據(jù)庫

2011-08-29 17:25:21

Oracle日期范圍搜索

2009-08-17 17:48:00

C#自定義鼠標(biāo)樣式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號