答案:四.插入数据记录: </P><P> 对数据库进行插入记录操作和删除记录操作基本的思路是一致的,就是通过ADO.NET首先插入数据记录到数据库,然后对"DataSet"对象进行必要的修改。下列代码就是以Access 2000数据库为模型修改当前记录的代码: </P><P>protected void Update_record ( object sender , System.EventArgs e )
{
int i = myBind.Position ;
try{
file://连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
file://从数据库中修改指定记录
string strUpdt = " UPDATE person SET xm = '"
+ t_xm.Text + "' , xb = '"
+ t_xb.Text + "' , nl = "
+ t_nl.Text + " , zip = "
+ t_books.Text + " WHERE id = " + t_id.Text ;
OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;
myCommand.ExecuteNonQuery ( ) ;
myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
myConn.Close ( ) ;
}
catch ( Exception ed )
{
MessageBox.Show ( "修改指定记录错误: " + ed.ToString ( ) , "错误!" ) ;
}
myBind.Position = i ;
} </P><P> 由于对Sql Server 2000数据记录修改操作和Access 2000数据记录修改操作的差异只在于不同的数据链接,具体的代码可以参考"删除数据记录"中的代码,在这里就不提供了。
五.插入数据记录:
和前面二种操作在思路是一致的,就是通过ADO.NET首先插入数据记录到数据库,然后对"DataSet"对象进行必要的修改。下列代码就是以Access 2000数据库为模型插入一条数据记录的代码
protected void Insert_record ( object sender , System.EventArgs e )
{
try
{
file://判断所有字段是否添完,添完则执行,反之弹出提示
if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )
{
string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;
strInsert += t_id.Text + ", '" ;
strInsert += t_xm.Text + "', '" ;
strInsert += t_xb.Text + "', " ;
strInsert += t_nl.Text + ", " ;
strInsert += t_books.Text + ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;
myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
}
else
{
MessageBox.Show ( "必须填满所有字段值!" , "错误!" ) ;
}
}
catch ( Exception ed )
{
MessageBox.Show ( "保存数据记录发生 " + ed.ToString ( ) , "错误!" ) ;
}
}
同样对Sql Server 2000数据库进行插入记录操作和Access 2000数据库插入记录操作的差异也只在于不同的数据链接,具体的代码可以参考"删除数据记录"中的代码,在这里就不提供了。
六.Visual C#数据库编程的完成源代码和程序运行的主界面:
掌握了上面要点,编写一个完整的数据库编程的程序就显得非常容易了,下面是Visual C#进行数据库编程的完整代码(Data01.cs),此代码是以Access 2000数据库为模型设计的,具体如下:
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
public class Data : Form
{
private System.ComponentModel.Container components = null ;
private Button lastrec ;
private Button nextrec ;
private Button previousrec ;
private Button firstrec ;
private TextBox t_books ;
private TextBox t_nl ;
private ComboBox t_xb ;
private TextBox t_xm ;
private TextBox t_id ;
private Label l_books ;
private Label l_nl ;
private Label l_xb ;
private Label l_xm ;
private Label l_id ;
private Label label1 ;
private DataSet myDataSet ;
private Button button1 ;
private Button button2 ;
private Button button3 ;
private Button button4 ;
private BindingManagerBase myBind ;
public Data ( )
{
file://连接到一个数据库
GetConnected ( ) ;
// 对窗体中所需要的内容进行初始化
InitializeComponent ( ) ;
}
file://清除在程序中使用过的资源
protected override void Dispose( bool disposing )
{
if( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose( disposing ) ;
}
public static void Main ( )
{
Application.Run ( new Data ( ) ) ;
}
public void GetConnected ( )
{
try
{
file://创建一个 OleDbConnection
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM person " ;
file://创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "person" ) ;
file://关闭此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "连接错误! " + e.ToString ( ) , "错误" ) ;
}
}
private void InitializeComponent ( )
{
file://添加控件,略
this.Name = "Data" ;
this.Text = "Visual C#的数据库编程!" ;
this.ResumeLayout(false) ;
myBind = this.BindingContext [ myDataSet , "person" ] ;
}
protected void New_record ( object sender , System.EventArgs e )
{
t_id.Text = ( myBind.Count + 1 ).ToString ( ) ;
t_xm.Text = "" ;
t_xb.Text = "" ;
t_nl.Text = "" ;
t_books.Text = "" ;
}
protected void Insert_record ( object sender , System.EventArgs e )
{
try
{
file://判断所有字段是否添完,添完则执行,反之弹出提示
if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )
{
string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;
strInsert += t_id.Text + ", '" ;
strInsert += t_xm.Text + "', '" ;
strInsert += t_xb.Text + "', " ;
strInsert += t_nl.Text + ", " ;
strInsert += t_books.Te
上一个:DataGrid中加入CheckBox,并实现单选
下一个:VC#数据库编程1