VB.NET SQL Server Insert - ExecuteNonQuery: Connection property has not been initialized

前端 未结 5 633
醉话见心
醉话见心 2021-01-20 01:17

In the form load event, I connect to the SQL Server database:

Private Sub AddBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles My         


        
5条回答
  •  后悔当初
    2021-01-20 01:54

    Please try to wrap the use of your connections (including just opening) inside a USING block. Assuming the use of web.config for connection strings:

        Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("web.config_connectionstring").ConnectionString)
        Dim query As New String = "select * from Table1"
        Dim command as New SqlCommand(query, connection)
    
    Using connection
       connection.Open()
       command.ExecuteNonQuery()
    End Using
    

    And PARAMETERIZE anything user-entered.. please!

提交回复
热议问题