2010-12-18 39 views
0

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 

回答

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 

嘗試做錯了,告訴我你是否仍然面臨問題