六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

用Visual C#來增加數(shù)據(jù)記錄(2)

[摘要]二.用Visual C#往SQL SERVER數(shù)據(jù)庫(kù)中插入記錄: (1)用Visual C#往Access 2000和SQL SERVER添加記錄的主要區(qū)別在于使用了不同的數(shù)據(jù)庫(kù)引擎。在編寫程序之前,首先假設(shè)數(shù)據(jù)庫(kù)服務(wù)器名稱為:server1,要訪問的數(shù)據(jù)庫(kù)名稱為:data1,數(shù)據(jù)表名稱為:boo...

二.用Visual C#往SQL SERVER數(shù)據(jù)庫(kù)中插入記錄:
(1)用Visual C#往Access 2000和SQL SERVER添加記錄的主要區(qū)別在于使用了不同的數(shù)據(jù)庫(kù)引擎。在編寫程序之前,首先假設(shè)數(shù)據(jù)庫(kù)服務(wù)器名稱為:server1,要訪問的數(shù)據(jù)庫(kù)名稱為:data1,數(shù)據(jù)表名稱為:books。用戶名為:sa。其中數(shù)據(jù)表的數(shù)據(jù)結(jié)構(gòu)和Access 2000的表的結(jié)構(gòu)相同。下面是程序中打開SQL SERVER的數(shù)據(jù)引擎程序代碼:
// 設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開Sql server數(shù)據(jù)庫(kù),服務(wù)器名稱為server1,數(shù)據(jù)庫(kù)為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 數(shù)據(jù)庫(kù)中插入記錄的源程序代碼( add02.cs ):
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
//導(dǎo)入程序中使用到的名稱空間
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 ( ) {
//連接到一個(gè)數(shù)據(jù)庫(kù)
GetConnected ( ) ;
// 對(duì)窗體中所需要的內(nèi)容進(jìn)行初始化
InitializeComponent ( );
}
//釋放程序使用過的所以資源
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataAdd ( ) ) ;
}
public void GetConnected ( )
{
try{
// 設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開Sql server數(shù)據(jù)庫(kù),服務(wù)器名稱為server1,數(shù)據(jù)庫(kù)為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 " ;
//創(chuàng)建一個(gè) DataSet
myDataSet = new DataSet ( ) ;
//用 OleDbDataAdapter 得到一個(gè)數(shù)據(jù)集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//把Dataset綁定books數(shù)據(jù)表
myCommand.Fill ( myDataSet , "books" ) ;
//關(guān)閉此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "連接錯(cuò)誤! " + e.ToString ( ) , "錯(cuò)誤" ) ;
}
}
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 ( ) ;
//以下是對(duì)數(shù)據(jù)瀏覽的四個(gè)按鈕進(jìn)行初始化
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 ) ;
//以下是對(duì)顯示標(biāo)簽進(jìn)行初始化
l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
l_bookid.Text = "書本序號(hào):" ;
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 = "價(jià) 格:" ;
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 = "書 架 號(hào):" ;
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#來增加數(shù)據(jù)記錄!" ;
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 ) ;
//以下是對(duì)為顯示數(shù)據(jù)記錄而設(shè)定的標(biāo)簽和文本框進(jìn)行初始化,并把記錄綁定在不同的綁定到文本框"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#來增加數(shù)據(jù)記錄的程序窗口!" ;
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 ) ;
//把對(duì)象DataSet和"books"數(shù)據(jù)表綁定到此myBind對(duì)象
myBind= this.BindingContext [ myDataSet , "books" ] ;
}
protected void saveClick ( object sender , System.EventArgs e )
{
try
{
//判斷所有字段是否添完,添完則執(zhí)行,反之彈出提示
if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.Text != "" && t_bookprice.Text != "" && t_bookstock.Text != "" )
{
// 設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開Sql server數(shù)據(jù)庫(kù),服務(wù)器名稱為server1,數(shù)據(jù)庫(kù)為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 ( "必須填滿所有字段值!" , "錯(cuò)誤!" ) ;
}
}
catch ( Exception ed )
{
MessageBox.Show ( "保存數(shù)據(jù)記錄發(fā)生 " + ed.ToString ( ) , "錯(cuò)誤!" ) ;
}
}
protected void t_newClick ( object sender , System.EventArgs e )
{
t_bookid.Text = "" ;
t_booktitle.Text = "" ;
t_bookauthor.Text = "" ;
t_bookprice.Text = "" ;
t_bookstock.Text = "" ;
}
//按鈕"尾記錄"對(duì)象事件程序
protected void GoLast ( object sender , System.EventArgs e )
{
myBind.Position = myBind.Count - 1 ;
}
//按鈕"下一條"對(duì)象事件程序
protected void GoNext ( object sender , System.EventArgs e )
{
if ( myBind.Position == myBind.Count -1 )
MessageBox.Show ( "已經(jīng)到了最后一條記錄!" ) ;
else
myBind.Position += 1 ;
}
//按鈕"上一條"對(duì)象事件程序
protected void GoPrevious ( object sender , System.EventArgs e )
{
if ( myBind.Position == 0 )
MessageBox.Show ( "已經(jīng)到了第一條記錄!" ) ;
else
myBind.Position -= 1 ;
}
//按鈕"首記錄"對(duì)象事件程序
protected void GoFirst ( object sender , System.EventArgs e )
{
myBind.Position = 0 ;
}
}


三.總結(jié):
本文主要是通過二個(gè)程序的例子來具體說明用Visual C#如何往遠(yuǎn)程數(shù)據(jù)庫(kù)--SQL SERVER和本地?cái)?shù)據(jù)庫(kù)-- Access 2000中插入記錄。對(duì)于其他類型的數(shù)據(jù)庫(kù)也可以比照這二個(gè)這二個(gè)數(shù)據(jù)庫(kù)來處理,一般來說,用Visual C#處理數(shù)據(jù)庫(kù)只是在選用數(shù)據(jù)庫(kù)引擎上有較大的差別,在程序中的具體設(shè)計(jì)和處理上,還是很類似的。最后希望此篇文章能對(duì)你的數(shù)據(jù)庫(kù)編程有所幫助。


 




主站蜘蛛池模板: 一二三四免费观看在线8 | 日本成人在线网站 | 一区二区三区在线免费视频 | 天天色综合图片 | 天堂网在线最新版www中文网 | 亚洲精品视频在线播放 | 亚洲成人福利网站 | 青青草原免费在线视频 | 图片亚洲va欧美va国产综合 | 日韩爱爱网 | 性色视频在线观看 | 日本黄色网址免费 | 色综合久久综合欧美综合 | 性福利影院 | 性高湖久久久久久久久 | 视频二区 调教中字 知名国产 | 色狠台湾色综合网站 | 一级黄色片免费的 | 午夜视频成人 | 妖精视频亚洲 | 天天干天天操天天碰 | 亚洲第一网站在线观看 | 性爽视频 | 又粗又硬又爽的三级视频在线观看 | 四虎国产成人免费观看 | 亚洲国产精品成人综合色在线婷婷 | 四虎影院地址 | 日本www在线观看 | 天堂中文字幕在线 | 天天色天天摸 | 最近中文字幕无免费视频 | 色吊丝最新永久免费观看网站 | 伊人久久大香线蕉观看 | 青青草视频在线观看 | 色天天综合久久久久综合片 | 青草青青高清影院在线观看 | 亚洲全网成人资源在线观看 | 日本一区不卡在线观看 | 天天干在线影院 | 熟女毛片 | 四虎8848永久在线 |