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语言的 ...
随机推荐
- ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component解决
第一:先确定一下开发流程是否正确 1.写好servlet组件类 2.写好web.xml文件--向服务器介绍组件 3.发布--就是拷贝 注意:要拷贝包结构,不要只拷贝组件类文件 另外,拷贝的是.clas ...
- ajax请求node.js接口时出现 No 'Access-Control-Allow-Origin' header is present on the requested resource错误
ajax请求node.js接口出现了如下的错误: XMLHttpRequest cannot load http://xxx.xxx.xx.xx:8888/getTem?cityId=110105&a ...
- javascript的window.onload()方法和jQuery的$(document).ready()的对比
jQuery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 windo ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- Linux基础命令(1)
使用Ctr + Alt + F1(2,3,4,5,6)进入终端. 使用Ctr + Alt + F7回到界面. date 显示系统日期 cal 2015 显示2015年的日历表 reboot 重启 sh ...
- MVC Area Usage
ASP.NET MVC Area操作 新建 Area:右键 -> Add –> Area 继承 AreaRegistration,重写AreaName属性与RegisterArea方法 p ...
- BZOJ2905 : 背单词
首先对所有单词建立AC自动机,$S$是$T$的子串等价于$T$的某个前缀通过$fail$链可以走到$S$的终止节点,即$S$的终止节点是$T$某个前缀在$fail$树上的祖先. 设$f[i]$表示考虑 ...
- 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...
- c++ stack 的使用
(1) stack::empty bool empty ( ) const; 判断是否为空. return Value : true if the container size is 0, false ...
- 全面解析Linux数字文件权限
全面解析Linux数字文件权限 来源: 时间:2013-09-04 20:35:13 阅读数:11433 分享到:0 [导读] 在刚开始接触Linux时对于文件权限的理解并不是很透彻,这里详细 ...