SqlDataReader对象使用例子
if(!this.IsPostBack )
{
SqlConnection con=new SqlConnection( "server=.;database=MsgBoard;uid=sa;pwd=sa");
con.Open();
string mysql="select * from MsgBoard";
SqlCommand cmd=new SqlCommand(mysql,con);
cmd.Connection=con;
//Command对象的ExecuteReader方法,返回值为SqlDataReader
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
// get the results of each column
string Name = (string)sdr["MsgWriter"];
string Content = (string)sdr["MsgContent"];
string MyTime = (string)sdr["MsgTime"];
// print out the results
this.lblWriter.Text=Name.ToString();
this.tetContent.Text =Content.ToString();
this.lblTime.Text=MyTime;
}
{
SqlConnection con=new SqlConnection( "server=.;database=MsgBoard;uid=sa;pwd=sa");
con.Open();
string mysql="select * from MsgBoard";
SqlCommand cmd=new SqlCommand(mysql,con);
cmd.Connection=con;
//Command对象的ExecuteReader方法,返回值为SqlDataReader
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
// get the results of each column
string Name = (string)sdr["MsgWriter"];
string Content = (string)sdr["MsgContent"];
string MyTime = (string)sdr["MsgTime"];
// print out the results
this.lblWriter.Text=Name.ToString();
this.tetContent.Text =Content.ToString();
this.lblTime.Text=MyTime;
}
con.Close();
}
本文展示了一个使用SqlDataReader从数据库中读取数据并显示在网页上的.NET示例。通过SqlConnection连接到本地数据库,创建SqlCommand执行SQL查询,利用SqlDataReader逐行读取结果。

583

被折叠的 条评论
为什么被折叠?



