1

此处出现错误不确定为什么它不打开连接。希望有人可以帮助我。

 Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
    Dim Sqlstr As String
    Dim con As SqlConnection
    Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
    Dim cmdInsert As New SqlCommand(Sqlstr, con)
    Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (@FirstName,@LastName,@Email,@Phone,@Address,@City,@State,@Zip)"
    Try
        Using connection As New SqlConnection(connectionString)
            connection.Open()
            cmdInsert.Parameters.Add("@FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
            cmdInsert.Parameters.Add("@LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
            cmdInsert.Parameters.Add("@Email", Data.SqlDbType.NVarChar).Value = Email.Text
            cmdInsert.Parameters.Add("@Phone", Data.SqlDbType.NChar).Value = Phone.Text
            cmdInsert.Parameters.Add("@Address", Data.SqlDbType.NVarChar).Value = Address.Text
            cmdInsert.Parameters.Add("@City", Data.SqlDbType.NVarChar).Value = City.Text
            cmdInsert.Parameters.Add("@State", Data.SqlDbType.NVarChar).Value = State.Text
            cmdInsert.Parameters.Add("@Zip", Data.SqlDbType.NChar).Value = Zip.Text
            cmdInsert.ExecuteNonQuery()
            connection.Close()
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
4

4 回答 4

3

您有两个 SqlConnection 对象 - 一个在 Dim con As SqlConnection 中,另一个在您的 using 语句中。在 using 语句之后创建 SqlCommand 并将连接传递给构造函数。

于 2013-05-14T20:40:00.110 回答
2

你有错误的范围 - 你在实例SqlCommand化和打开你试图用来执行命令的实际 SQL 连接之前实例化你的。

我相信这将修复您的代码(我将插入调用移至using范围):

Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
Dim Sqlstr As String
Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"
Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (@FirstName,@LastName,@Email,@Phone,@Address,@City,@State,@Zip)"

Try
    Using connection As New SqlConnection(connectionString)

        connection.Open()

        Dim cmdInsert As New SqlCommand(Sqlstr, connection)  <----- **** Moved this here, changed the connection

        cmdInsert.Parameters.Add("@FirstName", Data.SqlDbType.NVarChar).Value = FirstName.Text()
        cmdInsert.Parameters.Add("@LastName", Data.SqlDbType.NVarChar).Value = LastName.Text
        cmdInsert.Parameters.Add("@Email", Data.SqlDbType.NVarChar).Value = Email.Text
        cmdInsert.Parameters.Add("@Phone", Data.SqlDbType.NChar).Value = Phone.Text
        cmdInsert.Parameters.Add("@Address", Data.SqlDbType.NVarChar).Value = Address.Text
        cmdInsert.Parameters.Add("@City", Data.SqlDbType.NVarChar).Value = City.Text
        cmdInsert.Parameters.Add("@State", Data.SqlDbType.NVarChar).Value = State.Text
        cmdInsert.Parameters.Add("@Zip", Data.SqlDbType.NChar).Value = Zip.Text
        cmdInsert.ExecuteNonQuery()

        connection.Close()
    End Using
Catch ex As Exception
    MsgBox(ex.Message)
End Try
于 2013-05-14T20:50:26.000 回答
0

未声明/更新连接时,总是发生连接字符串属性错误

解决方案很简单

dim con as new SqlConnection 
con.connectionString = "Provide your SQL connection"
con.open
'write code or action you want to perform
con.close

希望能帮助到你。

于 2014-01-18T05:06:55.050 回答
0
 Protected Sub Btn_Submit_Click(ByVal sender As System.Object, e As System.EventArgs) Handles Btn_Submit.Click
    Dim Sqlstr As String

    ' =================================================
    ' This is a declaration, not an instantiation
    ' =================================================
    Dim con As SqlConnection      

    Dim connectionString As String = "Data Source=DB\TEST;Initial Catalog=Orders;Integrated Security=True"

    ' =================================================
    ' con is Nothing here
    ' =================================================
    Dim cmdInsert As New SqlCommand(Sqlstr, con)     

    Sqlstr = "insert into customers(FirstName,LastName,Email,Phone,Address,City,State,Zip) values (@FirstName,@LastName,@Email,@Phone,@Address,@City,@State,@Zip)"
    Try

        ' =================================================
        ' connection is not what you passed to cmdInsert
        ' =================================================
        Using connection As New SqlConnection(connectionString)
            connection.Open()
于 2013-05-14T20:52:23.340 回答