目录
1 控件准备
1.1 Mdi窗体空间及设置
1.2 数据库处理子窗体
2 实现代码
2.1 名称空间引用
2.2 全局变量定义
2.3 在窗体Load事件中添加代码显示数据
2.4 数据库更新代码
2.5 删除DataGridView数据记录代码
2.6 更新数据库代码
2.7 输出到Excel代码
3 界面展示
3.1 原始数据库及数据载入
3.2 删除和增加记录
3.3 数据库更新
3.4 导出到Excel
4 总结及疑问
4.1 成果总结:
4.2 仍存在的问题:
创建winform窗体,并将其IsMdiContainer属性设置为true
(1)RibbonControl控件
Navigation & Layout下添加RibbonControl控件
(2)按钮添加
添加按钮barbtn_database,Caption = “数据库处理”;
添加名称为dataBase的窗体
添加dataGridView控件,属性设置如下:
属性 | 赋值 |
AutoSize | True |
Anchor | Top, Bottom, Left, Right |
AllowUserToAddRows | true |
AllowUserToDeleteRows | true |
添加panel控件,设置Anchor属性为Top, Bottom, Left, Right
在panel控件上拖拽四个button控件,
Name | btn_deleteDataItem | btn_addDataItem | btn_updataToDatabase | btn_ouputToExcel | btn_refreshDataID |
Text | 删除数据记录 | 增加数据记录 | 更新数据到数据库 | 输出数据到excel | 刷新数据ID |
AutoSize | true | true | true | true | true |
Anchor | Right | Right | Right | Right | Right |
添加名称空间引用:using System.Data.OleDb; //连接Access数据库使用
添加名称空间引用:using System.Threading; //导出到excel使用
添加名称空间引用:using Excel = Microsoft.Office.Interop.Excel;
数据库链接字符串全局变量:
string connectStr= @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=你的数据库mdb文件路径\Database1.mdb";
存放数据的中介变量
System.Data.DataTable dt = new System.Data.DataTable(); //注:此处与Excel的名称空间冲突,故使用全名
//加载数据库
private void dataBase_Load(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(connectStr);
connection.Open();
OleDbCommand cmd = connection.CreateCommand();
cmd.CommandText = "select * from fruit";
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
connection.Close();
cmd.Dispose();
connection.Dispose();
}
//更新数据到数据库中
private void btn_updataToDatabase_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(connectStr);
connection.Open();
OleDbCommand cmd = connection.CreateCommand();
cmd.CommandText = "select * from fruit";
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cmdBulid = new OleDbCommandBuilder(adapter);
adapter.DeleteCommand = cmdBulid.GetDeleteCommand();
adapter.InsertCommand = cmdBulid.GetInsertCommand();
adapter.UpdateCommand = cmdBulid.GetUpdateCommand();
adapter.Update((System.Data.DataTable)dataGridView1.DataSource);
adapter.Dispose();
cmdBulid.Dispose();
cmd.Dispose();
connection.Dispose();
}
//删除dataGridView中的记录
private void btn_deleteDataItem_Click(object sender, EventArgs e)
{
int cnt = dataGridView1.SelectedRows.Count;
for (int i = 0; i < cnt; i++)
{
DataRowView drv = dataGridView1.SelectedRows[i].DataBoundItem as DataRowView;
drv.Delete();
}
}
//更新数据到数据库中
private void btn_updataToDatabase_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(connectStr);
connection.Open();
OleDbCommand cmd = connection.CreateCommand();
cmd.CommandText = "select * from fruit";
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cmdBulid = new OleDbCommandBuilder(adapter);
adapter.DeleteCommand = cmdBulid.GetDeleteCommand();
adapter.InsertCommand = cmdBulid.GetInsertCommand();
adapter.UpdateCommand = cmdBulid.GetUpdateCommand();
adapter.Update((System.Data.DataTable)dataGridView1.DataSource);
adapter.Dispose();
cmdBulid.Dispose();
cmd.Dispose();
connection.Dispose();
}
//导出数据到Excel表格中
private void btn_ouputToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "*.xls|*.xls";
string outputStr;
if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Excel._Application myExcel;
ThreadPool.QueueUserWorkItem(
(pp) => {
myExcel = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook p_wk = myExcel.Workbooks.Add();
Excel.Worksheet p_ws = (Excel.Worksheet)p_wk.Worksheets.Add();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
p_ws.Cells[i + 1, 1] = dataGridView1.Rows[i].Cells[0].Value.ToString();
p_ws.Cells[i + 1, 2] = dataGridView1.Rows[i].Cells[1].Value.ToString();
p_ws.Cells[i + 1, 3] = dataGridView1.Rows[i].Cells[2].Value.ToString();
}
p_wk.SaveAs(saveDlg.FileName);
((Excel._Application)myExcel.Application).Quit();
this.Invoke((MethodInvoker)(() => { MessageBox.Show("导出到Excel成功", "提示信息"); }));
} );
}
outputStr = saveDlg.FileName;
saveDlg.Dispose();
}
系统练习了使用C#进行数据库更新、删除和修改操作,并将dataGridView中的数据导出到Excel表格中;
(1)数据库采用自动编号,我手动输入的ID与更新后的数据库ID不一致,有待深入研究改善;
(2)刷新ID仅仅是在DataGridView控件中进行的,并未实际更新到数据库中