兩種方法連線MySql資料庫

2021-09-06 15:30:28 字數 3323 閱讀 8404

1

、用mysqldrivercs連線mysql資料庫

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

private

void form1_load(object

sender, eventargs e)

} }

複製**

2、通過odbc訪問mysql資料庫:

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

2.安裝mdac 2.7或者更高版本:我安裝的是mdac_typ.exe 2

.7簡體中文版

3.安裝mysql的odbc驅動程式:我安裝的是 odbc_net.msi

4.管理工具 -> 資料來源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好像沒有這個命名空間,在c#2008下測試自動生成的

using

system.text;

using

system.windows.forms;

using

microsoft.data.odbc;

namespace

mysql

private

void form1_load(object

sender, eventargs e)

;" +

"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);

//處理異常:插入重覆記錄有異常

try

catch

(exception ex)

finally

//***********************用read方法讀資料到textbox**********************

string tmp1 = null

;

string tmp2 = null

;

string t*** = null

; query = "

select * from test

";

odbccommand cmd2 = new

odbccommand(query, myconnection);

odbcdatareader reader =cmd2.executereader();

while

(reader.read())

this.textbox1.text = tmp1 + "

" + tmp2 + "

" +t***;

*/

//************************用datagridview控制項顯示資料表**************************

string myconstring = "

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();

} }

複製**

www.unity蠻牛.com/6880.html

資料庫連線的兩種方法

方法一 使用jdbc odbc橋驅動程式連線資料庫 1.載入driver驅動程式 class.fornam sun.jdbc.odbc.jdbcodbcdriver 2.宣告jdbc url string url jdbc odbc person 3.建立連線 connection con driv...

c 連線MySql資料庫的兩種方法

今晚在家除錯了一下用c 連線mysql資料庫,有兩種方法可以成功訪問,以下是我測試通過的 測試環境 windows xp mysql 5.0.24 visual c 2008 express edition by lucas 2008.12.29 1 用mysqldrivercs連線mysql資料庫...

c 連線MySql資料庫的兩種方法

1 用mysqldrivercs連線mysql資料庫 在安裝資料夾下面找到mysqldriver.dll,然後將mysqldriver.dll新增引用到專案中 using system using system.collections.generic using system.componentmo...