C#访问SQLServer增删改查代码实例


一个专门实现访问sql server数据库增删改查的操作代码,分享给大家,具体内容如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    //查询
    private void button1_Click(object sender, EventArgs e)
    {
      string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no";//定义数据库连接参数
      SqlConnection MyConnection = new SqlConnection(MyConn);//定义一个数据连接实例
      SqlCommand MyCommand = new SqlCommand("SELECT * FROM 图书借阅", MyConnection); //定义一个数据库操作指令
      SqlDataAdapter SelectAdapter = new SqlDataAdapter();//定义一个数据适配器
      SelectAdapter.SelectCommand = MyCommand;//定义数据适配器的操作指令
      DataSet MyDataSet = new DataSet();//定义一个数据集
      MyConnection.Open();//打开数据库连接
      SelectAdapter.SelectCommand.ExecuteNonQuery();//执行数据库查询指令
      MyConnection.Close();//关闭数据库
      SelectAdapter.Fill(MyDataSet);//填充数据集
      DataGrid1.DataSource = MyDataSet.Tables[0];
      //DataGrid1.DataBind();//将数据表格用数据集中的数据填充
    }

    //添加
    private void button2_Click(object sender, EventArgs e)
    {
      string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no";
      SqlConnection MyConnection = new SqlConnection(MyConn);
      string MyInsert = "insert into 图书借阅 (图书编号,读者编号,续借次数) values ('" + Convert.ToString(textBox2.Text) + "','" +
        Convert.ToString(textBox3.Text)+ "','"+Convert.ToInt32(textBox4.Text)+ "')";
      SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
      try//异常处理
      {
        MyConnection.Open();
        MyCommand.ExecuteNonQuery();
        MyConnection.Close();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    //更新
    private void button3_Click(object sender, EventArgs e)
    {
      string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no";
      SqlConnection MyConnection = new SqlConnection(MyConn);
      string MyUpdate = "Update 图书借阅 set 操作员='" + textBox2.Text + "'" + " where 借阅编号=" + "'" + textBox1.Text + "'";
      SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
      try
      {
        MyConnection.Open();
        MyCommand.ExecuteNonQuery();
        MyConnection.Close();
        textBox1.Text = "";
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

    //删除
    private void button4_Click(object sender, EventArgs e)
    {
      string MyConn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;Trusted_Connection=no";
      SqlConnection MyConnection = new SqlConnection(MyConn);
      string MyDelete = "Delete from 图书借阅 where 借阅编号=" + textBox1.Text;
      SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
      try
      {
        MyConnection.Open();
        MyCommand.ExecuteNonQuery();
        MyConnection.Close();
        textBox1.Text = "";
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }
  }
}

 数据库如下;


winform中查询成功;


插入时,因为借阅编号为自增,不能插入值,会自己生成;



更新,外键冲突;插入的图书编号为000999,无此图书,故出错;


插入成功;


更新操作员为"王老师";


删除借阅编号为31的记录;


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持phpstudy。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3