实例介绍
【实例简介】
只要几条语句轻松实现:增删改查操作数据库(含数据库)
【实例截图】
【核心代码】
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();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论