SQL事务机制


                DateTime indate 
= CheckedDateFunction();
                
string connstr = Connectionstr.ConnectionString;
                OleDbConnection oleconn 
= new OleDbConnection(connstr);

                oleconn.Open();
                OleDbCommand myCommand 
= oleconn.CreateCommand();
                OleDbTransaction myTrans;
                myTrans 
= oleconn.BeginTransaction();
                myCommand.Connection 
= oleconn;
                myCommand.Transaction 
= myTrans;


                
try
                
...{
                    myCommand.CommandText 
= "Insert Into ClientRecord(ClientID,ClientName,RoomID,[ClientMemo],InDate,CheckDate,Settle) values( '" + txtClientID.Text + "'" +
                                                                                                                              
",'" + txtClientName.Text + "'" +
                                                                                                                              
",'" + txtNum.Text + "'" +
                                                                                                                              
",'" + txtMemo.Text + "'" +
                                                                                                                              
",'" + indate.ToString() + "'" +
                                                                                                                              
",Now(),0 ) ";//[Memo]不能少了中括号
                    string inserttemp = myCommand.ExecuteNonQuery().ToString();



                    
if (int.Parse(inserttemp) == 1)
                    
...{
                        myCommand.CommandText 
= "Insert Into Client(ClientID,ClientName,ClientSex,ClientAddress) values('" + txtClientID.Text + "'" +
                                                                                                                       
",'" + txtClientName.Text + "'" +
                                                                                                                       
",'" + cboSex.Text + "'" +
                                                                                                                       
",'" + cboClientAddress.Text + "')";
                        
string insertclientinfo = myCommand.ExecuteNonQuery().ToString();

                        
if (int.Parse(insertclientinfo) == 1)
                        
...{
                            myCommand.CommandText 
= "Update Room Set ActuallyCount = [ActuallyCount] +" + Convert.ToInt16(cboPutupPeople.Text) + ",RoomState = 1 where  RoomNumber = '" + txtNum.Text.ToString() + "'";
                            myCommand.ExecuteNonQuery();
                            myTrans.Commit();
                            MessageBox.Show(
"操作成功!欢迎光临!""成功提示");
                            
this.Close();
                        }

                        
else
                        
...{
                            myTrans.Rollback();
                            
return;
                        }

                    }

                    
else
                    
...{
                        myTrans.Rollback();
                        
return;
                    }


                }

                
catch (Exception ex)
                
...{
                    myTrans.Rollback();
                    MessageBox.Show(
"Error:" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                
finally
                
...{
                    oleconn.Close();
                }
阅读更多
文章标签: sql
个人分类: 我的C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭