string strSQL = "Select * From Employees;Select * from Customers";执行两次查询
SqlCommand对象的字符串SQL命令可以做多个,
以查询为例,用到SqlDataReader的一些方法,如ExecuteReader(),Read()(一条命令内的移动至下一记录),NextResult()(移动到下一个命令并执行)。
using System;
using System.Collections.Generic;
using System.Text; using System.Data;
using System.Data.SqlClient;
using System.Data.Common; namespace AutoLotDataReader
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Data Readers *****\n"); #region Connection string builder logic
// Create a connection string via the builder object.
// SqlConnectionStringBuilder,这样就不用写连接字符串了
SqlConnectionStringBuilder cnStrBuilder =
new SqlConnectionStringBuilder();
//cnStrBuilder.InitialCatalog = "AutoLot";
//cnStrBuilder.DataSource = @"(local)\SQLEXPRESS";
cnStrBuilder.InitialCatalog="Northwind";
cnStrBuilder.DataSource = @"NLH774";
cnStrBuilder.ConnectTimeout = ;
cnStrBuilder.IntegratedSecurity = true; SqlConnection cn = new SqlConnection();
cn.ConnectionString = cnStrBuilder.ConnectionString;
cn.Open();
ShowConnectionStatus(cn);
#endregion // Create a SQL command object w/ 2 select statements.
//执行两句SQL语句
//string strSQL = "Select * From Inventory;Select * from Customers";
string strSQL = "Select * From Employees;Select * from Customers";
SqlCommand myCommand = new SqlCommand(strSQL, cn); // Obtain a data reader a la ExecuteReader().
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); #region Loop over each table.
do
{
//遍历一个查询中的记录
while (myDataReader.Read())
{
//遍历按行输出一个记录的信息,格式列名=列值
Console.WriteLine("***** Record *****");
for (int i = ; i < myDataReader.FieldCount; i++)
{
Console.WriteLine("{0} = {1}",
myDataReader.GetName(i),
myDataReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
} Console.WriteLine("**********************************");
} while (myDataReader.NextResult()); //执行下一个结果集查询(Select * from Customers)
#endregion // Because we specified CommandBehavior.CloseConnection, we
// don't need to explicitly call Close() on the connection.
myDataReader.Close();
Console.ReadLine();
} #region Helper method
static void ShowConnectionStatus(DbConnection cn)
{
// Show various stats about current connection object.
Console.WriteLine("***** Info about your connection *****");
Console.WriteLine("Database location: {0}", cn.DataSource);
Console.WriteLine("Database name: {0}", cn.Database);
Console.WriteLine("Timeout: {0}", cn.ConnectionTimeout);
Console.WriteLine("Connection state: {0}\n", cn.State.ToString());
}
#endregion
}
}
虽然可以这样,但我觉得从软件编码规范、清晰可读性上说,最好还是不要这么操作。
最好应该分开执行不同的命令,大不了多声明几个字符串命令而已。
string strSQL = "Select * From Employees;Select * from Customers";执行两次查询的更多相关文章
- SQL-31 获取select * from employees对应的执行计划
题目描述 获取select * from employees对应的执行计划 explain select * from employees explain 用于获得表的所有细节
- select count(*)和select count(1)
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...
- select count(*)和select count(1)的区别
一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...
- 仅Firefox中A元素包含Select时点击Select不能选择option
这是在使用京东的一个日期组件时碰到的bug,重现bug的代码精简如下 <!DOCTYPE HTML> <html> <head> <title> 仅Fi ...
- select count(*)和select count(1)的区别 (转)
A 一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键 ...
- 8.2.1.1 Speed of SELECT Statements 加速SELECT 语句
8.2.1 Optimizing SELECT Statements 8.2.2 Optimizing Data Change Statements 8.2.3 Optimizing Database ...
- db2的select语句在db2 client上执行正确,JDBC连接数据库时报错
db2的select语句在db2 client上执行正确,JDBC连接数据库时报错. sql语句是:select ...from QUALIFIER.tableName fetch first 21 ...
- 【SQL】INSERT INTO SELECT语句与SELECT INTO FROM语句
INSERT INTO SELECT语句与SELECT INTO FROM语句,都是将一个结果集插入到一个表中: #INSERT INTO SELECT语句 1.语法形式: Insert into T ...
- Go语言规格说明书 之 select语句(Select statements)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
随机推荐
- BZOJ4389 : ZYB and Trees
Link-Cut Tree维护. 每个点x维护以下信息: v:这个点的点权 s:实链上的信息和 st:子树信息和(不包括链上) sa:子树+链上的信息和 as:所有虚儿子的sa的和 则有 s[x]=v ...
- [Cocos2d-x For WP8]Progress 进度条
Cocos2d-x可以有多种进度条的展示方式,进度条的种类是根据进度条运动的方向来区分,包括顺时针,逆时针,从左到右,从右到左,从下到上和从上到下6种方式,这和WP8的进度条是由很大的区别的.那么Co ...
- 【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...
- BZOJ3925: [Zjoi2015]地震后的幻想乡
Description 傲娇少女幽香是一个很萌很萌的妹子,而且她非常非常地有爱心,很喜欢为幻想乡的人们做一些自己力所能及的事情来帮助他们. 这不,幻想乡突然发生了地震,所有的道路都崩塌了.现在的首要任 ...
- hiho#14
军训去了没有打,回来看题跑. T1:hehe 注意X可能是实数233 #include<cstdio> #include<cctype> #include<queue&g ...
- sqlserver常用日期、时间函数和格式
Sql Server中常用的日期与时间函数1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 ...
- .NET生成静态页面的方案总结
转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...
- JavaScript事件大全3
//无模式的提示框 //屏蔽按键 <html> <head> <meta http-equiv="Content-Type" content=& ...
- OpenCV IplImage FlyCapture2 Image Conversion 两种图像类的相互转化
OpenCV的IplImag和 FlyCapture2 的 Image是两种常见的图片格式,在实际的应用中,我们通常要混合使用OpenCV和FlyCapture2这两个SDK,所以这两种图片格式之间的 ...
- 一种少见的跨目录写webshell方法
http://hi.baidu.com/kwthqquszlbhkyd/item/480716204cfa33c3a5275afa