在C#中使用控件DataGridView实现数据库增删改查
2011-01-30 16:49
771 查看
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";
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
来源:(http://blog.sina.com.cn/s/blog_665a1a1d0100hxap.html) - 在C#中使用控件DataGridView实现数据库增删改查_libraliu_新浪博客
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("修改失败!");
}
}
}
}
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";
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
来源:(http://blog.sina.com.cn/s/blog_665a1a1d0100hxap.html) - 在C#中使用控件DataGridView实现数据库增删改查_libraliu_新浪博客
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#中使用控件DataGridView实现数据库增删改查
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- 在C#中使用控件DataGridView实现数据库增删改查
- 利用VS2010使用C#语言建立数据库Oracle_11g实现增删改查管理系统
- C#中使用repeater控件实现List的clear,add,update,delete功能之二数据库
- [C#] 检索数据库并用DataGridView 控件显示数据.
- 在C#中使用Panel控件实现在一个窗体中嵌套另一个窗体
- C#使用方向键实现控件的移位
- C#如何使用dataGridView控件显示类对象数据
- 使用EF实现数据库的增删改查
- Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
- DataGridView控件中加入ComboBox下拉列表框的实现(C#)
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- C# 使用SerialPort控件用类及线程实现串口通信
- SQL server 2008R2 中 C#Winfoirm 使用 SqlDependency 机制实现 数据库中某一张表的监视
- 使用C#反射实现用户控件调用父页面方法
- vs C#控件使用:[2]DataGridView控件(二)