2013-08-02 144 views
0

DataReader的努力在我的vb.net 頁面執行命令時拋出一個錯誤消息時收到錯誤,代碼拋出的錯誤是:試圖執行一個SQL命令

Dim connectionString As String 
Dim connection As SqlConnection 
Dim sql As String 

connectionString = \\\connection string\\\ 
connection = New SqlConnection(connectionString) 

sql = "select * from jb_jobs where city='Los Angeles' " 
connection.Open() 
Dim reader As SqlDataReader = sql.ExecuteReader() 

和錯誤是: 'ExecuteReader'不是'string'的成員 如何解決此問題?

+2

SQL不是的SqlCommand。您需要創建sqlcommand。 –

+0

你可能想看看SqlCommand對象。 – Chris

回答

2

嘗試添加此:

sql = "select * from jb_jobs where city='Los Angeles' "; 
    var sqlCommand = new SqlCommand(sql, connection); 
    sqlCommand.Connection.Open(); 
    var reader = sqlCommand.ExecuteReader(); 
+0

這個vb.net你使用c# – SK2185

1

添加此

connection.Open() 
Dim cmd as new SqlCommand(sql,connection)  
Dim reader As SqlDataReader = cmd.ExecuteReader() 
+0

[單參數構造函數](http://msdn.microsoft.com/zh-cn/library/sebfsz50.aspx)需要文本,而不是連接。 –

+0

@Damien_The_Unbeliever Oups,是的,你說得對。我會修好它。 –

0
Dim cmd As SqlCommand = new SqlCommand(); 

cmd=(sql,connection); 

cmd.CommandType=CommandType.Text; 

Dim reader As SqlDataReader = cmd.ExecuteReader()