代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=(local);Initial Catalog=database1;uid=sa;pwd=");
conn.Open();
SqlDataAdapter rdr = new SqlDataAdapter("select * from student", conn);
DataSet ds = new DataSet();
rdr.Fill(ds, "student");
GridView dg = new GridView();
dg.DataSource = ds.Tables["student"].DefaultView;
dg.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.ToString () == "")
{
TextBox1.Focus();
}
string str = @"insert into student (sno,sname,sex) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
SqlCommand cmd = new SqlCommand(str, conn);
DataSet ds = new DataSet();
GridView dg = new GridView();
cmd.ExecuteNonQuery();
SqlDataAdapter dsd = new SqlDataAdapter("select * from student", conn);
dsd.Fill(ds,"student");
dg.DataSource = ds.Tables["student"].DefaultView;
dg.DataBind();
}
}
我点击添加按钮时,把信息添加到了表中,却不能在gridview中显示出来,只能在重启后才能在gridview中看到.