Asp.net对数据库操作

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.SqlClient;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.     }
  16.     protected void ShowButton_Click(object sender, EventArgs e) //显示所有用户
  17.     {
  18.         String sqlstr = "select * from users";
  19.         SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
  20.         SqlDataAdapter mysd = new SqlDataAdapter(sqlstr, con);
  21.         DataSet ds=new DataSet();
  22.         con.Open();
  23.         mysd.Fill(ds);
  24.         GridView1.DataSource=ds;
  25.         GridView1.DataBind();
  26.         con.Close();
  27.     }
  28.     protected void AddButton_Click(object sender, EventArgs e)  //增加一个用户
  29.     {
  30.         int _id;
  31.         String _name;
  32.         long _phone;
  33.         String _addr;
  34.         _id = Convert.ToInt32(IDTextBox.Text);
  35.         _name = NameTextBox.Text;
  36.         _phone = Convert.ToInt32(PhoneTextBox.Text);
  37.         _addr = AddrTextBox.Text;
  38.         String sqlstr = "insert into users values('"+_id+"','"+_name+"','"+_phone+"','"+_addr+"')";
  39.         SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
  40.         SqlCommand cmd = new SqlCommand(sqlstr, con);
  41.         con.Open();
  42.         cmd.ExecuteNonQuery();
  43.         con.Close();
  44.     }
  45.     protected void DelButton_Click(object sender, EventArgs e)  //删除一个用户
  46.     {
  47.         int _id;
  48.         _id = Convert.ToInt32(IDTextBox2.Text);
  49.         String sqlstr = "delete from users  where id='"+_id+"'";
  50.         SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
  51.         SqlCommand cmd = new SqlCommand(sqlstr, con);
  52.         con.Open();
  53.         cmd.ExecuteNonQuery();
  54.         con.Close();
  55.     }
  56.     protected void AlterButton_Click(object sender, EventArgs e)  //修改用户信息
  57.     {
  58.         int _id;
  59.         String _name;
  60.         long _phone;
  61.         String _addr;
  62.         _id = Convert.ToInt32(IDTextBox3.Text);
  63.         _name = NameTextBox2.Text;
  64.         _phone = Convert.ToInt32(PhoneTextBox2.Text);
  65.         _addr = AddrTextBox2.Text;
  66.         String sqlstr = "update users set name='"+_name+"', phone='"+_phone+"' , addr='"+_addr+"' where id='"+_id+"'  ";
  67.         SqlConnection con = new SqlConnection("Data Source=RAOXIANGUANG//SQLEXPRESS;Initial Catalog=123;User ID=test;Pwd=123");
  68.         SqlCommand cmd = new SqlCommand(sqlstr, con);
  69.         con.Open();
  70.         cmd.ExecuteNonQuery();
  71.         con.Close();
  72.     }
  73. }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值