显示SQL数据库中的数据

时间:2012-10-26 06:05:28

标签: c# asp.net

我在VS 2008中设计了一个表单。我有一个包含以下字段的数据库表:

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; Password=sa@; 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 个答案:

答案 0 :(得分: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. 确定您的自定义类,例如'电子邮件'这样:

    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);
    } 
    

答案 1 :(得分: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
}

如果您想要显示它们,只需显示它们。

答案 2 :(得分: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
            }

你只需要写这个