人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 19, 2006

常用读取数据的方法

Filed under: SQL&DB Accessing

string connectionString = "";
string queryString = ""; 

//–使用dataset         
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
        connection.Open();
        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(ds);
    }
    catch (Exception exp)
    {
        Console.WriteLine(exp.Message);
    }
}

//–使用Data reader
List<MyType> fields = new List<MyType>();

using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
        connection.Open();
        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            fields.Add(new MyType(name, type));
        }
    }
    catch (Exception exp)
    {
        Console.WriteLine(exp.Message);
    }
}

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/05/19/p47/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham