C#与SQL Server数据库操作

代码:
1.引入名称空间:using System.Data.SqlClient;
2.定义连接字符串,连接对象,命令对象:
   

private String connectionstr;
private SqlConnection connection;
private SqlCommand command;


3.在构造函数中初始化连接字符串,连接对象,命令对象

   (1)初始化连接字符串:
    方式① connectionstr="server=localhost;uid=sa;pwd=123456;database=Demo";
    方式② connectionstr="server=127.0.0.1";Integrade Security=SSPI;database=Demo";
    其中,SIMS是我要连接的数据库名.(1)中的uid 和pwd是你登录数据库的登录名和密码
    注:这种连接是连接本地的数据库,若要连接局域网内其它机子上的数据库,可将方式①的"server=localhost;"改为"server=数据库所在机子的IP;"
   (2)初始化连接对象            
      connection = new SqlConnection(connectionstr);
   (3)初始化命令对象
      command =new SqlCommand();
      command .Connection =connection ;
4.操作数据库中的数据
   (1)查询数据库中的数据

            

             string snum=txtNum.Text;
             string str = "select * from Student where studentnum='" + snum + "'";
             command .CommandText =str;
             connection.Open();
             if (command.ExecuteScalar() == null)
             {
                 MessageBox.Show("您输入的学号对应的学生不存在!", "错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
           
             }
             else
             {
                 SqlDataAdapter sda = new SqlDataAdapter(str,connection );
                 DataSet ds = new DataSet();
                 sda.Fill(ds, "Student");
                 DataTable dt = ds.Tables["Student"];
                
                 GridView1.DataSource=dt;  
  
            	 GridView1.DataBind();  

             }
             connection.Close();

           
(2)向数据库中添加数据
           

             string snum = tBstudentnum.Text.Trim ();
             string sname = tBstudentname.Text.Trim();
             if (snum == "" || sname == "")
             {
                 MessageBox.Show("学生学号或姓名不能为空!", "错误", MessageBoxButtons.OK,
                                  MessageBoxIcon.Error);
             }
             else
             {
                 string insertstr="insert into Student values('"+snum +"','"+sname +"')";
                 command.CommandText = insertstr;
                 connection.Open();
                 command.ExecuteNonQuery();
                 MessageBox.Show("学生添加成功!", "提示", MessageBoxButtons.OK,
                     MessageBoxIcon.Information);
                 connection.Close();
             }


       
(3)修改数据库中的数据
      
            
             string snum = tBstudentnum.Text.Trim();
             string sname = tBstudentname.Text.Trim();
             if (snum == "" || sname == "")
             {
                 MessageBox.Show("学生学号或姓名不能为空!", "错误", MessageBoxButtons.OK,
                                  MessageBoxIcon.Error);
             }
             else
             {
                 string modifystr = "update Student set studentname='" + sname + 
                                    "' where studentnum='" + snum + "'";
                 command.CommandText = modifystr;
                 connection.Open();
                 command.ExecuteNonQuery();
                 MessageBox.Show("学生的姓名修改成功!", "提示", MessageBoxButtons.OK,
                                  MessageBoxIcon.Information );
                 connection.Close();
             }


   (4)删除数据库中的数据
       
           
             string snum = tBstudentnum.Text.Trim();
             if (snum == "")
             {
                 MessageBox.Show("学生学号不能为空!", "错误", MessageBoxButtons.OK,
                                   MessageBoxIcon.Error);
             }
             else
             {
                 string str = "select * from Student where studentnum='" + snum + "'"; 
                 string deletestr = "delete from Student where studentnum='" + snum + "'";
                 command.CommandText =str ;
                 connection.Open();
                 if (command.ExecuteScalar() == null)
                 {
                     MessageBox.Show("此学号对应的学生不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);                                    
                 }
                 else
                 {
                     command.CommandText = deletestr;
                     command.ExecuteNonQuery();
                      MessageBox.Show("学生的信息删除成功!", "提示", MessageBoxButtons.OK,
                                       MessageBoxIcon.Information);
                 }
                 connection.Close();

和asp一样

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值