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.SqlClient;
using DataAcessTest;
namespace DBAcessTest
{
public partial class Form1 : Form
{
//数据库连接信息
String connectionString = "server=.;database=FF3DB;uid=sa;pwd=123456";
String selectStr = "select * from ";
string selectTableStr = "select name from sysobjects where type='U'";
String curTableStr = "";
String columnNameStr = null;
private DataGridView dataGridView1 = new MyDataGridView();
//添加checkbox
DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
//查询按钮
private Button selectButton = new Button();
//重载按钮
private Button reloadButton = new Button();
//提交按钮
private Button submitButton = new Button();
//删除按钮
private Button mDeleteButton = new Button();
//查询关键字符
private TextBox keyTextBox = new TextBox();
//添加按钮
private Button addButton = new Button();
//数据改变标志
private Boolean isChanged = false;
//数据表选择Box
private ComboBox tableCombobox = new ComboBox();
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;
this.Text = "对FF3DB数据库的增删改查";
reloadButton.Text = "载入";
submitButton.Text = "提交";
selectButton.Text = "查询";
keyTextBox.Text = "请输入查询关键字";
mDeleteButton.Text = "删除";
addButton.Text = "添加";
//注册监听事件
reloadButton.Click += new System.EventHandler(reloadButton_Click);
submitButton.Click += new System.EventHandler(submitButton_Click);
selectButton.Click += new System.EventHandler(selectButton_Click);
mDeleteButton.Click += new System.EventHandler(mDeleteButton_Click);
keyTextBox.MouseClick += new MouseEventHandler(keyTextBox_MouseClick);
addButton.Click += new System.EventHandler(addButton_Click);
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
FlowLayoutPanel panel = new FlowLayoutPanel();
//设置panel在顶部
panel.Dock = DockStyle.Top;
panel.AutoSize = true;
//panel中添加控件
panel.Controls.AddRange(new Control[] { tableCombobox,reloadButton, submitButton, addButton, mDeleteButton, selectButton, keyTextBox });
//添加布局文件
this.Controls.AddRange(new Control[] { dataGridView1, panel });
this.Load += new System.EventHandler(Form1_Load);
}
#region 加载表中数据
/// <summary>
/// 加载表中数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, System.EventArgs e)
{
//设置DataGridView columns长度 适合字段头
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
//设置前景色背景色
dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
//设置不可添加行
dataGridView1.AllowUserToAddRows = false;
//每行头添加checkBox
dataGridView1.Columns.Insert(0, newColumn);
//将 DataGridView 控件绑定到BindingSource组件,并调用 ExuSQL 方法从数据库中检索数据
dataGridView1.DataSource = bindingSource1;
//执行查询数据库表方法
InsertCombox();
}
#endregion
#region 载入数据
/// <summary>
/// 重新加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void reloadButton_Click(object sender, System.EventArgs e)
{
string tableNameStr = tableCombobox.Text.ToString();
try
{
ExuSQL(selectStr + tableNameStr);
curTableStr = tableNameStr.Trim();
MessageBox.Show("载入成功!");
}
catch
{
MessageBox.Show("无相关表~~~!");
}
}
#endregion
#region 添加数据
/// <summary>
/// 添加数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addButton_Click(object sender, System.EventArgs e)
{
//可添加行设置为true
dataGridView1.AllowUserToAddRows = true;
}
#endregion
#region 删除
/// <summary>
/// 批量删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mDeleteButton_Click(object sender, System.EventArgs e)
{
string mDeleteStr = "(";
//删除数据条数计数
int count = 0;
//遍历数据获取checkbox为ture的行的ListCd值
for (int i = 0; i < Convert.ToInt32(dataGridView1.RowCount); i++)
{
if (Convert.ToBoolean(dataGridView1[0, i].Value))
{
mDeleteStr += dataGridView1[1, i].Value.ToString().Trim() + ",";
count++;
}
}
if (count > 0)
{
mDeleteStr = mDeleteStr.Substring(0, mDeleteStr.Length - 1) + ")";
mDeleteStr = "delete from " + curTableStr + " where ListCd in " + mDeleteStr;
ExuSQL(mDeleteStr);
MessageBox.Show("已删除数据:" + count + "条");
ExuSQL(selectStr + curTableStr);
}
else
{
MessageBox.Show("未选中任何数据!");
}
}
#endregion
#region 提交修改
/// <summary>
/// 提交修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void submitButton_Click(object sender, System.EventArgs e)
{
dataAdapter.Update((DataTable)bindingSource1.DataSource);
if (isChanged)
{
MessageBox.Show("恭喜你提交成功!");
isChanged = false;
}
else
{
MessageBox.Show("数据木有改变!!");
}
dataGridView1.AllowUserToAddRows = false;
}
#endregion
#region 查询数据
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void selectButton_Click(object sender, System.EventArgs e)
{
string keyword = keyTextBox.Text.ToString();
string delkeyword = keyTextBox.Text.ToString();
if (keyword.Equals("") || keyword.Equals("请输入查询关键字"))
{
MessageBox.Show("不能为空!");
return;
}
//模糊查找语句
String seletcString = "select * from " + curTableStr + " where " + columnNameStr + " like " + "'" + "%" + keyword.Trim() + "%" + "'";
ExuSQL(seletcString);
//判断查询有无相关结
if (bindingSource1.Count < 1)
{
MessageBox.Show("无相关字段!");
ExuSQL(selectStr + curTableStr);
}
else
{
MessageBox.Show("查询完毕!");
}
}
#endregion
#region 获取数据库数据
/// <summary>
/// 数据库获取数据
/// </summary>
/// <param name="selectCommand"></param>
private void ExuSQL(string selectCommand)
{
try
{
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
//创建command bulider 来确保 数据库 更新 插入 删除 命令
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
//创建一个DataTable 并绑定bindingSource
DataTable table = new DataTable();
//数据culture设置
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
columnNameStr = table.Columns[1].ColumnName;
}
catch (SqlException)
{
//捕获异常抛给上一层
throw (new System.Exception());
}
}
#endregion
#region 绑定数据库表名
/// <summary>
/// 绑定数据库表名
/// </summary>
private void InsertCombox()
{
DataTable table = new DataTable();
dataAdapter = new SqlDataAdapter(selectTableStr,connectionString);
dataAdapter.Fill(table);
tableCombobox.DataSource = table;
//设置combpbox显示数据name
tableCombobox.DisplayMember = "name";
}
#endregion
#region 清空textbox提示语
/// <summary>
/// 清空textbox提示语
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void keyTextBox_MouseClick(Object sender, MouseEventArgs e)
{
keyTextBox.Text = "";
}
#endregion
#region 监听数据是否改变
/// <summary>
/// 监听数据是否改变
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//数据改变标志至为true
isChanged = true;
}
#endregion
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。