using System; using System.Data; 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.SqlClient;
//向GridView中加入新行方法,即通过此控件向数据库中加入新行.... public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GetData(); }
SqlConnection con = new SqlConnection("Server=.;Database=test;Integrated Security=SSPI"); SqlCommand cmd; SqlDataAdapter dap; SqlCommandBuilder cmb; protected void Button1_Click(object sender, EventArgs e) { con.Open(); cmd= new SqlCommand("select * from Users",con ); dap = new SqlDataAdapter(cmd); cmb = new SqlCommandBuilder(dap);//用此法向表中加入新行是最简单的吧... DataSet dst = new DataSet (); dap.Fill(dst, "Users"); DataTable dt = dst.Tables["Users"]; DataRow dro = dt.NewRow(); dro[0] = this.TextBox1.Text; dro[1] = this.TextBox2.Text; dro[2] = Convert.ToInt32(this.TextBox3.Text); dst.Tables[0].Rows.Add(dro); dap.Update(dst, "Users"); con.Close(); GetData(); }
public void GetData() { con.Open(); cmd = new SqlCommand("select *from Users",con ); DataSet dst = new DataSet(); dap = new SqlDataAdapter(cmd); dap.Fill(dst, "Users"); this.GridView1.DataSource = dst; GridView1.DataBind(); con.Close(); }