2011-05-10 106 views
0

我想從Visual Web Developer中使用asp.net連接到SQL Server,但我面臨一些問題如果有人在這方面提供幫助,我將會很有幫助。在ASP.NET中連接到SQL服務器

public partial class _Default : Page 
{ 
    protected void Page_Load(object sender, EventArgs e)    
    {  
     SqlConnection conn = new SqlConnection("Server=localhost;" + "Database=DB;User ID=aaaa;" + "Password=aaaa"); 

     conn.Open(); SqlDataReader reader = conn.ExecuteReader(); 
     while (reader.Read()) { 
      employeesLabel.Text += reader["Name"] + "<br />"; 
     } 
     reader.Close(); 
     conn.Close();    
    } 
} 

我得到錯誤說

「System.Data.SqlClient.SqlConnection」不包含「的ExecuteReader」的定義,並沒有擴展方法「的ExecuteReader」接受的第一個參數可以找到類型'System.Data.SqlClient.SqlConnection'(你是否缺少使用指令或程序集引用?)

名稱'employeesLabel'不存在於curren上下文。

有人可以告訴可能的原因嗎?

+0

是否缺少using指令或程序集引用? – ariel 2011-05-10 08:25:54

+1

嗯執行有趣的連接.... – Ruben 2011-05-10 08:28:52

回答

4

我認爲你必須同時創建SqlCommand類的對象和命令字符串傳遞給它的構造。試試這個」

SqlConnection conn = new SqlConnection("Data Source=serverName;" 
      + "Initial Catalog=databaseName;" 
      + "Persist Security Info=True;" 
      + "User ID=userName;Password=password"); 

conn.Open(); 

// create a SqlCommand object for this connection 
SqlCommand command = conn.CreateCommand(); 
command.CommandText = "Select * from tableName"; 
command.CommandType = CommandType.Text; 

// execute the command that returns a SqlDataReader 
SqlDataReader reader = command.ExecuteReader(); 

// display the results 
while (reader.Read()) { 
    //whatever you want to do. 
} 

// close the connection 
reader.Close(); 
conn.Close(); 
0

您沒有傳遞任何命令/查詢的ExecuteReader這樣的事情是正確的:

SqlDataReader rdr = null; 
conn.Open(); 
SqlCommand cmd = new SqlCommand("select * from Customers", conn); 
rdr = cmd.ExecuteReader(); 
2

嘗試

SqlConnection conn = new SqlConnection("Server=localhost;" + "Database=DB;User ID=aaaa;" + "Password=aaaa");    
conn.Open(); 
SqlCommand cmd = new SqlCommand("Your Query", conn);//Put your query here 
SqlDataReader reader = cmd.ExecuteReader();    
while (reader.Read()) 
{     
    employeesLabel.Text += reader["Name"] + "<br />"; 
} 
reader.Close();    
conn.Close(); 
+0

THX答覆他的錯誤‘‘employeesLabel’這個名字在目前情況下’不存在 – Naresh 2011-05-10 08:38:36

+0

employeesLabel.Text + = reader [「Name」] +「
」; 您是否可以在上面的行中看到任何錯誤? – Naresh 2011-05-10 08:53:56

+0

使用要設置其Text屬性的Label的名稱更新'employeesLabel' – 2011-05-10 09:27:29

1
using (SqlConnection conn = new SqlConnection(...)) 
using (SqlCommand command = conn.CreateCommand()) 
{ 
    command.CommandText = "..."; 
    conn.Open(); 
    using (SqlDataReader reader = command.ExecuteReader()) 
    { 
     // do the sutff 
    } 
} 
0

SqlConnection沒有任何ExecuteReader()方法。 您必須創建SqlCommand的對象。 您的代碼應該是這樣的:

namespace LearningASP 

{ public partial class _Default : System.Web.UI.Page 

{ protected void Page_Load(object sender, EventArgs e) 

{ 

SqlConnection conn = new SqlConnection("Server=localhost;" + "Database=DB;User ID=aaaa;" + "Password=aaaa"); 

     conn.Open(); 
**SqlCommand cmd = new SqlCommand(); 
     SqlDataReader reader = cmd.ExecuteReader();** 
while (reader.Read()) {    employeesLabel.Text += reader["Name"] + "<br />"; } reader.Close();   conn.Close();  } } } 
+0

正確設置您的答案! – abatishchev 2011-05-10 08:32:47