public class LiknBase
{
public static  SqlConnection myConnection = new  SqlConnection ("packet size=4096;user id=sa;initial catalog=HuaDong;p" +"ersist security info=True;password=123");
        

public static DataTable SelectDataBase(string tempStrSQL)
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter da = new SqlDataAdapter(tempStrSQL,myConnection);
da.Fill(ds);
}
catch(Exception ee)
{}
return ds.Tables[0];
} public static int UpdateDataBase(string tempStrSQL)
{
int intNumber=0;
try
{
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL,myConnection);
intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
}
catch(Exception ee)
{}
return intNumber; }
}string sql="select wordbook_monnect from wordbook where  wordbook_code>'110101' and wordbook_code<'331181' and Wordbook_TYPE='中华人民共和国行政区划'";
this.DropDownList5.DataSource=LiknBase.SelectDataBase(sql);
this.DropDownList5.DataBind();当我运行的时候 为什么 
我的DropDownList5 现在很多条记录但是都是 " System.Data.....DefaultView"下面方法可以实现我知道... 
DropDownList1.DataSource = DataSet1.Tables["Users"].DefaultView;  我现在想用字符串连接~ 所以怎么修改我方法能够实现了?

解决方案 »

  1.   

    public static DataTable GetDropDownListTable(string sql)
    {
    DataSet ds =  执行(sql);
    if (ds == null || ds.Tables[0].Rows.Count==0)
    return null;
    DataTable dt = ds.Tables[0];
    System.Type type = dt.Columns[0].DataType;
    DataRow dr = dt.NewRow();
    if (type.FullName.IndexOf("Int32")>0 || type.FullName.IndexOf("Byte")>0 || type.FullName.IndexOf("Int16")>0)
    dr[0]=0;
    else
    dr[0]="";
    dr[1]="请选择";
    dt.Rows.InsertAt(dr,0);
    return dt;
    }
    以上是通用方法。。自己可以改进this.DropDownList.DataSource = GetDropDownListTable(sql)
    DropDownList.DataBind();
      

  2.   

    DataSet ds = this.c_pro_class.ProductClass_select(); //查询数据库字段
    this.genreDropDownList.DataSource = ds;
    this.genreDropDownList.DataTextField = "PClass_Name";//你要显示的字段
    this.genreDropDownList.DataValueField = "PClass_ID";//该字段编号
    this.genreDropDownList.DataBind();