C#与数据库连接简单测试
效果展示

数据库代码
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();
}
}
}
}
C#与数据库连接简单测试的更多相关文章
- 利用EasyMock生成数据库连接简单测试示例
package demo.mock; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Re ...
- TODO:Golang UDP连接简单测试慎用Deadline
TODO:Golang UDP连接简单测试慎用Deadline UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interco ...
- .net orm比较之dapper和Entity Framework6的简单测试比较
.net orm比较之dapper和Entity Framework6的简单测试比较
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试 系列目录 我想测试EF在一百万条数据下的显示时间! ...
- ORACLE 数据库简单测试
ORACLE 数据库简单测试 操作系统:Windows 7 – ORACLE:oracle database 10.2.0.4 一.目的 测试 启动监听程序.数据库 非同一个用户的情况,用户是否可以 ...
- Javascript的简单测试环境
在<JavaScript忍者秘籍>2.4测试条件基础知识中,作者给出了一个精简版的assert和assert组的实现,对于初学者而言,这无疑是一个很好的例子,既让我们得到了一个好用的小工具 ...
- struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)
为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...
- [20190423]简单测试latch nowilling等待模式.txt
[20190423]简单测试latch nowilling等待模式.txt --//我对这个问题的理解就是如果参数willing=0,表示无法获取该latch,直接退出,再寻找类似的latch.--/ ...
随机推荐
- react-native 中使用 mobx
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法而MobX提供了一种存储,更新 状态 的方法React 和 MobX都在优化着软件开发中相同的问题.Reac ...
- DICOM医学图像处理:WEB PACS初谈二,图像的传输
背景: 如前一篇专栏博文所述,借助于CGI或FastCGI技术转发浏览器发送过来的用户请求,启动本地的DCMTK和CxImage库响应.然后将处理结果转换成常规图像返回到浏览器来实现Web PACS. ...
- js事件之onmousedown和onmouseup
<!DOCTYPE html> <html> <head> <script> function mouseDown() { document.getEl ...
- SpringBoot使用Thymeleaf模板
© 版权声明:本文为博主原创文章,转载请注明出处 Thymeleaf模板简介 Thymeleaf模板是一个现代化的服务端java模板引擎对于所有的web和独立环境 Thymeleaf的主要目标是为你的 ...
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...
- 实现Nullable 可空类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace demo ...
- 微信公众号开发---上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing
1.上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing 解决办法:因为php版本 ...
- 在Mac上为自己手动编译安装一套PHP7的开发环境
首先你得去官网下载php7 beta1的版本 这里由于我是在mac上安装,所以就去下载linux相关的版本,地址也直接附上了php7 beta1windows版的官方也有发布详情猛戳:这里 解压安装包 ...
- spring boot配置文件
1.spring boot通常打成一个jar文件发布,想修改配置文件比较麻烦,但他提供了一种读取外部配置文件的方式.在代码的主类中增加如下代码 System.setProperty("spr ...
- sublime使用技巧(4)-- 其他技巧【持续更新】
命令模式 1.切换语言格式,ctrl + shirt + p 2.简化操作 ctrl + shirt + p 输入 snippet:function 自动生成function的基本结构!tab键 移动 ...