0

내 웹 사이트의 코드를 디버깅 할 때이 오류가 발생합니다. 내 목표는 SQL Server 2008 데이터베이스에 데이터를 삽입하는 것입니다.Visual Studio 및 SQL Server 2008의 오류

소스 오류 :

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 예외, 부울 breakConnection) +5009584
System.D ata.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run (RunBehavior runBehavior, SqlCommand를 cmdHandler, SqlDataReader 개체 데이터 스트림, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 2275
.....

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 

감사합니다 사전에 vb.net로 작성된 코드 소스. 하는 SqlClient 엔진

또한
strSQL = "insert into userTD(username,password) values(@username,@password)" 

에 대한 매개 변수의 이름이 같은

답변

1

사용의 자리, 난 당신이 모든 일회용 객체에 대한 연결 (더 ​​나은의 using statement를 사용하는 것이 좋습니다,하지만 연결은 큰 문제가있는 경우입니다 당신은 예외를 얻는다. 그리고 그것은 열린 채로있다).

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 
+0

그것은 매우 감사한다. – user2649088