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

26 篇文章 0 订阅

码码:

 

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的记录;

 

设置sql server 自增字段按下图设置列属性;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值