• string strSQL = "Select * From Employees;Select * from Customers";执行两次查询


    SqlCommand对象的字符串SQL命令可以做多个,
    以查询为例,用到SqlDataReader的一些方法,如ExecuteReader(),Read()(一条命令内的移动至下一记录),NextResult()(移动到下一个命令并执行)。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 using System.Data;
     6 using System.Data.SqlClient;
     7 using System.Data.Common;
     8 
     9 namespace AutoLotDataReader
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             Console.WriteLine("***** Fun with Data Readers *****
    ");
    16 
    17             #region Connection string builder logic
    18             // Create a connection string via the builder object.
    19             // SqlConnectionStringBuilder,这样就不用写连接字符串了
    20             SqlConnectionStringBuilder cnStrBuilder =
    21               new SqlConnectionStringBuilder();
    22             //cnStrBuilder.InitialCatalog = "AutoLot";
    23             //cnStrBuilder.DataSource = @"(local)SQLEXPRESS"; 
    24             cnStrBuilder.InitialCatalog="Northwind";
    25             cnStrBuilder.DataSource = @"NLH774";
    26             cnStrBuilder.ConnectTimeout = 30;
    27             cnStrBuilder.IntegratedSecurity = true;
    28 
    29             SqlConnection cn = new SqlConnection();
    30             cn.ConnectionString = cnStrBuilder.ConnectionString;
    31             cn.Open();
    32             ShowConnectionStatus(cn);
    33             #endregion
    34 
    35             // Create a SQL command object w/ 2 select statements.
    36             //执行两句SQL语句
    37             //string strSQL = "Select * From Inventory;Select * from Customers";
    38             string strSQL = "Select * From Employees;Select * from Customers";
    39             SqlCommand myCommand = new SqlCommand(strSQL, cn);
    40 
    41             // Obtain a data reader a la ExecuteReader().
    42             SqlDataReader myDataReader;
    43             myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    44 
    45             #region Loop over each table.
    46             do
    47             {
    48                 //遍历一个查询中的记录
    49                 while (myDataReader.Read())
    50                 {
    51                     //遍历按行输出一个记录的信息,格式列名=列值
    52                     Console.WriteLine("***** Record *****");    
    53                     for (int i = 0; i < myDataReader.FieldCount; i++)
    54                     {
    55                         Console.WriteLine("{0} = {1}",
    56                           myDataReader.GetName(i),
    57                           myDataReader.GetValue(i).ToString().Trim());
    58                     }
    59                     Console.WriteLine();
    60                 }
    61 
    62                 Console.WriteLine("**********************************");
    63             } while (myDataReader.NextResult());    //执行下一个结果集查询(Select * from Customers)
    64             #endregion
    65 
    66             // Because we specified CommandBehavior.CloseConnection, we 
    67             // don't need to explicitly call Close() on the connection.
    68             myDataReader.Close();
    69             Console.ReadLine();
    70         }
    71 
    72         #region Helper method
    73         static void ShowConnectionStatus(DbConnection cn)
    74         {
    75             // Show various stats about current connection object.
    76             Console.WriteLine("***** Info about your connection *****");
    77             Console.WriteLine("Database location: {0}", cn.DataSource);
    78             Console.WriteLine("Database name: {0}", cn.Database);
    79             Console.WriteLine("Timeout: {0}", cn.ConnectionTimeout);
    80             Console.WriteLine("Connection state: {0}
    ", cn.State.ToString());
    81         }
    82         #endregion
    83     }
    84 }

    虽然可以这样,但我觉得从软件编码规范、清晰可读性上说,最好还是不要这么操作。

    最好应该分开执行不同的命令,大不了多声明几个字符串命令而已。

  • 相关阅读:
    iOS开发UI篇—UITabBarController简单介绍
    iOS开发UI篇—ios应用数据存储方式(归档)
    iOS开发UI篇—ios应用数据存储方式(偏好设置)
    iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
    iOS开发UI篇—控制器的View的创建
    WordPress显示备案号
    jQuery插件之Wookmark瀑布流
    Nodejs电影建站开发实例(下)
    Nodejs电影建站开发实例(上)
  • 原文地址:https://www.cnblogs.com/nlh774/p/3436092.html
Copyright © 2020-2023  润新知