C#2005中使用控件DataGridView实现对数据库增删改查操作
来源:互联网 发布:linux cp出某个文件 编辑:程序博客网 时间:2024/06/07 15:54
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(id,ption) 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 para =new SqlParameter();
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText ="delete from region where regionid=@id";
para = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4,"regionid");
para .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 para =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");
para = da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4,"regionid");
para .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";
/**//**
* 此方法为将数据库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(id,ption) 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 para =new SqlParameter();
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText ="delete from region where regionid=@id";
para = da.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4,"regionid");
para .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 para =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");
para = da.UpdateCommand.Parameters.Add("@oldid", SqlDbType.Int, 4,"regionid");
para .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("修改失败!");
}
}
}
}
0 0
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- 在C#中使用控件DataGridView实现数据库增删改查
- vs中C#语言datagridview控件与sqlserver数据库数据增删改操作实现
- 在DataGridView中实现增删改查
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
- PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty)
- 使用C#对XML进行增删改查操作
- 使用C#对XML进行增删改查操作 收藏
- c# datagridview 增删查改
- 使用MyEclipse对MongoDB数据库 进行增删改查操作
- 通过jdbc实现对数据库中数据的增删查改操作
- Java语言实现对MySql数据库中数据的增删改查操作
- 使用LINQ实现对数据库单张表的基础增删改查操作
- fireFox插件Selenium IDE
- Android如何获取Wifi名称即SSID
- Cscope的使用(领略Vim + Cscope的强大魅力)
- memcached简介及java使用方法
- 解释在维基百科
- C#2005中使用控件DataGridView实现对数据库增删改查操作
- Linux 多核编程
- node.js基础主要参考阿里巴巴国际站前端技术部推出的文档
- Monitor Oracle Resource Consumption in UNIX (文档 ID 148466.1)
- 解决extjs4.0 grid不能编辑表格问题
- java 理论与实践:正确使用Volatile
- __attribute__ ((format (printf, 2, 3))); 疑惑
- tnsnames.ora 是什么
- Thanks for Andreas and his TexturePacker license