日期:2014-05-18  浏览次数:20432 次

怎么把已有数据库绑定到gridview中,最好给出代码
如题

------解决方案--------------------
string connStr =ConfigurationSettings.AppSettings[ "ConnString1 "].ToString();
Session[ "constr "] = connStr;
string SqlStr = "SELECT * FROM 表 ";
DataSet ds = new DataSet();

try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed ") conn.Open();

SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01 ");
if (conn.State.ToString() == "Open ") conn.Close();

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

}
catch (Exception ex)
{
//throw ex;
Response.Write( "数据库错误,错误原因: " + ex.Message);
Response.End();
}
------解决方案--------------------
DataTable dt=..//你自己获取数据的方法,也可以是dataset等
gridview1.DataSource=dt;
gridview.DataBind();
------解决方案--------------------
string str1= "select * from table ";
try
{
using(con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "DSN "]))
{

DataSet ds = new DataSet();
adapter = new SqlDataAdapter(str1,con);
adapter.SelectCommand.Parameters.Add( "@Id ",Request.QueryString[ "Id "].ToString());
adapter.Fill(ds);
DataColumn dc = new DataColumn( "Des ",System.Type.GetType( "System.String "));
this.gridview1.DataSource = ds.Tables[0].DefaultView;
this.gridview1.DataBind();
}
}
catch
{}