您的位置:首页 > 数据库

C#与数据库连接简单测试

2013-11-12 12:25 344 查看
效果展示





 

数据库代码

create database OneDb
go
USE OneDb;
GO
CREATE TABLE classify   --分类表
(
id int primary key identity(1,1),
name nvarchar(20) not null
)

GO
CREATE TABLE product  --产品表
(
id int primary key identity(1,1),
name nvarchar(20) not null,
price decimal,
number int default 0,
c_id int FOREIGN KEY references classify(id)
)
GO

--添加分类测试数据
insert into classify(name) values('图书');
insert into classify(name) values('家电');
insert into classify(name) values('服饰');

--添加商品测试数据
insert into product(name,price,number,c_id) values('arduino基础版',168,50,1);
insert into product(name,price,number,c_id) values('arduino提高版',268,50,1);
insert into product(name,price,number,c_id) values('arduino至尊版',468,50,1);
insert into product(name,price,number,c_id) values('比基尼',68,50,3);
insert into product(name,price,number,c_id) values('虎皮裙',168,50,3);
insert into product(name,price,number,c_id) values('长靴',368,50,3);
insert into product(name,price,number,c_id) values('电冰箱',3268,50,2);
insert into product(name,price,number,c_id) values('烘干机',2268,50,2);
GO

select * from classify;
go
select * from product order by c_id;
go


c# 代码form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AdoTestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = "全部分类";
//确定数据库连接字符串
string constr = "server=QT-201303030913;database=OneDb;uid=sa;pwd=daxiang";
//实例化一个数据库连接的对象
SqlConnection conn = new SqlConnection(constr);
//打开数据库连接
conn.Open();
//实例化SqlCommand对象(该对象主要用于执行Sql命令)
SqlCommand cmd = new SqlCommand();
//指定要执行的SQL语句或者存储过程名称
cmd.CommandText = "select id,name from classify";
//确定上面为CommandText类型所赋的值是SQL语句还是存储过程名称
cmd.CommandType = CommandType.Text;
//指定该命令所用的数据库连接
cmd.Connection = conn;
//声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它
SqlDataReader sdr = cmd.ExecuteReader();

//循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中
//sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的
while (sdr.Read())
{
//将数据流中的第一列的值添加到listBox1的项中
comboBox1.Items.Add(sdr[0]+"-->"+sdr["name"]);
//上面这句也可以用下面这句代替,sdr["name"]表示当前sdr的name列的值
//comboBox1.Items.Add(str["name"]);
}
//关闭数据流
sdr.Close();
//关闭数据库连接
conn.Close();

}

private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text != "全部分类")
{
string constr = "server=QT-201303030913;database=OneDb;uid=sa;pwd=daxiang";
//实例化一个数据库连接的对象
SqlConnection conn = new SqlConnection(constr);
//打开数据库连接
conn.Open();
//实例化SqlCommand对象(该对象主要用于执行Sql命令)
SqlCommand cmd = new SqlCommand();

//获取分类的id
//int i = comboBox1.Text.IndexOf("-->");//获取字符串中-->所在位置索引
string id = comboBox1.Text.Substring(0, 1);//只获取-->之前的字符

//指定要执行的SQL语句和存储过程名字
cmd.CommandText = "select * from product where c_id=" + id;
//去顶上面的CommandText属性所赋值到底是sql还是存储过程名称
cmd.CommandType = CommandType.Text;
//指定该命令所用的数据库连接
cmd.Connection = conn;
//声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它
SqlDataReader sdr = cmd.ExecuteReader();

//清空listBox中的项
listBox1.Items.Clear();

//循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中
//sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的
while (sdr.Read())
{
//将数据流中的第一列的值添加到listBox1的项中
listBox1.Items.Add(sdr["name"]);

}
//关闭数据流
sdr.Close();
//关闭数据库连接
conn.Close();
}
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count > 0)
{
string constr = "server=QT-201303030913;database=OneDb;uid=sa;pwd=daxiang";
//实例化一个数据库连接的对象
SqlConnection conn = new SqlConnection(constr);
//打开数据库连接
conn.Open();
//实例化SqlCommand对象(该对象主要用于执行Sql命令)
SqlCommand cmd = new SqlCommand();

//获取分类的id
//int i = comboBox1.Text.IndexOf("-->");//获取字符串中-->所在位置索引
string id = comboBox1.Text.Substring(0, 1);//只获取-->之前的字符

//指定要执行的SQL语句和存储过程名字
cmd.CommandText = "select * from product where c_id=" + id;
//去顶上面的CommandText属性所赋值到底是sql还是存储过程名称
cmd.CommandType = CommandType.Text;
//指定该命令所用的数据库连接
cmd.Connection = conn;
//声明一个SqlDataReader(数据流对象),并将cmd执行后的结果交给它
SqlDataReader sdr = cmd.ExecuteReader();

//循环整个SqlDataReader对象,将里面的值取出来添加到ListBox中
//sdr.Read()的作用是前进到下一条记录,这也说明SqlDataReader中的数据是一行行放置的
while (sdr.Read())
{
if (sdr["name"].ToString() == listBox1.SelectedItem.ToString())
{
lbl_name.Text = sdr["name"].ToString();
lbl_price.Text = sdr["price"].ToString();
lbl_number.Text = sdr["number"].ToString();
lbl_c_id.Text = comboBox1.Text;
}
}
//关闭数据流
sdr.Close();
//关闭数据库连接
conn.Close();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: