2015-02-09 181 views
-1

當我嘗試運行此代碼,我得到這個錯誤:vb.net數據庫CommandText屬性尚未初始化(system.data.dll中)

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 
+0

的一個你想讀者什麼樣的回報?你有一個SQL查詢執行? – 2015-02-09 13:37:52

+0

閱讀您用作標題的錯誤消息的實際字詞,然後問自己初始化代碼中的哪個位置(將值賦給)CommandText屬性。消息中的單詞使得它非常清楚問題是什麼 - 它是學習真正閱讀這些單詞絕對必需的。它們不只是隨機字母,他們實際上提供有意義的信 – 2015-04-17 00:52:26

回答

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

相關問題