using(SqlConnection scn=new SqlConnection("Server=.;Database=db;Integrated Security=sspi"))
{
scn.Open();
using(SqlCommand scm=scn.CreateCommand())
{
scm.CommandText="select * f rom student";
using(SqlDataAdapter sda=new SqlDataAdapter(scm))
{
DataTable dt=new DataTable();
sda.Fill(dt);
for(int i=0;i<dt.Rows.Count;i++)
{
for(int j=0;j<dt.Columns.Count;j++)
{
MessageBox.Show(dt[i][j]+" ");
}
}
}
能不能请高手帮我解读一下这段代码,越详细越好

解决方案 »

  1.   


    //准备一个连接对象,使用完自动释放
    using(SqlConnection scn=new SqlConnection("Server=.;Database=db;Integrated Security=sspi"))
    {
    //打开数据库连接
    scn.Open();
    //用现有连接对象创建一个Command对象,使用完自动释放
    using(SqlCommand scm=scn.CreateCommand())
    {
    //设置Command对象的text属性,sql语句或存储过程名
    scm.CommandText="select * f rom student";
    //创建适配器对象,用完自动释放
    using(SqlDataAdapter sda=new SqlDataAdapter(scm))
    {
    //创建一个新的数据表对象
    DataTable dt=new DataTable();
    //把适配器中取得的数据表填充到准备好的数据表对象
    sda.Fill(dt);
    //显示每个单元格的值
    for(int i=0;i<dt.Rows.Count;i++)
    {
    for(int j=0;j<dt.Columns.Count;j++)
    {
    MessageBox.Show(dt[i][j]+" ");
    }
    }
    }
      

  2.   

    http://msdn.microsoft.com/zh-cn/library
    具体用法可以去这里看
      

  3.   

    被老大骂是一种荣幸,嘻嘻,加油吧楼主;给你个参考文章吧
    http://blog.csdn.net/kkkkkxiaofei/article/details/7904569