Vb.net Visual Studio和SQL Server 2008出现错误
我在调试网站的代码时遇到此错误。我的目标是将数据插入SQLServer2008数据库 源错误:Vb.net Visual Studio和SQL Server 2008出现错误,vb.net,visual-studio-2010,sql-server-2008,web-site-project,Vb.net,Visual Studio 2010,Sql Server 2008,Web Site Project,我在调试网站的代码时遇到此错误。我的目标是将数据插入SQLServer2008数据库 源错误: objcnd.Parameters.Add(New SqlParameter("@username", strun)) Ligne 22 : objcnd.Parameters.Add(New SqlParameter("@password", strpw)) Ligne 23 : objcnd.ExecuteNonQuery() Lig
objcnd.Parameters.Add(New SqlParameter("@username", strun))
Ligne 22 : objcnd.Parameters.Add(New SqlParameter("@password", strpw))
Ligne 23 : objcnd.ExecuteNonQuery()
Ligne 24 : 'close connection
Ligne 25 : objconnection.Close()
错误
[SqlException(0x80131904):靠近“?”的语法不正确。]System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)+2030802
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔断开连接)+5009584
System.Data.SqlClient.TdsParser.throweexception和warning()+234
System.Data.SqlClient.TdsParser.Run(RunBehavior RunBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2275
用vb.net编写的源代码
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class index
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strun As String = Request.Form("username")
Dim strpw As String = Request.Form("password")
Dim objconnection As SqlConnection = Nothing
Dim objcnd As SqlCommand = Nothing
Dim strconnection As String, strSQL As String
'connection string
strconnection = ("Data Source=myPC-PC;Database=mytempDB;Integrated Security=true ")
objconnection = New SqlConnection(strconnection)
objconnection.ConnectionString = strconnection
objconnection.Open()
strSQL = "insert into userTD(username,password) values(?,?)"
objcnd = New SqlCommand(strSQL, objconnection)
objcnd.Parameters.Add(New SqlParameter("@username", strun))
objcnd.Parameters.Add(New SqlParameter("@password", strpw))
objcnd.ExecuteNonQuery()
'close connection
objconnection.Close()
Response.Write("Entered Successfully ! ")
End Sub
End Class
提前感谢。为SqlClient引擎使用具有相同参数名称的占位符
strSQL = "insert into userTD(username,password) values(@username,@password)"
此外,我建议您使用用于连接的(对于所有一次性对象更好,但是如果出现异常并且连接保持打开状态,则连接将是一个大问题)
Using objconnection = New SqlConnection(strconnection)
......
objcnd.ExecuteNonQuery()
' No need to close explicititly the connection, '
' the following End Using statement takes care of that'
' also in case of exceptions.....'
End Using