1

VS2008でフォームを設計しました。次のフィールドを含むデータベーステーブルがあります。

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

コンソールにデータを保存して表示するにはどうすればよいですか

4

3 に答える 3

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

また

  1. カスタムクラスを定義します。たとえば、次のように「Eメール」を送信します。

    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);
    } 
    
于 2012-10-26T06:11:24.167 に答える
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
}

それらを表示したい場合は、代わりに表示してください。

于 2012-10-26T06:17:48.887 に答える
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
            }

Uはこれを書く必要があります

于 2012-10-26T06:22:56.310 に答える