效果展示
數據庫代碼
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(); } } } }