基本信息
源码名称:C#史上语句最少的:增删改查操作数据库(含数据库)
源码大小:0.22M
文件格式:.rar
开发语言:C#
更新时间:2020-01-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

只要几条语句轻松实现:增删改查操作数据库(含数据库)


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

namespace test
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private System.ComponentModel.Container components = null;
        private OleDbConnection oleConnection1 = null;
        private OleDbCommand oleCommand1 = null;

        public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			this.oleConnection1 = new OleDbConnection(test.database.dbConnection.connection);
			this.oleCommand1 = new OleDbCommand();
			this.oleCommand1.Connection = this.oleConnection1;

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}



        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“hyckDataSet.Item”中。您可以根据需要移动或删除它。
            LoadData();
            TxtNo.Focus();
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.YellowGreen;
            

        }
        private void LoadData()
           
    {
             string sql = "select ItemNo AS 料号,ItemName AS 名称,ItemDesc AS 规格,QTY AS 数量,Unit AS 单位 from Item where 1=1 "; ;


             if (TxtNo.Text != "" )
             {
                 sql = sql   " AND ItemNo like '%"   TxtNo.Text   "%'";
             }

             if (TxtName.Text != "")
             {
                 sql = sql   " AND ItemName like '%"   TxtName.Text   "%'";
             }

             if (TxtDesc.Text != "")
             {
                 sql = sql   " AND ItemDesc like '%"   TxtDesc.Text   "%'";
             }

             if (TxtQty.Text != "")
             {
                 sql = sql   " AND QTY="   TxtQty.Text;
             }

             if (CbUnit.Text != "")
             {
                 sql = sql   " AND Unit='"   CbUnit.Text "'";
             }
                oleConnection1.Open();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleConnection1);
            DataSet ds = new DataSet();
            ds.Clear();
            adp.Fill(ds, "Item");
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            oleConnection1.Close();
            ClearText();
            TxtNo.Focus();
    }

        private void button1_Click(object sender, EventArgs e)
        {
  
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateData();
        }

        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                this.Close();
            }
            


        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            dataGridView1.CancelEdit();
            e.Cancel = true;
        }
       


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)

        {
           

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                this.Close();
            }
        }

        private void UpdateData()
    {
            string sql=null;
            if (dataGridView1.CurrentCell.ColumnIndex == 1)
                sql = "update Item set ItemName='"   dataGridView1.CurrentCell.Value.ToString()   "' where ItemNo='"   dataGridView1.CurrentRow.Cells[0].Value "'";
            if (dataGridView1.CurrentCell.ColumnIndex == 2)
                sql = "update Item set ItemDesc='"   dataGridView1.CurrentCell.Value.ToString()   "' where ItemNo='"   dataGridView1.CurrentRow.Cells[0].Value   "'";
            if (dataGridView1.CurrentCell.ColumnIndex == 3)
                sql = "update Item set QTY="   dataGridView1.CurrentCell.Value.ToString()   " where ItemNo='"   dataGridView1.CurrentRow.Cells[0].Value   "'";
            if (dataGridView1.CurrentCell.ColumnIndex == 4)
                sql = "update Item set Unit='"   dataGridView1.CurrentCell.Value.ToString()   "' where ItemNo='"   dataGridView1.CurrentRow.Cells[0].Value "'";
            if ( sql==null )
                return;
            try
            {
                oleConnection1.Open();
                oleCommand1.CommandText = sql;
                oleCommand1.ExecuteNonQuery();
                oleConnection1.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出现错误");
            }

        }
        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
        
        }

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
   
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除这行吗?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                return;
                
			if (dataGridView1.CurrentRow.Index >=0 && dataGridView1.DataSource != null )
			{
                string sql = "delete * from Item where ItemNo='"   dataGridView1.CurrentRow.Cells[0].Value   "'";
				oleConnection1.Open();
				oleCommand1.CommandText = sql;
				oleCommand1.ExecuteNonQuery();
				oleConnection1.Close();
                dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
			}
			else
            {
				MessageBox.Show("没有指定物料信息!","提示");
		

            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            TxtQty.Text = "0";
            try
            {

                OleDbConnection oleDb = new OleDbConnection(test.database.dbConnection.connection);
                oleDb.Open();
                string sql;
                if (TxtQty.Text == "")
                    TxtQty.Text = "0";
                if (TxtDesc.Text=="")
                {
                  sql = "insert into Item (ItemNo,ItemName,ItemDesc,QTY,Unit) values ('"   TxtNo.Text   "','"   TxtName.Text   "',null,"   TxtQty.Text   ",'"   CbUnit.Text   "')";
                }
                else
                {
                  sql = "insert into Item (ItemNo,ItemName,ItemDesc,QTY,Unit) values ('"   TxtNo.Text   "','"   TxtName.Text   "','"   TxtDesc.Text   "',"   TxtQty.Text   ",'"   CbUnit.Text   "')";
                }
                OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDb);
                oleDbCommand.ExecuteNonQuery(); //返回被修改的数目
                oleDb.Close();

                MessageBox.Show("添加成功!");
                ClearText();
                dataGridView1.DataSource = null;
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出现错误");
            }

        }

        private void ClearText()
    {
                TxtNo.Text  = "";
                TxtName.Text  = "";
                TxtDesc.Text  = "";
                TxtQty.Text = "";
                CbUnit.SelectedIndex = 0;
                TxtNo.Focus();

    }
        private void BtnEdit_Click(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
            



        }

        private void TxtQty_Enter(object sender, EventArgs e)
        {
           
        }

        private void TxtQty_Click(object sender, EventArgs e)
        {
            TxtQty.SelectAll();
        }

        private void TxtQty_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    BtnAdd_Click(this, EventArgs.Empty);
                    break;

                case Keys.F2:
                    BtnDel_Click(this, EventArgs.Empty);
                    break;
                case Keys.F3:
                    BtnEdit_Click(this, EventArgs.Empty);
                    break;
                case Keys.F4:
                    BtnQry_Click(this, EventArgs.Empty);
                    break;
            }
        }

        private void TxtQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && e.KeyChar != '.' && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            } 
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView1.ClearSelection();
        }

        private void BtnQry_Click(object sender, EventArgs e)
        {
            LoadData();
        }

   
    }
}