vb.net数据库commandtext属性尚未初始化(system.data.dll)

时间:2015-02-09 13:28:06

标签: vb.net

当我尝试运行此代码时,出现此错误:

An unhandled exception of type System.InvalidOperationException' occurred in System.Data.dll

其他信息:ExecuteReader: CommandText property has not been initialized

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand


        con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\database test\ikeagoed\ikeagoed\test.mdf;Integrated Security=True"
        con.Open()
        cmd.Connection = con

        Dim reader As SqlDataReader = cmd.ExecuteReader() <<error at this line
        If Not reader.HasRows Then
            'the data does not exist.
            MsgBox("bestaat niet!", MsgBoxStyle.Exclamation, "Add New User!")
        Else
            'The record exists
            MsgBox("User Already Exist!", MsgBoxStyle.Exclamation, "Add New User!")
        End If

        con.Close()

    End Sub

1 个答案:

答案 0 :(得分:0)

你的代码中没有任何地方告诉sql server你正在搜索的是什么。

某处你需要一个SQL查询

cmd.CommandText = "select * from users where userName = @userName" ' This query will be used for finding all users with that user name

只是您可以做的事情的一个例子。你也想查找sql​​参数

Here只是学习sql的众多网站之一