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";执行两次查询的更多相关文章

  1. SQL-31 获取select * from employees对应的执行计划

    题目描述 获取select * from employees对应的执行计划 explain select * from employees explain  用于获得表的所有细节

  2. select count(*)和select count(1)

    一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...

  3. select count(*)和select count(1)的区别

    一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...

  4. 仅Firefox中A元素包含Select时点击Select不能选择option

    这是在使用京东的一个日期组件时碰到的bug,重现bug的代码精简如下 <!DOCTYPE HTML> <html> <head> <title> 仅Fi ...

  5. select count(*)和select count(1)的区别 (转)

    A 一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键 ...

  6. 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 ...

  7. db2的select语句在db2 client上执行正确,JDBC连接数据库时报错

    db2的select语句在db2 client上执行正确,JDBC连接数据库时报错. sql语句是:select ...from QUALIFIER.tableName fetch first 21 ...

  8. 【SQL】INSERT INTO SELECT语句与SELECT INTO FROM语句

    INSERT INTO SELECT语句与SELECT INTO FROM语句,都是将一个结果集插入到一个表中: #INSERT INTO SELECT语句 1.语法形式: Insert into T ...

  9. Go语言规格说明书 之 select语句(Select statements)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...

随机推荐

  1. VMware 虚拟机网络 组网问题

    1.VMware虚拟机组网概述 整个结构: 需要确定的内容: 1) 虚拟机连接到哪个VMnet(交换机)? 2) VMnet(交换机)的组网模式? 首先,讲一下VMware的界面内容 安装好VMwar ...

  2. linux全部命令

    linux全部命令 一.安装和登陆命令1.进入图形界面startx 2.进入图形界面init 5 3.进入字符界面init 3 4.登陆login 5.关机poweroff-p 关闭机器的时候关闭电源 ...

  3. [转]说说C语言运算符的“优先级”与“结合性”

    补充自己的一点理解: 1.关于++i 与 i++的区别. ++i 和 i++如果是单独使用的语句,即二者后面均加上分号,或者其他单独使用的语句,没有任何区别.例如: for(i=0;i<100; ...

  4. [LintCode] Binary Tree Serialization

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  5. c++ string 结束符‘\000’

    昨天输出string类型时总是出错,发现输出到文件的一行里多了^@,输出到console却看不到,debug发现,string类型中多了\000,这主要由于我想要用\0截掉字符串最后一位,所以把字符串 ...

  6. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

  7. 分支语句:if

    (1)分支语句if: if(判断条件) { 满足条件要执行的语句(若满足则alert输出(“”)) } else { 不满足条件时执行的语句 } (若if满足,else绝对不走,反之,走else) 例 ...

  8. mysql5.6安装

    mysql5.6安装 #卸载原有的mysqlyum remove mysql*ls /etc/my.cnf*mv /etc/my.cnf* /tmp/ #安装依赖包yum install make c ...

  9. Apache Commons CLI 简介

    CLI 命令代码实现 命令行程序处理流程相对比较简单,主要流程为设定命令行参数 -> 解析输入参数 -> 使用输入的数据进行逻辑处理CLI 定义阶段 每一条命令行都必须定义一组参数,它们被 ...

  10. [转] - QThread应用探讨

    QThread 似乎是很难的一个东西,特别是信号和槽,有非常多的人(尽管使用者本人往往不知道)在用不恰当(甚至错误)的方式在使用 QThread,随便用google一搜,就能搜出大量结果出来.无怪乎Q ...