}
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 ( ) , "错误!" ) ;
}
}
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 ;
}
protected void Delete_record ( object sender , System.EventArgs e )
{
DialogResult r = MessageBox.Show ( "是否删除当前记录!" , "删除当前记录!" , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ;
int ss = ( int ) r ;
if ( ss == 6 ) // 按动 "确定"按钮
{
try{
file://连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strDele = "DELETE FROM person WHERE id= " + t_id.Text ;
OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;
file://从数据库中删除指定记录
myCommand.ExecuteNonQuery ( ) ;
file://从DataSet中删除指定记录
myDataSet.Tables [ "person" ] . Rows