VS2008C#Sqlserver2008数据库的连接,增删改查
来源:互联网 发布:手机淘宝 降价提醒 编辑:程序博客网 时间:2024/05/29 20:02
using System.Data.SqlClient;
1
SqlConnection conn;
1
2
3
4
5
6
7
8
//连接数据库
private void Form1_Load(object sender, EventArgs e)
{
string constr =
"server=ACER-PC\\LI;database=db_test;uid=sa;pwd=123"
;
conn = new SqlConnection(constr); //数据库连接
}
//查看数据库信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//这里只要连接数据库即可,不必打开数据库
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand(
"select * from tb_ls"
, conn);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds,
"cs"
);
dataGridView1.DataSource = ds.Tables[0];
}
//删除选中的一行信息
private void button2_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.
Count
> 0)
{
DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem
as
DataRowView;
drv.
Delete
();
}
conn.
Open
();//打开数据库
SqlCommand cmd = new SqlCommand(
"delete from tb_ls where 编号="
+this.dataGridView1.CurrentRow.Cells[
"编号"
].Value+
""
,conn);
cmd.ExecuteNonQuery();
conn.
Close
();//关闭数据库
}
//添加信息
private void button3_Click(object sender, EventArgs e)
{
conn.
Open
();
SqlCommand cmd = new SqlCommand(
"insert into tb_ls values('"
+textBox1.Text+
"','"
+textBox2.Text+
"','"
+textBox3.Text+
"','"
+textBox4.Text+
"')"
,conn);
cmd.ExecuteNonQuery();
conn.
Close
();
}
private void button4_Click(object sender, EventArgs e)
{
conn.
Open
();
SqlCommand cmd = new SqlCommand(
"update tb_ls set 姓名='"
+textBox2.Text+
"',性别='"
+textBox3.Text+
"',年龄='"
+textBox4.Text+
"' where 编号='"
+textBox1.Text+
"'"
,conn);
textBox1.ReadOnly =
false
;
cmd.ExecuteNonQuery();
conn.
Close
();
}
1
2
3
4
5
6
7
8
9
//将选中的某一行的信息显示在TextBox文本框中
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.ReadOnly =
true
;
textBox1.Text = this.dataGridView1.CurrentRow.Cells[
"编号"
].Value.ToString();
textBox2.Text = this.dataGridView1.CurrentRow.Cells[
"姓名"
].Value.ToString();
textBox3.Text = this.dataGridView1.CurrentRow.Cells[
"性别"
].Value.ToString();
textBox4.Text = this.dataGridView1.CurrentRow.Cells[
"年龄"
].Value.ToString();
}
0 0
- VS2008C#Sqlserver2008数据库的连接,增删改查
- java连接数据库的增删改查
- Android--增删改查--数据库的连接
- VS2008C#连接SQLserver2008示例
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删查改
- 数据库的增删查改
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 支持向量机通俗导论(理解SVM的三层境界)
- activemq的P2P和订阅发布模型
- 二维树状数组模板
- 存储、读取二进制的图像
- ReactNative学习五-Atom安装
- VS2008C#Sqlserver2008数据库的连接,增删改查
- Android Shape 形状
- 寻找旋转排序数组中的最小值
- 如何学习WEB前端设计
- 为学习Hadoop使用VMware准备3台一模一样的Linux虚拟机的详细搭建过程
- 获取文件路径
- JS总结
- 100个直接可以拿来用的JavaScript实用功能代码片段
- 概率论与数量统计(二)3___ 第一章__概率,频率定义