惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 段静迪

跟我学LINQ之一:为什么要学习LINQ C语言游戏 JAVA猜字游戏 在.NET平台下实现打印超市收银票据 JAVA上课源代码 基于三层架构的软件开发技术 MS-SQL数据库开发常用汇总 计算机类在Java中的设计于实现码 基于C语言的个人所得税计税系统 JAVA 教学实例 在.NET平台下使用SQL2000 Image类型数据 在.NET平台下使用SQL2000 Image类型数据 界面和业务代码分离的记事本 在.Net平台下读写文件流 键盘游戏 多线程摇奖机程序源代码 网页第五章 HTML标签详解 JDK 目录下的*.exe文件的使用
对SQL Server2000数据块进行查询
段静迪 · 2007-10-24 · via 博客园 - 段静迪
 

SQL Server2000数据块进行查询

问题描述:

       针对各种不同的数据库应用环境,ADO.NET提供了四种数据库提供程序,这四种数据库提供程序位于不同的命名空间,但是它们的组成结构相同,使用方法相似。本实验要求利用VS2003平台对SQL2000进行数据库的查询操作。

实验目的:

1、熟悉T-SQL语句

2、在线式非存储过程方式查询数据

3、在线式存储过程方式查询数据

4、断开式非存储过程方式查询数据

5、断开式存储过程方式查询数据

问题分析:

       无论式那种模式,操纵数据库的步骤都一样:

1、确立查询语句:

select * from job where min_lvl >20

结果如下:

job_id job_desc                                           min_lvl max_lvl

------ -------------------------------------------------- ------- -------

3      Business Operations Manager                        176     225

4      Chief Financial Officier                           176     250

5      Publisher                                          151     250

6      Managing Editor                                    141     225

7      Marketing Manager                                  121     200

8      Public Relations Manager                           103     175

9      Acquisitions Manager                               78      175

10     Productions Manager                                78      165

11     Operations Manager                                 78      150

12     Editor                                             28      100

13     Sales Representative                               28      100

14     Designer                                           28      100

(所影响的行数为 12 行)

2、建立连接

3、建立Command对象

4、执行

5、显示结果

关键代码解析

1、在线式非存储过程方式查询数据

//SQL认证模式的建立连接代码如下

//using( SqlConnection sc = new SqlConnection("server =192.168.8.66;database = pubs;uid = sa;pwd = sa;") )

//Windows集成认证模式的建立连接代码如下

using(SqlConnection sc = new SqlConnection("server = .;database = pubs;trusted_connection = sspi;"))

           {//执行可能抛出异常的代码

              try

              {//打开数据库连接

                  sc.Open();

//实例化命令对象

               SqlCommand com =new SqlCommand("select * from job where min_lvl >20",sc);

                  com.CommandType = ommandType.Text;

//只读向前的方式显示数据

                  SqlDataReader sr = com.ExecuteReader();

                  while(sr.Read())

                  {

                     Console.WriteLine(sr[0]);

                  }

              }

              catch(Exception ex)

              {

                  Console.WriteLine(ex.ToString());

              }

              finally

              {

                  if(sc.State ==ConnectionState.Open)

                     sc.Close();

              }

}

2、在线式存储过程方式查询数据

创建存储过程:

USE pubs

--如果存在Sel存储过程,则将其删除

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'Sel' AND type = 'P')

   DROP PROCEDURE Sel

GO

create proc Sel

       (@con int)

as

select * from job where min_lvl < @con

显示代码:

//using( SqlConnection sc = new SqlConnection("server =192.168.8.66;database = pubs;uid = sa;pwd = sa;") )

           using(SqlConnection sc = new SqlConnection("server = .;database = pubs;trusted_connection = sspi;"))

           {

              try

              {

                  sc.Open();

//指明存储过程的过程名和连接对象                

SqlCommand com =new SqlCommand("sel",sc);

//指明命令对象的执行命令类型

                  com.CommandType = CommandType.StoredProcedure;

//为命令对象添加参数对象

/*

    需要指明参数对象的存储过程变量名"@con",数据类型SqlDbType.Int

*/

SqlParameter sp = new SqlParameter("@con",SqlDbType.Int);

//设定值

sp.Value =20;

//设定传值方向

sp.Direction = ParameterDirection.Input;

//为命令对象添加参数对象

com.Parameters.Add(sp);

//执行命令

SqlDataReader sr = com.ExecuteReader();

                  while(sr.Read())

                  {//遍历数据

                     Console.WriteLine(sr[0]);

                  }

              }

              catch(Exception ex)

              {

                  Console.WriteLine(ex.ToString());

              }

              finally

              {

                  if(sc.State ==ConnectionState.Open)

                     sc.Close();

              }

3、断开式非存储过程方式查询数据

//实例化sqlcommand对象

this.sqlSelectCommand1.CommandText ="SELECT id, name, sex, email FROM stuinfo";

this.sqlSelectCommand1.Connection = this.sqlConnection1;

//为数据适配器设置查询命令对象

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

//绑定数据源

this.dataGrid1.DataSource =this.dataSet11;

this.dataGrid1.DataMember = "stuinfo";

//填充数据集

this.sqlDataAdapter1.Fill(this.dataSet11);

4、断开式存储过程方式查询数据

//实例化sqlcommand对象,步骤二完全类似

this.sqlSelectCommand1.CommandText =   "Sel";

this.sqlSelectCommand1.CommandType = CommandType.StoredProcedure;

SqlParameter sp = new SqlParameter("@con",SqlDbType.Int);

        sp.Value =20;

        sp.Direction = ParameterDirection.Input;

        this.sqlSelectCommand1.Parameters.Add(sp);

this.sqlSelectCommand1.Connection = this.sqlConnection1;

//为数据适配器设置查询命令对象

this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;

//绑定数据源

this.dataGrid1.DataSource =this.dataSet11;

this.dataGrid1.DataMember = "stuinfo";

//填充数据集

this.sqlDataAdapter1.Fill(this.dataSet11);