SqlDataAdapter da 和 DataSet ds 如何反复使用?及:在一个查询完毕了 对另一个表进行查询C#代码如何写?

解决方案 »

  1.   

    eg: SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
                da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
                DataSet ds = new DataSet();
                da.Fill(ds, "city");            da = new SqlDataAdapter("select * from otherTable", conn);            da.Fill(ds, "otherTable");
      

  2.   


    using (SqlConnection conn = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Test"))
            {
               SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
                da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
                DataSet ds = new DataSet();
                da.Fill(ds, "city");            da = new SqlDataAdapter("select * from otherTable", conn);            da.Fill(ds, "otherTable");
           }
      

  3.   

    顶!阿非给出了示例代码!
    SqlDataAdapter,你可以理解为去数据库获取数据的搬运工,你只要告诉她去哪里搬运数据(SqlConnection对象),以及搬运什么数据(它的SelectCommand对象所指定的sql语句,其实我们通过new SqlDataAdapter(sqlCommandText,SqlConnection)创建一个SqlDataAdapter时,就是指定了它的SelectCommand对象的命令)然后你一个Fill调用,它就去乖乖的搬运数据了,搬运回来以后,放在哪里,通过Fill方法的参数来指定,第一个参数是一个DataSet对象,就是一个数据集,你可以理解它是一个脱机的、本地内存中的数据库,既然是数据库,就能存放很多表,你每次用不同的读取数据命令,让SqlDataAdapter去搬运数据后,只要指定Fill方法中不同的第二个参数(就是表名),Fill方法就会自动将数据放在本地内存数据库的不同表之中了。
      

  4.   

        protected void SqLCon()
        {
            strConstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn = new SqlConnection();
            DataSet ds = new DataSet();
            try
            {  
                conn.ConnectionString = strConstring;
                conn.Open();
                
                da = new SqlDataAdapter("select ZhType from 帐户类别", conn);
                da.Fill(ds);            DropDownList32.Items.Clear();
                DropDownList32.DataTextField = "ZhType";
                DropDownList32.DataValueField = "ZhType";
                DropDownList32.DataSource = ds;
                DropDownList32.DataBind();
                DropDownList32.SelectedValue = "";
                DropDownList32.SelectedIndex = 0;
                strTypeVal = DropDownList32.SelectedItem.Text;
     
                DropDownList33.Items.Clear();
                DropDownList33.DataTextField = "ZhType";
                DropDownList33.DataValueField = "ZhType";
                DropDownList33.DataSource = ds;
                DropDownList33.DataBind();
                DropDownList33.SelectedValue = "";
                DropDownList33.SelectedIndex = 0;
                strTypeVal = DropDownList33.SelectedItem.Text;     
           
                da.= new SqlDataAdapter("select distinct(sheng) from 地名表", conn);
                da.Fill(ds);
                
                DropDownList2.Items.Clear();
                DropDownList2.DataTextField = "sheng";
                DropDownList2.DataValueField = "sheng";
                DropDownList2.DataSource = ds;
                DropDownList2.DataBind();
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS1001: 应输入标识符源错误: 行 114:            strTypeVal = DropDownList33.SelectedItem.Text;     
    行 115:       
    行 116:            da.= new SqlDataAdapter("select distinct(sheng) from 地名表", conn);
    行 117:            da.Fill(ds);
    行 118:            
     源文件: f:\text\4\ZcwWebApp\ZcwWebApp\Login.aspx.cs    行: 116 如何处理??
      

  5.   

    修改为:
    protected void SqLCon()
        {
            strConstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn = new SqlConnection();
            DataSet ds = new DataSet();
            try
            {  
                conn.ConnectionString = strConstring;
                conn.Open();
                
                da = new SqlDataAdapter("select ZhType from [帐户类别]", conn);
                da.Fill(ds,"aaa");            DropDownList32.Items.Clear();
                DropDownList32.DataTextField = "ZhType";
                DropDownList32.DataValueField = "ZhType";
                DropDownList32.DataSource = ds.Tables["bbb"];
                DropDownList32.DataBind();
                DropDownList32.SelectedValue = "";
                DropDownList32.SelectedIndex = 0;
                strTypeVal = DropDownList32.SelectedItem.Text;
     
                DropDownList33.Items.Clear();
                DropDownList33.DataTextField = "ZhType";
                DropDownList33.DataValueField = "ZhType";
                DropDownList33.DataSource = ds;
                DropDownList33.DataBind();
                DropDownList33.SelectedValue = "";
                DropDownList33.SelectedIndex = 0;
                strTypeVal = DropDownList33.SelectedItem.Text;     
           
                da.= new SqlDataAdapter("select distinct(sheng) from [地名表]", conn);
                da.Fill(ds,"bbb");
                
                DropDownList2.Items.Clear();
                DropDownList2.DataTextField = "sheng";
                DropDownList2.DataValueField = "sheng";
                DropDownList2.DataSource = ds.Tables["bbb"];
                DropDownList2.DataBind();
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
      

  6.   

    protected void SqLCon()
        {
            strConstring = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn = new SqlConnection();
            DataSet ds = new DataSet();
            try
            {  
                conn.ConnectionString = strConstring;
                conn.Open();
                
                da = new SqlDataAdapter("select ZhType from [帐户类别]", conn);
                da.Fill(ds,"aaa");            DropDownList32.Items.Clear();
                DropDownList32.DataTextField = "ZhType";
                DropDownList32.DataValueField = "ZhType";
                DropDownList32.DataSource = ds.Tables["bbb"];
                DropDownList32.DataBind();
                DropDownList32.SelectedValue = "";
                DropDownList32.SelectedIndex = 0;
                strTypeVal = DropDownList32.SelectedItem.Text;
     
                DropDownList33.Items.Clear();
                DropDownList33.DataTextField = "ZhType";
                DropDownList33.DataValueField = "ZhType";
                DropDownList33.DataSource = ds;
                DropDownList33.DataBind();
                DropDownList33.SelectedValue = "";
                DropDownList33.SelectedIndex = 0;
                strTypeVal = DropDownList33.SelectedItem.Text;     
           
                da.= new SqlDataAdapter("select distinct(sheng) from [地名表]", conn);
                da.Fill(ds,"bbb");
                
                DropDownList2.Items.Clear();
                DropDownList2.DataTextField = "sheng";
                DropDownList2.DataValueField = "sheng";
                DropDownList2.DataSource = ds.Tables["bbb"];
                DropDownList2.DataBind();
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
      

  7.   

    纠正:
      DropDownList32.DataSource = ds.Tables["bbb"];
    ===》
      DropDownList32.DataSource = ds.Tables["aaa"];
      

  8.   

     SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
                da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
                DataSet ds = new DataSet();
                da.Fill(ds, "city");            da = new SqlDataAdapter("select * from otherTable", conn);            da.Fill(ds, "otherTable");
      

  9.   

    using (SqlConnection conn = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Test"))
            {
               SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
                da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
                DataSet ds = new DataSet();
                da.Fill(ds, "city");            da = new SqlDataAdapter("select * from otherTable", conn);            da.Fill(ds, "otherTable");
           }