代码如下,错在哪?
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=8313796;Initial Catalog=testnet");
protected void Page_Load(object sender, EventArgs e)
{
conn.Open();//打开数据库链接
if (!Page.IsPostBack)
{
BindGrid();
}
//conn.Close();
}
ICollection CreateTable() //查询
{
String SQL = "select * from student";
SqlCommand comm = new SqlCommand(SQL, conn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
DataSet ds = new DataSet();
da.Fill(ds, "student");
return ds.Tables["student"].DefaultView;
}
protected void UpdateTable() //修改
{
String SQL1 = "update student set studentname='Kate' where id=1";
SqlCommand comm1 = new SqlCommand(SQL1, conn);
comm1.ExecuteNonQuery();
BindGrid();
}
protected void DelTable() //删除
{
String SQL2 = "Delete from student where id=6";
SqlCommand comm2 = new SqlCommand(SQL2, conn);
comm2.ExecuteNonQuery();
BindGrid();
}
public void BindGrid() //显示
{
GridView1.DataSource =CreateTable() ;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//DelTable();
//UpdateTable();
//Server.Transfer("Default_add.aspx", true);
}