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(
"
修改失败!
"
);
}
}
}
}
查看全文
相关阅读:
Linux下的vim简易配置与Windows下的vim配置
ASP.NET MVC2(Visual Studio.NET 2010)学习之路(一)
NonSQL
完成公司核名
调试iOS 已经发布代码 Crash 文件分析出出错对应代码
iOS RSA公钥加密数据 服务端接受PHP私钥解密 反过服务端公钥加密数据 iOS端私钥解密数据
iOS 日期格式串 setDateFormat 显示格式代码
iOS5 UI 设计新手段 Storyboard
UIEdgeInsets
xcode调试找出错误行
原文地址:https://www.cnblogs.com/da6wei6/p/1277309.html
最新文章
[逆向][Writeup]EIS2016 chkflag .NET程序逆向
[逆向][Writeup]ISG2015 flagfinder .NET程序逆向
常用安全工具汇总
Linux系统调用号
C语言学习笔记(一) 开发环境的搭建
rem、em、px的区别
github上传项目(使用git)、删除项目、添加协作者
react入门(1)
批处理(Batch)控制台显示彩色字体FindStr实现
合金弹头 逆向分析与外挂制作报告【内联HOOK】
热门文章
iOS开发中判断空字符串的几种方法
一套稍微难点的iOS面试题
UITextView中return key点击事件的监听方法
一套稍微难点的iOS面试题 第1题参考答案
一套稍微难点的iOS面试题 第2~12题参考答案
ios开发中在当前窗口上加载视图的方法
一套稍微难点的iOS面试题 第13题答案
一套稍微难点的iOS面试题 第14题参考答案
iOS开发之应用首次启动显示用户引导
NSString的常用方法
Copyright © 2011-2022 走看看