我正在创建一个需要留言簿的应用程序..数据库中的数据显示在网站上,但我从网站输入的任何数据都不会自动放入数据库。
这是我的代码..
Protected Sub ButSign_Click(sender As Object, e As EventArgs) Handles ButSign.Click
Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "
Data source=C:\USERS\SHAUNA WATSON\DESKTOP\PROJECT COPIES\FINAL\APP_DATA\FACTORY.MDF"
Dim strSQL As String = (((("INSERT INTO Guestbook " & "( Name,Address,Email,Comments)" & "VALUES ('")
+ TxtName.Text.ToString() & " ',' ") + TxtAddress.Text.ToString() & " ', '") + TxtEmail.Text.ToString() & " ',' ") + TxtComments.Text.ToString() & " ')"
' set Access connection and select strings
' Create oleDbDataAdapter
Dim myConn As New OleDbConnection(strDSN)
'Create ole Db Command And call ExecuteNonQuery to execute
' a SQL statement
Dim myCmd As New OleDbCommand(strSQL, myConn)
Try
myConn.Open()
myCmd.ExecuteNonQuery()
Catch exp As Exception
Console.WriteLine("Error: {0}", exp.Message)
End Try
myConn.Close()
' open Thans.aspx page after adding entries to the guest book
Response.Redirect("GuestbookThanks.aspx")
End Sub
我只是想知道是否有人能发现我可能错过的东西!
答案 0 :(得分:0)
您可以尝试将代码更改为:
myConn.Open()
Dim myCmd As New OleDbCommand
With myCmd
.Connection = myConn
.CommandType = CommandType.Text
.CommandText = "INSERT INTO Guestbook (Name,Address,Email,Comments) VALUES (@Name,@Address,@Email,@Comments)"
.Parameters.Add("@Name", OleDbType.VarChar).Value = TxtName.Text
.Parameters.Add("@Address", OleDbType.VarChar).Value = TxtAddress.Text
.Parameters.Add("@Email", OleDbType.VarChar).Value = TxtEmail.Text
.Parameters.Add("@Comments", OleDbType.VarChar).Value = TxtComments.Text
End With
myCmd.ExecuteNonQuery()
myConn.Close()
请勿将其放入Try Block中。如果您收到错误,只需发布错误,我们就会看到错误是什么。