SQL事务机制

来源:互联网 发布:阿里云退票 编辑:程序博客网 时间:2024/06/06 00:04

                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();
                }
原创粉丝点击