为什么INSERT INTO 向ACESS一次插入两条数据(新手上路)请多多指教
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox3.Text.Trim() <> Me.TextBox4.Text.Trim() Then
Me.ClientScript.RegisterStartupScript(Me.GetType(), "", "<script>alert('前后两次输入的密码不一致');</script>")
Me.TextBox4.Focus()
Return
End If
If Me.TextBox3.Text.Trim() = "" Then
Me.ClientScript.RegisterStartupScript(Me.GetType(), "", "<script>alert('密码不能为空');</script>")
Me.TextBox3.Focus()
Return
End If
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("App_Data/DataBase.mdb")
conn.Open()
Dim cmd As New Data.OleDb.OleDbCommand("select id,name,password from admin where id=" + Me.hf_uid.Value + " and password='" + Me.TextBox3.Text.Trim() + "'", conn)
ds = New Data.DataSet
Dim dap As New Data.OleDb.OleDbDataAdapter(cmd)
dap.Fill(ds, "seach")
dtable = ds.Tables(0)
Dim cmdup As New Data.OleDb.OleDbCommand("insert into admin(name,[password]) values('" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "')", conn)
Dim ia As Integer = cmdup.ExecuteNonQuery()
If ia > 0 Then
If ds.ToString.Trim() <> TextBox2.Text.Trim() Then
Me.ClientScript.RegisterStartupScript(Me.GetType(), "", "<script>alert('注册成功');location.href='login.aspx';</script>")
Else
Me.ClientScript.RegisterStartupScript(Me.GetType(), "", "<script>alert('用户已经存在,请重新操作');</script>")
Me.TextBox2.Focus()
End If
Else
Me.ClientScript.RegisterStartupScript(Me.GetType(), "", "<script>alert('注册失败,请重新操作');</script>")
Me.TextBox2.Focus()
End If
conn.Close()
End Sub
[解决办法]
插入一般都是一次一条 。
启动事务,执行多条插入,事实上可以认为是同时插入
[解决办法]
Dim cnt As New ADODB.Connection
On Error GoTo ErrHandler
cnt.BeginTrans
For i = 0 To n
cnt.Execute "Insert into theTable(.....) VALUES(.........)"
Next
cnt.CommitTrans
Exit Sub 'exit function
ErrHandler:
cnt.RollbackTrans