asp .Net 学习笔记 (二) DataSet

//业务类 opeartor.cs
public DataSet useDataSet()
      {
          String sql = "select * from tb_goodinfo";

          SqlDataAdapter da = new SqlDataAdapter();
        //用于影射表名
          da.TableMappings.Add("Table","tb_goodinfo");
       
          SqlConnection conn = new SqlConnection();
          conn.ConnectionString = "persist security info=False;Integrated Security=SSPI;server=localhost;Trusted_Connection=true;database=Test";
       
          conn.Open();
          SqlCommand cmd = new SqlCommand(sql,conn);

          cmd.CommandType= CommandType.Text;
         //查询
          da.SelectCommand = cmd;

          DataSet dataSet = new DataSet("Table");
          da.Fill(dataSet);

          return dataSet;

      }
/******/
在前台显示,如下:
    void showDataSetValue()
      {
          try
          {
              DataSet ds = new DataSet();
              opeartor op = new opeartor();
              ds=op.useDataSet();
              //得到表中的行 进行循环 并显示
              foreach (DataRow row in ds.Tables["tb_goodinfo"].Rows)
              {
                  Response.Write("Row: " + row["GoodsName"]);
                  this.MyLable.Text = row["GoodsName"].ToString();
              }
          }
          catch (Exception ex)
          {
              Response.Write("<font color=red>Error: </font>"+ex.Message);
          }
      }

posted on 2007-06-10 12:04    阅读(238)  评论(0)    收藏  举报

导航