程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用Visual C#來增加數據記錄

用Visual C#來增加數據記錄

編輯:C#入門知識

作者:王天

在本篇文章中,我們將介紹Visual C#對數據庫的一個基本操作,即:如何往數據庫中添加記錄。我們將通過一些數據庫操作的例子,來具體說明一下。為了更清楚的說明這個問題,在選用數據庫方面采用了二種當前比較典型的數據庫,其一是本地數據庫--Access 2000,另外一個是遠程數據庫--SQL SERVER 7.0。首先介紹如何用Visual C#來添加Access 2000數據庫的記錄。

一.用Visual C#來添加Access 2000數據庫的記錄
(一).程序設計和運行的環境設置:
(1)視窗2000服務器版
(2)Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )
(3)本文程序使用的數據庫的介紹:

程序中使用的數據庫名稱為sample.mdb,在此數據庫中有一張數據表books。此數據表的結構如下:
字段名稱 字段類型 代表意思
Bookid 數字 序號
booktitle 文本 書籍名稱
bookauthor 文本 書籍作者
bookprice 數字 價格
bookstock 數字 書架號

(二).程序設計難點和應該注意的問題:
如何正確的往數據庫中添加記錄是本文要討論的一個重點和難點,下面就是解決這一問題的具體思路:
(1)創建並打開一個 OleDbConnection對象。
(2)創建一個插入一條記錄的SQL語句。
(3)創建一個OleDbCommand對象。
(4)通過此OleDbCommand對象完成對插入一條記錄到數據庫的操作。
以下是在程序中實現的具體語句:
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
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 ( ) ;


(三).用Visual C#來插入記錄的程序源代碼( add.cs )和執行後的界面:
下圖是add.cs編譯後的執行界面:



add.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{
//創建一個 OleDbConnection對象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM books " ;
//創建一個 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
//用 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.ContentAlignme

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved