- 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;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void ShowButton_Click(object sender, EventArgs e) //显示所有用户
- {
- String sqlstr = "select * from users";
- SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
- SqlDataAdapter mysd = new SqlDataAdapter(sqlstr, con);
- DataSet ds=new DataSet();
- con.Open();
- mysd.Fill(ds);
- GridView1.DataSource=ds;
- GridView1.DataBind();
- con.Close();
- }
- protected void AddButton_Click(object sender, EventArgs e) //增加一个用户
- {
- int _id;
- String _name;
- long _phone;
- String _addr;
- _id = Convert.ToInt32(IDTextBox.Text);
- _name = NameTextBox.Text;
- _phone = Convert.ToInt32(PhoneTextBox.Text);
- _addr = AddrTextBox.Text;
- String sqlstr = "insert into users values('"+_id+"','"+_name+"','"+_phone+"','"+_addr+"')";
- SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
- SqlCommand cmd = new SqlCommand(sqlstr, con);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- protected void DelButton_Click(object sender, EventArgs e) //删除一个用户
- {
- int _id;
- _id = Convert.ToInt32(IDTextBox2.Text);
- String sqlstr = "delete from users where id='"+_id+"'";
- SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
- SqlCommand cmd = new SqlCommand(sqlstr, con);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- protected void AlterButton_Click(object sender, EventArgs e) //修改用户信息
- {
- int _id;
- String _name;
- long _phone;
- String _addr;
- _id = Convert.ToInt32(IDTextBox3.Text);
- _name = NameTextBox2.Text;
- _phone = Convert.ToInt32(PhoneTextBox2.Text);
- _addr = AddrTextBox2.Text;
- String sqlstr = "update users set name='"+_name+"', phone='"+_phone+"' , addr='"+_addr+"' where id='"+_id+"' ";
- SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
- SqlCommand cmd = new SqlCommand(sqlstr, con);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
Asp.net对数据库操作
最新推荐文章于 2024-09-22 11:17:00 发布