Sql不会运行执行阅读器

时间:2015-05-26 18:45:37

标签: sql vb.net

我知道ConfigurationManager.AppSettings("ADOConnectionString")为我的连接提供了正确的位置,而family_id是数字6(family_id=6我知道我的Product中有正确的数据}表格为什么这会超过执行读者?

Dim sqlConnection1 As New SqlConnection(ConfigurationManager.AppSettings("ADOConnectionString"))
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=@family_id"
cmd.Parameters.AddWithValue("@family_id", family_id)
cmd.CommandType = CommandType.Text
sqlConnection1.Open()

reader = cmd.ExecuteReader()

1 个答案:

答案 0 :(得分:3)

您尚未将连接附加到命令。一种方法是:

cmd.Connection = sqlConnection1

另一种方法是将它作为SqlCommand构造函数的一部分来完成:

    Dim sql As String
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader
    sql = "SELECT * FROM [TID].[dbo].[Product] WHERE family_id=@family_id"

    cmd = new SqlCommand(sql, sqlConnection1)