zoukankan
html css js c++ java
在C#中使用控件DataGridView实现数据库增删改查
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(
"
修改失败!
"
);
}
}
}
}
查看全文
相关阅读:
css hack 【转】http://blog.csdn.net/arcow/article/details/1681027
插入错误: 列名或所提供值的数目与表定义不匹配。
XCopy 过程加日志
textindent br
asp.net 防止重复提交
穷在闹市无人问,富在深山有远亲
关于SqlDataReader遍历和缓存结果集
在AJAX中使用 JS
Application、Session和Cookie 的区别 总结
C#中抽象类和接口的区别与使用
原文地址:https://www.cnblogs.com/da6wei6/p/1277309.html
最新文章
Win7下卸载Oracle11g
jQuery EasyUI API 中文文档 布局(Layout)
Windows7硬盘安装Fedora16图文教程
方便Office文件编辑的利器 : Office Tab (MS_Office插件)
jQuery EasyUI API 中文文档 组合框(ComboBox)
jQuery EasyUI API 中文文档 DataGrid数据表格
项目文件①——删除不必要的组件,只剩下app.vue组件②——如何新增页面③router.js掌管所有页面信息“/home”
Element UI使用;字体图标icon;数据绑定;$$$表单验证rules prop;重置登录前的预验证(表单验证的总和验证)
后台项目初始化②配置node、npm环境;postman测试接口
VSCode按tab键默认是4个空格键,如下设置,可将其调整为2个空格键。”
热门文章
前端项目初始化③项目托管到gitee中
前端项目初始化②插件、依赖
后台项目初始化①phpstudy导入数据库
发起请求——axios包,校验登录用户是否存在
8分类乳腺癌数据
前端项目初始化①vue ui
查询数据库性能的语句
sqlserver 数据库订阅报错 22202 14058
莫名其妙的生成出一个dll,然后还报未能加载文件或程序集
存储过程结果进行查询 select 存过过程
Copyright © 2011-2022 走看看