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(
"
修改失败!
"
);
}
}
}
}
查看全文
相关阅读:
HTML5小时钟
简单画板
li样式不显示使用overflow:hidden导致Li前面点、圈等样式不见
Dede 列表页 缩略图 有显示无则不显示
CSS3的position:sticky介绍
json 包含字段及函数的写法
PHP+Ajax 异步通讯注册验证
find命令结合cp bash mv 命令使用的4种方式
markdown完整语法规范3.0+编辑工具介绍
几款 ping tcping 工具总结
原文地址:https://www.cnblogs.com/da6wei6/p/1277309.html
最新文章
Intellij IDEA中部署Tomcat报错“war exploded: Server is not connected. Deploy is not available”
Tomcat卸载
JavaWeb学习-Tomcat
Tomcat的测试页打开空白页的解决方法
Intellij IDEA中配置Maven
【转】Intellij IDEA快捷键
maven 基础
eclipse常用快捷键
Eclipse调试Java的10个技巧
eclipse注释模板
热门文章
eclipse 如何引入本地dtd
eclipse常用设置
spring与mybatis三种整合方法
三、java三大特性--多态
二、java三大特性--继承
一、java三大特性--封装
软件工程及各阶段描述
软件工程的6个阶段
炫酷万花筒
远程连接Ucenter数据库
Copyright © 2011-2022 走看看