datagrid的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 10:30:05
【写了一个datagrid的小程序,希望可以显示数据表中的部分列,一下为后台代码,希望高手看一下哪里出错了】
public partial class TestDG : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
bind();
}

private void bind()
{
SqlConnection sqlcon;
string strCon = "Server=(local);Database=db_FM;Uid=wjx2;Pwd=1234";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter("select top(6) file_title,file_author,file_keywords,file_updatedtime from tb_File", sqlcon);
DataSet ds = new DataSet();
sqlcon.Open();
myda.Fill(ds, "file");
this.DataGrid1.DataSource = ds.Tables["file"];
this.DataGrid1.DataBind();
sqlcon.Close();
}
}

【显示页面代码如下】
<form id="form1" runat="server" method="post">
<div>

避免反复刷新:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
获取记录集前必须打开数据库连接
把sqlcon.Open();移到定义SqlDataAdapter的前面.
SQL语句把6的括号去掉
select top 6 file_title,file_author,file_keywords,file_updatedtime from tb_File

SQL语法写错了吧
top 6 这样就可以吧
不用加()
应该是 你试一下

sqlcon.Open(); 这个不用写