【问题标题】:C# SQL Select Query only dump one columnC# SQL Select Query 只转储一列
【发布时间】:2015-06-03 14:22:41
【问题描述】:

我卡在连接 SQL 并运行一个简单的选择查询,我需要将 select * from ServerInfo where ServerID = 1991638835" 输出到控制台行或 txt 文件:

这是我的代码:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text;

namespace ConnectingToSQLC
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("server= XXXXX; database = ES1Archive; Integrated Security=false; User ID = sa; Password=XXXXXX");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from ServerInfo  where ServerID = 1991638835", conn);

            SqlDataReader reader = cmd.ExecuteReader ();
            while (reader.Read())
            {
            //Console.WriteLine ("{1}", "{0}", reader.GetString (0), reader.GetString (1));
                Console.WriteLine(reader.GetString(3));
            }
            reader.Close();
            conn.Close();

            if (Debugger.IsAttached)
            {
                Console.ReadLine(); 
            }

      }
    }
}

一切正常。但如果我改为 Console.WriteLine ("{1}", "{3}", reader.GetString (1), reader.GetString (3)); 我总是有第 2 列。总共有 7 列,第 3 列是空的,而第 4 列是 XML。

如何修改我的代码以全部转储?

【问题讨论】:

  • 表格的格式是什么?

标签: c# sql select


【解决方案1】:

您的格式字符串过多 - 将它们组合成一个,例如:

Console.WriteLine ("{0} - {1}", reader.GetString(1), reader.GetString(3))

Console.WriteLineoverload you are using 格式化字符串的索引类似于string.Format - 它不依赖于您在阅读器中使用的哪些 索引,格式参数占位符只需要以匹配其他params args 参数的顺序

还有:

  • 一般来说,使用SELECT * 是不好的做法,当您按序号索引访问数据读取器列时甚至更危险,因为表中的更改会破坏您的读取器代码。建议您明确命名您正在使用的列和/或使用列名访问器来访问reader
  • 如果阅读器的唯一用途是Console.WriteLine 上的字符串输出,则不需要使用Get<Type>() 重载 - 您可以简单地使用序数访问器(在保证了具有显式列名称 SELECT 的列 :-)

例如对于读者第 1、3 和 6 列(当然是从零开始):

Console.WriteLine ("{0} - {1} - {2}", reader[1], reader[3], reader[6]);

但更好的是:

Console.WriteLine ("{0} - {1} - {2}", reader["RealNameOfCol1"], 
                                      reader["RealNameOfCol3"], reader["RealNameOfCol6"]);

【讨论】:

    【解决方案2】:

    您可以使用 IDataRecord 接口循环执行此操作:

    class Program
    {
       static void Main(string[] args)
       {
           SqlConnection conn = new SqlConnection("server= XXXXX; database = ES1Archive; Integrated Security=false; User ID = sa; Password=XXXXXX");
           conn.Open();
           SqlCommand cmd = new SqlCommand("select * from ServerInfo  where ServerID = 1991638835", conn);
    
           SqlDataReader reader = cmd.ExecuteReader ();
           while (reader.Read())
           {
                for(int i = 0; i<reader.FieldCount;i++)
                {
                    Console.WriteLine(reader[i]);
                }
           }
           reader.Close();
           conn.Close();
    
           if (Debugger.IsAttached)
           {
               Console.ReadLine(); 
           }
    
     }
    }
    

    【讨论】:

    • 您不必强制转换为 IDataRecord 即可使用 FieldCount 和索引器。
    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多