2012-10-26 63 views
0

我已經設計了一個表格在VS 2008我有包含以下字段的數據庫表中的數據:顯示從SQL數據庫

Fname (char) 
MailFrom (char) 
MailTo (char) 
Subject (char) 
Body (char) 
MailID (int) 

現在我到從數據庫中提取的數據,並在表格上顯示它在各自的領域。

我後面的代碼是:

SqlConnection conn = new SqlConnection(
     "Data Source=PTZ1\\SQLEXPRESS;Initial Catalog = test; Integrated Security=SSPI;User ID=sa; [email protected]; Trusted_Connection=True;"); 

    SqlDataReader rdr = null; 

    try 
    { 
     // Open the connection 
     conn.Open(); 

     // Pass the connection to a command object 
     SqlCommand cmd = new SqlCommand("select * from testing", conn); 

     // 
     // Use the connection 
     // 

     // get query results 
     rdr = cmd.ExecuteReader(); 

     while (rdr.Read()) 
     { 
      Console.WriteLine(rdr[]); 
     } 
    } 
    finally 
    { 
     // close the reader 
     if (rdr != null) 
     { 
      rdr.Close(); 
     } 

     // Close the connection 
     if (conn != null) 
     { 
      conn.Close(); 
     } 
    } 

如何存儲並顯示在控制檯上的數據

回答

3

可以使用DataTable存儲和那顯示存儲在內存中的查詢結果在Console數據:

// Create a String to hold the query. 
string query = "SELECT * FROM testing"; 

// Create a SqlCommand object and pass the constructor the connection string and the query string. 
SqlCommand cmd = new SqlCommand(query, conn); 

// Use the above SqlCommand object to create a SqlDataReader object. 
SqlDataReader rdr = queryCommand.ExecuteReader(); 

// Create a DataTable object to hold all the data returned by the query. 
DataTable dataTable = new DataTable(); 

// Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable. 
dataTable.Load(rdr); 

OR

  1. 確定您的自定義類,例如「Email`這樣:

    class Email 
    { 
        public string Fname { get; set; } 
        public string MailFrom { get; set; } 
        public string MailTo { get; set; } 
        public string Subject { get; set; } 
        public string Body { get; set; } 
        public int MailID { get; set; }  
    } 
    
  2. 閱讀值自定義類集合:

    List<Email> list = new List<Email>(); 
    
    while (rdr.Read()) 
    { 
        Email o = new Email() { Fname=rdr["Fname"], MailFrom=rdr["MailFrom"], 
         MailTo=rdr["MailTo"], Subject=rdr["Subject"], Body=rdr["Body"], 
         MailID=Convert.ToInt32(rdr["MailID"]) }; 
    
        Console.WriteLine("First Name: {0}", o.Fname); 
        Console.WriteLine("MailFrom: {0}", o.MailFrom); 
        Console.WriteLine("Mail To: {0}", o.MailTo); 
        Console.WriteLine("Subject: {0}", o.Subject); 
        Console.WriteLine("Body: {0}", o.Body); 
        list.Add(o); 
    } 
    
+0

我不能用**的**循環在'而(rdr.read())'命令和顯示數據? – Esha

+0

當然你也可以這樣使用它。看到我編輯的答案。 –

1

的代碼幾乎是準備在這一點上。

在reader.Read()循環中,您每次都要通過數據1記錄。

要訪問的列,用自己的名字

(char) reader["Fname"] 

現在,它取決於你想要做什麼。如果你想將它們存儲在自己的對象 - 這樣的事情

List<MyObject> myObjects = new List<MyObject>(); 

while (reader.Read()) 
{ 
MyObject obj = new MyObject; 
obj.Fname = (char) reader["Fname"]; 
obj.MailFrom = //you get the idea 
//etc 
} 

如果你想顯示他們,只是顯示他們,而不是。

0
 while(rdr.Read()) 
      { 
       Console.WriteLine(rdr["FirstName"].ToString());//or get column data by name 
       Console.WriteLine(rdr.GetString(1));//here accessing column by index 
      } 

ü只需要寫這個