C 中資料庫各種操作

2021-07-04 18:10:45 字數 4408 閱讀 5889

一:c#資料庫查詢之資料庫連線**:

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.

open

(); 

二:資料庫的新增記錄**:

inti=0;  

strings1=

"",s2=

"";  

i=convert

.toint16(textbox1.text);  

s1=textbox2.text;  

s2=textbox3.text;  

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.

open

();  

messagebox.show(

"資料庫連線成功"

,"好"

);  

try  

catch(exceptiona)  

messagebox.show(

"新增成功!"

,"啊"

);  

}

三:資料庫的修改**:

inti=0;  

strings1=

"",s2=

"";  

s1=textbox2.text;  

s2=textbox3.text;  

if(textbox1.text.length==0)  

i=0;  

else

i=convert

.toint32(textbox1.text);  

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.

open

();  

messagebox.show(

"資料庫連線成功"

,"好"

);  

try  

catch(exceptiona)  

四:資料庫的刪除**:

inti=0;  

strings1=

"",s2=

"";  

s1=textbox2.text;  

s2=textbox3.text;  

if(textbox1.text.length==0)  

i=0;  

else

i=convert.toint16(textbox1.text);  

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.open();  

messagebox.show(

"資料庫連線成功"

,"好"

);  

try  

catch

(exceptiona)  

五:資料庫的查詢**:

1.類開始:

datatabledt1=newdatatable();  

sqldataadapterda1=newsqldataadapter(); 

2.按鈕**:

inti=0,n=0;  

strings1=

"",s2=

"";  

s1=textbox2.text;  

s2=textbox3.text;  

if(textbox1.text.length==0)  

i=0;  

else

i=convert

.toint32(textbox1.text);  

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.

open

();  

messagebox.show(

"資料庫連線成功"

,"好"

);  

stringquery=

"select*frominfowhereid="

+i;  

datasetobjdataset=newdataset();  

sqldataadapterobj=newsqldataadapter();  

obj.selectcommand=newsqlcommand(query,objsqlconnection);  

obj.fill(objdataset,

"info"

);  

sqlcommandobjsqlcommand=newsqlcommand(query,objsqlconnection);  

sqldatareaderobjsqlreader=objsqlcommand.executereader();  

while(objsqlreader.

read

())  

if(n==0)  

messagebox.show(

"資料庫中沒有這樣的記錄!"

);

六:c#資料庫查詢**:

inti=0;  

//intn=0;

strings1=

"",s2=

"";  

stringsql;  

s1=textbox2.text;  

s2=textbox3.text;  

if(textbox1.text.length==0)  

else

i=convert.toint32(textbox1.text);  

sqlconnectionobjsqlconnection=newsqlconnection(

"server=127.0.0.1;uid=sa;pwd=;database=test"

);  

objsqlconnection.open();  

messagebox.show(

"資料庫連線成功"

,"好"

);  

stringquery=

"select*frominfowhereid="

+i;  

if(i==0)  

sql=

"select*frominfo"

;  else

sql=

"select*frominfowhereid="

+i;  

da1=newsqldataadapter(sql,objsqlconnection);  

dt1.clear();  

da1.fill(dt1);  

datagridview1.datasource=dt1; 

c#資料庫查詢之資料庫的封裝類**:

classdbclass  

catch

(exceptiona)  

}  }  --db2資料庫連線**:  

stringstrcon=

"provider=ibmdadb2;datasource=hfzd;userid=db2admin;password=db2admin"

;  //stringsql="select*fromadministrator.hfzd";

stringsql=

"deletefromadministrator.hfzdwhereid=1"

;  oledbconnectionolecon=newoledbconnection(strcon);  

olecon.open();  

messagebox.show(

"資料庫已連線上"

);  

dt.clear();  

da=newoledbdataadapter(sql,olecon);  

da.fill(dt);  

datagridview1.datasource=dt;  

olecon.close();

C 對資料庫各種操作

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...

C 對資料庫各種操作

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...

C 中資料庫操作

using system using system.data using system.data.sqlclient using system.web namespace moban 連線資料庫引數 public string con set 資料庫連線引數 public sqlconnection...