在C#中使用控件DataGridView实现数据库增删改查
2013-04-01 14:13
573 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataSource
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private DataSet ds = new DataSet();
private SqlConnection conn = null;
private SqlDataAdapter da = null;
private const string DRIVER = "server=.;database=northwind;uid=sa;pwd=sa";
private const string sql_select = "select * from region";
/**//**
* 此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中
**/
private void Form1_Load(object sender, EventArgs e)
...{
conn = new SqlConnection(DRIVER);
da = new SqlDataAdapter(sql_select,conn);
da.Fill(ds,"table");
this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
}
private bool BtnInsert() //此方法作用于添加
...{
da.InsertCommand = conn.CreateCommand();
da.InsertCommand.CommandText = "insert into region values(@id,@ption)";
da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button1_Click(object sender, EventArgs e)
...{
if (this.BtnInsert())//调用此方法
...{
MessageBox.Show("添加成功!");
}
else
...{
MessageBox.Show("添加失败!");
}
}
private bool BtnDelect() //此方法作用于删除
...{
SqlParameter sp = new SqlParameter();
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText = "delete region where regionid=@id";
sp = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
sp.SourceVersion = DataRowVersion.Original;
ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button2_Click(object sender, EventArgs e)
...{
if (this.BtnDelect())//调用删除方法
...{
MessageBox.Show("删除成功!");
}
else
...{
MessageBox.Show("删除失败!");
}
}
private bool BtnUpdate() //此方法作用于修改
...{
SqlParameter sp = new SqlParameter();
da.UpdateCommand = conn.CreateCommand();
da.UpdateCommand.CommandText = "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";
da.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid");
da.UpdateCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription");
sp = da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4, "regionid");
sp.SourceVersion = DataRowVersion.Original;
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button3_Click(object sender, EventArgs e)
...{
if (this.BtnUpdate())//调用修改方法
...{
MessageBox.Show("修改成功!");
}
else
...{
MessageBox.Show("修改失败!");
}
}
}
}
相关文章推荐
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- C#中使用repeater控件实现List的clear,add,update,delete功能之二数据库
- 利用VS2010使用C#语言建立数据库Oracle_11g实现增删改查管理系统
- Visual C# 2008+SQL Server 2005 数据库与网络开发-- 13.3 使用公共对话框控件
- DBUtils学习之——使用ResultSetHandler接口的各个实现类实现数据库的增删改查
- C#中datagridview使用tooltip控件显示单元格内容的方法
- c#实现datagridview控件转datatable
- WinForm中DataGridView控件通过复选框实现多条记录的删除 c#2005
- 使用DataGridView进行增删改查,并同步到数据库
- 使用存储过程操作数据库(实现增删改查)
- Android下SQLite数据库学习笔记3——使用系统API实现数据库的增删改查
- c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单
- 使用sql标签来实现对数据库当中记录增删改查操作
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
- C#使用DataSet Datatable更新数据库的三种实现方法