本文介绍了ExecuteNonQuery:连接属性尚未初始化 VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里出现错误,不知道为什么没有打开连接.希望有人能帮助我.
Getting an error here not sure why its not opening the connection. Hoping someone can help me.
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
推荐答案
您的作用域有误 -- 在实例化并打开要尝试的实际 SQL 连接之前,您正在实例化 SqlCommand
用于执行命令.
You have the wrong scope -- you're instantiating your SqlCommand
before instantiating and opening the actual SQL connection you're trying to use to execute the command.
我相信这将修复您的代码(我将插入调用移至 using
范围):
I believe this is what will fix your code (I moved the insert call into the using
scope):
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
这篇关于ExecuteNonQuery:连接属性尚未初始化 VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!