阅读背景:

C#访问SQLServer增删改查代码实例_bcbobo21cn的专栏

来源:互联网 

码码:

 

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);
            }
        }
    }
}using System;
using System.Collection



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: