插入命令不要插入数据库?

时间:2010-12-18 20:47:07

标签: vb.net ado.net

HI

有人可以告诉我在这段代码中我做错了什么

Protected Sub insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Insert.Click
    Dim mydb As New OleDbConnection
    mydb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= |datadirectory|database.mdb;Persist Security Info=True")
    mydb.Open()
    Dim sqlstring = "INSERT INTO [maintable] ([field1], [field2]) VALUES (@textbox1, @textbox2);"
    Dim mydbcommand As New OleDbCommand(sqlstring, mydb)
    TextBox1.Text = mydbcommand.Parameters.Add("@textbox1", OleDbType.VarChar).Value
    TextBox2.Text = mydbcommand.Parameters.Add("@textbox2", OleDbType.VarChar).Value 
    mydbcommand.ExecuteNonQuery()
    mydb.Close()
    TextBox1.Text = ""
    TextBox2.Text = ""

End Sub

1 个答案:

答案 0 :(得分:5)

我认为你应该使用

mydbcommand.Parameters.Add("@textbox1", OleDbType.VarChar).Value = TextBox1.Text
mydbcommand.Parameters.Add("@textbox2", OleDbType.VarChar).Value = TextBox2.Text 

而不是

 TextBox1.Text = mydbcommand.Parameters.Add("@textbox1", OleDbType.VarChar).Value
 TextBox2.Text = mydbcommand.Parameters.Add("@textbox2", OleDbType.VarChar).Value 

尝试并告诉我你是否仍然面临问题