二.用visual c#往sql server數據庫中插入記錄:
(1)用visual c#往access 2000和sql server添加記錄的主要區別在于使用了不同的數據庫引擎。在編寫程序之前,首先假設數據庫服務器名稱為:server1,要訪問的數據庫名稱為:data1,數據表名稱為:books。用戶名為:sa。其中數據表的數據結構和access 2000的表的結構相同。下面是程序中打開sql server的數據引擎程序代碼:
// 設定數據連接字符串,此字符串的意思是打開sql server數據庫,服務器名稱為server1,數據庫為data1
string strcon = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data1 ; data source = server1 " ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
myconn.open ( ) ;
(2).用visual c#往sql server 數據庫中插入記錄的源程序代碼( add02.cs ):
using system ;
using system.drawing ;
using system.componentmodel ;
using system.windows.forms ;
using system.data.oledb ;
using system.data ;
//導入程序中使用到的名稱空間
public class dataadd : form {
private button lastrec ;
private button nextrec ;
private button previousrec ;
private button firstrec ;
private container components ;
private label title ;
private button t_new ;
private button save ;
private textbox t_bookstock ;
private textbox t_bookprice ;
private textbox t_bookauthor ;
private textbox t_booktitle ;
private textbox t_bookid ;
private label l_bookstock ;
private label l_bookprice ;
private label l_bookauthor ;
private label l_booktitle ;
private label l_bookid ;
private dataset mydataset ;
private bindingmanagerbase mybind ;
//定義在程序中要使用的組件
public dataadd ( ) {
//連接到一個數據庫
getconnected ( ) ;
// 對窗體中所需要的內容進行初始化
initializecomponent ( );
}
//釋放程序使用過的所以資源
public override void dispose ( ) {
base.dispose ( ) ;
components.dispose ( ) ;
}
public static void main ( ) {
application.run ( new dataadd ( ) ) ;
}
public void getconnected ( )
{
try{
// 設定數據連接字符串,此字符串的意思是打開sql server數據庫,服務器名稱為server1,數據庫為data1,用戶名為sa。
string strcon = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data1 ; data source = server1 " ;
oledbconnection myconn = new oledbconnection ( strcon ) ;
myconn.open ( ) ;
string strcom = " select * from books " ;
//創建一個 dataset
mydataset = new dataset ( ) ;
//用 oledbdataadapter 得到一個數據集
oledbdataadapter mycommand = new oledbdataadapter ( strcom , myconn ) ;
//把dataset綁定books數據表
mycommand.fill ( mydataset , "books" ) ;
//關閉此oledbconnection
myconn.close ( ) ;
}
catch ( exception e )
{
messagebox.show ( "連接錯誤! " + e.tostring ( ) , "錯誤" ) ;
}
}
private void initializecomponent ( )
{
components = new system.componentmodel.container ( ) ;
nextrec = new button ( ) ;
lastrec = new button ( ) ;
previousrec = new button ( ) ;
firstrec = new button ( ) ;
t_bookprice = new textbox ( ) ;
l_booktitle = new label ( ) ;
l_bookprice = new label ( ) ;
l_bookauthor = new label ( ) ;
t_bookid = new textbox ( ) ;
save = new button ( ) ;
title = new label ( ) ;
t_bookauthor = new textbox ( ) ;
t_booktitle = new textbox ( ) ;
t_new = new button ( ) ;
l_bookstock = new label ( ) ;
t_bookstock = new textbox ( ) ;
l_bookid = new label ( ) ;
//以下是對數據瀏覽的四個按鈕進行初始化
firstrec.location = new system.drawing.point ( 65 , 312 ) ;
firstrec.forecolor = system.drawing.color.black ;
firstrec.size = new system.drawing.size ( 40 , 24 ) ;
firstrec.font = new system.drawing.font("仿宋", 8f );
firstrec.text = "首記錄";
firstrec.click += new system.eventhandler(gofirst);
previousrec.location = new system.drawing.point ( 135 , 312 ) ;
previousrec.forecolor = system.drawing.color.black ;
previousrec.size = new system.drawing.size(40, 24) ;
previousrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
previousrec.text = "上一條" ;
previousrec.click += new system.eventhandler ( goprevious ) ;
nextrec.location = new system.drawing.point ( 205 , 312 );
nextrec.forecolor = system.drawing.color.black ;
nextrec.size = new system.drawing.size ( 40 , 24 ) ;
nextrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
nextrec.text = "下一條" ;
nextrec.click += new system.eventhandler ( gonext );
lastrec.location = new system.drawing.point ( 275 , 312 ) ;
lastrec.forecolor = system.drawing.color.black ;
lastrec.size = new system.drawing.size ( 40 , 24 ) ;
lastrec.font = new system.drawing.font ( "仿宋" , 8f ) ;
lastrec.text = "尾記錄" ;
lastrec.click += new system.eventhandler ( golast ) ;
//以下是對顯示標簽進行初始化
l_bookid.location = new system.drawing.point ( 24 , 56 ) ;
l_bookid.text = "書本序號:" ;
l_bookid.size = new system.drawing.size ( 112, 20 ) ;
l_bookid.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookid.textalign = system.drawing.contentalignment.middlecenter ;
l_booktitle.location = new system.drawing.point ( 24 , 108 ) ;
l_booktitle.text = "書 名:";
l_booktitle.size = new system.drawing.size ( 112 , 20 ) ;
l_booktitle.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_booktitle.textalign = system.drawing.contentalignment.middlecenter ;
l_bookprice.location = new system.drawing.point ( 24 , 212 ) ;
l_bookprice.text = "價 格:" ;
l_bookprice.size = new system.drawing.size ( 112 , 20 ) ;
l_bookprice.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookprice.textalign = system.drawing.contentalignment.middlecenter ;
l_bookstock.location = new system.drawing.point ( 24 , 264 ) ;
l_bookstock.text = "書 架 號:" ;
l_bookstock.size = new system.drawing.size ( 112 , 20 ) ;
l_bookstock.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookstock.tabindex = 16 ;
l_bookstock.textalign = system.drawing.contentalignment.middlecenter ;
l_bookauthor.location = new system.drawing.point ( 24 , 160 ) ;
l_bookauthor.text = "作 者:" ;
l_bookauthor.size = new system.drawing.size ( 112 , 20 ) ;
l_bookauthor.font = new system.drawing.font ( "仿宋" , 10f ) ;
l_bookauthor.textalign = system.drawing.contentalignment.middlecenter ;
title.location = new system.drawing.point ( 32 , 16 ) ;
title.text = "利用vsiual c#來增加數據記錄!" ;
title.size = new system.drawing.size ( 336 , 24 ) ;
title.forecolor = system.drawing.color.green ;
title.font = new system.drawing.font ( "仿宋" , 14f , system.drawing.fontstyle.bold ) ;
//以下是對為顯示數據記錄而設定的標簽和文本框進行初始化,并把記錄綁定在不同的綁定到文本框"text"屬性上
t_bookid.location = new system.drawing.point ( 184 , 56 ) ;
t_bookid.size = new system.drawing.size ( 80 , 20 ) ;
t_bookid.databindings.add ( "text" , mydataset , "books.bookid" ) ;
t_bookstock.location = new system.drawing.point ( 184 , 264 ) ;
t_bookstock.size = new system.drawing.size ( 80 , 20 ) ;
t_bookstock.databindings.add ( "text" , mydataset , "books.bookstock" ) ;
t_booktitle.location = new system.drawing.point ( 184 , 108 ) ;
t_booktitle.size = new system.drawing.size ( 176 , 20 ) ;
t_booktitle.databindings.add( "text" , mydataset , "books.booktitle" ) ;
t_bookprice.location = new system.drawing.point ( 184 , 212 ) ;
t_bookprice.size = new system.drawing.size ( 80 , 20 ) ;
t_bookprice.databindings.add ( "text" , mydataset , "books.bookprice" ) ;
t_bookauthor.location = new system.drawing.point ( 184 , 160 ) ;
t_bookauthor.size = new system.drawing.size ( 128 , 20 ) ;
t_bookauthor.databindings.add ( "text" , mydataset , "books.bookauthor" ) ;
t_new.location = new system.drawing.point ( 62 , 354 ) ;
t_new.size = new system.drawing.size ( 96 , 32 ) ;
t_new.text = "新建記錄" ;
t_new.click += new system.eventhandler ( t_newclick ) ;
save.location = new system.drawing.point ( 222 , 354 ) ;
save.size = new system.drawing.size ( 96 , 32 ) ;
save.tabindex = 4 ;
save.text = "保存記錄" ;
save.click += new system.eventhandler ( saveclick ) ;
this.text = "利用vsiual c#來增加數據記錄的程序窗口!" ;
this.autoscalebasesize = new system.drawing.size ( 5 , 13 ) ;
this.formborderstyle = formborderstyle.fixed3d ;
this.clientsize = new system.drawing.size ( 390 , 400 ) ;
//在窗體中加入下列組件
this.controls.add ( lastrec ) ;
this.controls.add ( nextrec ) ;
this.controls.add ( previousrec ) ;
this.controls.add ( firstrec ) ;
this.controls.add ( title ) ;
this.controls.add ( t_new ) ;
this.controls.add ( save ) ;
this.controls.add ( t_bookstock ) ;
this.controls.add ( t_bookprice ) ;
this.controls.add ( t_bookauthor ) ;
this.controls.add ( t_booktitle ) ;
this.controls.add ( t_bookid ) ;
this.controls.add ( l_bookstock ) ;
this.controls.add ( l_bookprice ) ;
this.controls.add ( l_bookauthor ) ;
this.controls.add ( l_booktitle ) ;
this.controls.add ( l_bookid ) ;
//把對象dataset和"books"數據表綁定到此mybind對象
mybind= this.bindingcontext [ mydataset , "books" ] ;
}
protected void saveclick ( object sender , system.eventargs e )
{
try
{
//判斷所有字段是否添完,添完則執行,反之彈出提示
if ( t_bookid.text != "" && t_booktitle.text != "" && t_bookauthor.text != "" && t_bookprice.text != "" && t_bookstock.text != "" )
{
// 設定數據連接字符串,此字符串的意思是打開sql server數據庫,服務器名稱為server1,數據庫為data1,用戶名為sa。
string strconn = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = datal ; data source = server1 " ;
oledbconnection myconn = new oledbconnection ( strconn ) ;
myconn.open ( ) ;
string strinsert = " insert into books ( bookid , booktitle , bookauthor , bookprice , bookstock ) values ( " ;
strinsert += t_bookid.text + ", '" ;
strinsert += t_booktitle.text + "', '" ;
strinsert += t_bookauthor.text + "', " ;
strinsert += t_bookprice.text + ", " ;
strinsert += t_bookstock.text + ")" ;
oledbcommand inst = new oledbcommand ( strinsert , myconn ) ;
inst.executenonquery ( ) ;
myconn.close ( ) ;
}
else
{
messagebox.show ( "必須填滿所有字段值!" , "錯誤!" ) ;
}
}
catch ( exception ed )
{
messagebox.show ( "保存數據記錄發生 " + ed.tostring ( ) , "錯誤!" ) ;
}
}
protected void t_newclick ( object sender , system.eventargs e )
{
t_bookid.text = "" ;
t_booktitle.text = "" ;
t_bookauthor.text = "" ;
t_bookprice.text = "" ;
t_bookstock.text = "" ;
}
//按鈕"尾記錄"對象事件程序
protected void golast ( object sender , system.eventargs e )
{
mybind.position = mybind.count - 1 ;
}
//按鈕"下一條"對象事件程序
protected void gonext ( object sender , system.eventargs e )
{
if ( mybind.position == mybind.count -1 )
messagebox.show ( "已經到了最后一條記錄!" ) ;
else
mybind.position += 1 ;
}
//按鈕"上一條"對象事件程序
protected void goprevious ( object sender , system.eventargs e )
{
if ( mybind.position == 0 )
messagebox.show ( "已經到了第一條記錄!" ) ;
else
mybind.position -= 1 ;
}
//按鈕"首記錄"對象事件程序
protected void gofirst ( object sender , system.eventargs e )
{
mybind.position = 0 ;
}
}
三.總結:
本文主要是通過二個程序的例子來具體說明用visual c#如何往遠程數據庫--sql server和本地數據庫-- access 2000中插入記錄。對于其他類型的數據庫也可以比照這二個這二個數據庫來處理,一般來說,用visual c#處理數據庫只是在選用數據庫引擎上有較大的差別,在程序中的具體設計和處理上,還是很類似的。最后希望此篇文章能對你的數據庫編程有所幫助。