五.插入数据记录:
和前面二种操作在思路是一致的,就是通过ADO.NET首先插入数据记录到数据库,然后对"DataSet"对象进行必要的修改。下列代码就是以Access 2000数据库为模型插入一条数据记录的代码
同样对Sql Server 2000数据库进行插入记录操作和Access 2000数据库插入记录操作的差异也只在于不同的数据链接,具体的代码可以参考"删除数据记录"中的代码,在这里就不提供了。[@more@]
和前面二种操作在思路是一致的,就是通过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数据库插入记录操作的差异也只在于不同的数据链接,具体的代码可以参考"删除数据记录"中的代码,在这里就不提供了。[@more@]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924583/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8781179/viewspace-924583/