您好,我是刚学vb.net 的, 只插入一条记录 又开启事务怎么会有错误! 代码: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim datatransaction As SqlTransaction Try If mycon.State = ConnectionState.Closed Then mycon.Open() End If '打开连接 Dim data As New SqlParameter("@data", SqlDbType.SmallDateTime) '日期 Dim ID As New SqlParameter("@ID", SqlDbType.SmallInt, 10) 'ID Dim yhjr As New SqlParameter("@yhjr", SqlDbType.Money, 10) '银行金额 Dim jsjr As New SqlParameter("@jsjr", SqlDbType.Money, 13) '即时余额 Dim sfcf As New SqlParameter("@sfcf", SqlDbType.Bit) '是否拆分 Dim dm As New SqlParameter("@dm", SqlDbType.VarChar, 10) '代码 Dim sjhm As New SqlParameter("@sjhm", SqlDbType.VarChar, 10) '收据号码 Dim gx As New SqlParameter("@gx", SqlDbType.VarChar, 1) '管型 Dim lzjr As New SqlParameter("@lzjr", SqlDbType.Money, 10) '入账金额 Dim str As String Dim com1 As SqlCommand = New SqlCommand("select max(ID) from [" + Trim(ComboBox1.Text) + "]", mycon) '取最大ID Dim com2 As SqlCommand = New SqlCommand("select sum([" + Trim(ComboBox1.Text) + "].银行金额)+banks.初始余额 from [" _ + Trim(ComboBox1.Text) + "],banks where banks.ID=[9559900020000274717].卡号 group by banks.初始余额", mycon) '取最当前余额
str = "insert into [" + Trim(ComboBox1.Text) + "](ID,日期,银行金额,即时余额,是否拆分,代码,收据号码,管型,入账金额,是否结转)" + _ " values(@ID,@data,@yhjr,@jsjr,@sfcf,@dm,@sjhm,@gx,@lzjr,0)" Dim com As SqlCommand = New SqlCommand(str, mycon) datatransaction = mycon.BeginTransaction com.Transaction = datatransaction If Me.DataGrid2.Visible = False Then data.Value = CDate(Trim(TextBox2.Text)) ID.Value = CType(com1.ExecuteScalar, Integer) + 1 yhjr.Value = CDec(TextBox3.Text) jsjr.Value = CType(com2.ExecuteScalar, Decimal) + CDec(TextBox3.Text) sfcf.Value = 0 dm.Value = Trim(TextBox1.Text) sjhm.Value = Trim(TextBox4.Text) gx.Value = Trim(TextBox6.Text) lzjr.Value = CDec(TextBox3.Text) com.Parameters.Add(data) com.Parameters.Add(ID) com.Parameters.Add(yhjr) com.Parameters.Add(jsjr) com.Parameters.Add(sfcf) com.Parameters.Add(dm) com.Parameters.Add(sjhm) com.Parameters.Add(gx) com.Parameters.Add(lzjr) com.ExecuteNonQuery() '提交更新 datatransaction.Commit() '提交事务 MsgBox("保存成功!") TextBox1.Focus() Else Dim myrow As DataRow 'Dim com2 As SqlCommand = New SqlCommand("select sum(" + Trim(ComboBox1.Text) + ".银行金额)+banks.初始余额 from [" _ '+ Trim(ComboBox1.Text) + "],banks where banks.ID=[9559900020000274717].卡号 group by banks.初始余额", mycon) '取最当前余额 data.Value = CDate(Trim(TextBox2.Text)) ID.Value = CType(com1.ExecuteScalar, Integer) + 1 gx.Value = Trim(TextBox6.Text) sfcf.Value = 0 jsjr.Value = CType(com2.ExecuteScalar, Decimal) + CDec(TextBox3.Text) i = 0 For Each myrow In myset.Tables("cftable").Rows If i = 0 Then yhjr.Value = CDec(TextBox3.Text) '银行金额 重点 Else yhjr.Value = 0
End If dm.Value = myrow("代码") sjhm.Value = myrow("收据号码") lzjr.Value = myrow("入账金额") com.Parameters.Add(data) com.Parameters.Add(ID) com.Parameters.Add(yhjr) com.Parameters.Add(jsjr) com.Parameters.Add(sfcf) com.Parameters.Add(dm) com.Parameters.Add(sjhm) com.Parameters.Add(gx) com.Parameters.Add(lzjr) com.ExecuteNonQuery() '提交更新 datatransaction.Commit() MsgBox("保存成功!") TextBox1.Focus() i = +1 Next End If mycon.Close() '关闭连接 Catch datatransaction.Rollback() MsgBox("更新失败!") End Try End Sub