簡體   English   中英

如何將 SQL 查詢的結果存儲在變量中?

[英]How can I store results of SQL query in a variable?

如果您看一下下面的代碼,您會看到我正在遍歷查詢列表並輸出帶有結果的查詢。

我希望能夠將當前輸出的內容存儲到我可以放入 smtp 電子郵件正文的變量或列表中。 有沒有人有任何想法?

private static void Main()
{
    Console.SetWindowSize(170, 60);

    List<string> queryList = new List<string>(new string[]
    {
        "query 1",
        "query 2",
        "query 3"
    });

    foreach (string query in queryList)
    {
        SqlConnection connection = new SqlConnection("Connection String");

        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine();
                Console.WriteLine(query  + Environment.NewLine + "{0}", reader.GetString(0));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }

    Console.WriteLine(Environment.NewLine + "Press any key to exit...");
    Console.ReadKey();
}

你可以這樣做。

private static void Main()
{
    var queryResults = new List<string>();

    foreach (string query in queryList)
    {
        ...

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine();
                Console.WriteLine(query  + Environment.NewLine + "{0}", reader.GetString(0));
                queryResults.Add(reader.GetString(0));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();

    }
    ...
}

您可以稍后將queryResults用於任何其他目的。

就像您使用列表來存儲要執行的查詢一樣,您也可以使用列表來存儲查詢的結果

List<string> results = new List<string>();
foreach (string query in queryList)
{
    using(SqlConnection connection = new SqlConnection("Connection String"))
    using(SqlCommand command = new SqlCommand(query, connection))
    {
         connection.Open();
         using(SqlDataReader reader = command.ExecuteReader())
         {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    results.Add(query + Environment.NewLine + reader.GetString(0));
                }
            }
            else
            {
                results.Add(query + Environment.NewLine + "No rows found";
            }
        }
    }
}

不要忘記在用於查詢數據庫的一次性對象周圍應用正確的 using 語句。 此外,更多地了解您的查詢可能會導致創建一批命令傳遞給數據庫引擎並使用SqlDataReaderNextResult方法檢索結果的更好方法

例如,假設您的查詢是這樣的:

 SELECT FIELD1 FROM TABLE1
 SELECT FIELD1 FROM TABLE2
 SELECT FIELD1 FROM TABLE3

那么您可以嘗試以這種方式將字符串連接在一起(刪除 foreach 循環和多個連接、命令和數據讀取器實例)

 string queries = string.Join(";", queryList.ToArray());
 using(SqlConnection connection = new SqlConnection("Connection String"))
 using(SqlCommand command = new SqlCommand(query, connection))
 {
    connection.Open();
    using(SqlDataReader reader = command.ExecuteReader())
    {
        do
        {
            if (dr.HasRows)
            {
                results.Add(query)
                while (dr.Read())
                {
                    results.Add(Environment.NewLine + reader.GetString(0));
                }
            }
            else
                results.Add(query + Environment.NewLine + "No rows found);
        }
        while (dr.NextResult());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM