SQL连接 - 同一连接下的DataReader不会带来不同的结果

时间:2014-07-02 14:43:12

标签: c# sql sql-server

在C#中编写了一个代码,用于连接两个SQL服务器,以便通过两个连接,三个数据库和六个查询获得6个不同查询的结果。 问题:第一个查询和最后三个查询正确地带来了结果,但第二个和第三个查询带来了查询1的结果。 请帮帮我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;

namespace TOCOrderStats
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection con1, con2;
            SqlDataReader dr1, dr2, dr3, dr4, dr5, dr6;
            SqlCommand cmd1, cmd2, cmd3, cmd4, cmd5, cmd6;

            con1 = new SqlConnection("Server=SSE-SQL-CLUSTER;Database=wlr3crm;Trusted_Connection=True;");         
            con2 = new SqlConnection("Server=WLR3-SQL-CLUSTE;Database=wlr3crm_orange;Trusted_Connection=True;");

            con1.Open();
            Console.WriteLine("Connection 1 Open");
            try
            {
                string query1 = "select servicetype, ordertype, count(servicetype) as ordercount from ordermaster "+
"where CONVERT(VARCHAR(10),created_date, 111) >= CONVERT(VARCHAR(10), GETDATE()-1, 111) and CONVERT(VARCHAR(10),"+
"created_date, 111) < CONVERT(VARCHAR(10), GETDATE(), 111) group by servicetype,ordertype;";

                Console.WriteLine("SSEQuery 1 Consumed");
                if (!string.IsNullOrEmpty(query1))
                {
                    Console.WriteLine("SSEQuery1.txt isn't NULL");
                }
                cmd1 = new SqlCommand(query1, con1);
                Console.WriteLine("cmd 1 initiated");

                dr1 = cmd1.ExecuteReader();
                Console.WriteLine("dr1 executed");

                if (dr1 == null)
                {
                    File.WriteAllText("DataReader1NULL.txt","DataReader brought nothing; check connection/query");
                    Console.WriteLine("DR1 brought nothing");
                }

                while (dr1.Read())
                {
                    Console.WriteLine("DR1 read begins");
                    string SSE1results_column1 = "";
                    string SSE1results_column2 = "";
                    string SSE1results_column3 = "";

                    SSE1results_column1 = dr1["servicetype"].ToString();
                    SSE1results_column2 = dr1["ordertype"].ToString();
                    SSE1results_column3 = dr1["ordercount"].ToString();

                    string SSE1results_rows = SSE1results_column1 + "#" + SSE1results_column2 + "#" + SSE1results_column3 + "#";
                    Console.WriteLine("Result of Query1 " + SSE1results_rows);

                    if (SSE1results_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result1.txt", "NULL1" + "#" + "NULL2" + "#" + "NULL3" + "#");
                        Console.WriteLine("Result in NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result1.txt", SSE1results_rows);
                        Console.WriteLine("Obtained result from Query1 and written to file");
                    }
                }
                Console.WriteLine("DR1 ends");

                dr1.Close();
                Console.WriteLine("DR1 closes");
                cmd1.Dispose();
                Console.WriteLine("cmd1 disposes");

                //###################################################

                string query2 = "select servicetype, ordertype, count(servicetype) as ordercount from"+
"ordermaster where CONVERT(VARCHAR(10),created_date, 111) >= CONVERT(VARCHAR(10), GETDATE()-2, 111) and "+
"CONVERT(VARCHAR(10),created_date, 111) < CONVERT(VARCHAR(10), GETDATE()-1, 111) group by servicetype,ordertype;";

                Console.WriteLine("SSEQuery 2 Consumed");
                if (!string.IsNullOrEmpty(query1))
                {
                    Console.WriteLine("SSEQuery2.txt isn't NULL");
                }

                cmd2 = new SqlCommand(query2, con1);
                Console.WriteLine("cmd2 initiated");
                dr2 = cmd1.ExecuteReader();
                Console.WriteLine("DR2 executed");

                if (dr2 == null)
                {
                    File.WriteAllText("DataReader2NULL.txt", "DataReader brought nothing; check connection/query");
                    Console.WriteLine("DataReader2 brought nothing");
                }

                while (dr2.Read())
                {
                    Console.WriteLine("DR2 read begins");
                    string SSE2results_column1 = "";
                    string SSE2results_column2 = "";
                    string SSE2results_column3 = "";

                    SSE2results_column1 = dr2["servicetype"].ToString();
                    SSE2results_column2 = dr2["ordertype"].ToString();
                    SSE2results_column3 = dr2["ordercount"].ToString();

                    string SSE2results_rows = SSE2results_column1 + "#" + SSE2results_column2 + "#" + SSE2results_column3 + "#";
                    Console.WriteLine("Result of Query2 " + SSE2results_rows);

                    if (SSE2results_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result2.txt", "NULL1" + "#" + "NULL2" + "#" + "NULL3" + "#");
                        Console.WriteLine("Result in NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result2.txt", SSE2results_rows);
                        Console.WriteLine("Obtained result from Query2 and written to file");
                    }
                }
                Console.WriteLine("DR2 ends");
                dr2.Close();
                Console.WriteLine("DR2 closes");
                cmd2.Dispose();
                Console.WriteLine("cmd2 disposes");

                //###################################################

                string query3 = "select servicetype, ordertype, count(servicetype) as ordercount from ordermaster where "+
"CONVERT(VARCHAR(10),created_date, 111) >= CONVERT(VARCHAR(10), GETDATE()-3, 111) and CONVERT(VARCHAR(10),created_date,"+
" 111) < CONVERT(VARCHAR(10), GETDATE()-2, 111) group by servicetype,ordertype;";
                Console.WriteLine("SSEQuery 3 Consumed");
                if (!string.IsNullOrEmpty(query1))
                {
                    Console.WriteLine("SSEQuery3.txt isn't NULL");

                }
                cmd3 = new SqlCommand(query3, con1);
                Console.WriteLine("cmd3 initiated");
                dr3 = cmd1.ExecuteReader();
                Console.WriteLine("DR3 executed");

                if (dr3 == null)
                {
                    File.WriteAllText("DataReader3NULL.txt", "DataReader brought nothing; check connection/query");
                    Console.WriteLine("DataReader3 brought nothing");
                }

                while (dr3.Read())
                {
                    Console.WriteLine("DR3 read begins");
                    string SSE3results_column1 = "";
                    string SSE3results_column2 = "";
                    string SSE3results_column3 = "";

                    SSE3results_column1 = dr3["servicetype"].ToString();
                    SSE3results_column2 = dr3["ordertype"].ToString();
                    SSE3results_column3 = dr3["ordercount"].ToString();


                    string SSE3results_rows = SSE3results_column1 + "#" + SSE3results_column2 + "#" + SSE3results_column3 + "#";
                    Console.WriteLine("Result of Query3 " + SSE3results_rows);

                    if (SSE3results_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result3.txt", "NULL1" + "#" + "NULL2" + "#" + "NULL3" + "#");
                        Console.WriteLine("Result is NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result3.txt", SSE3results_rows);
                        Console.WriteLine("Obtained result from Query3 and written to file");
                    }
                }
                Console.WriteLine("DR3 ends");
                dr3.Close();
                Console.WriteLine("DR3 closes");
                cmd3.Dispose();
                Console.WriteLine("cmd3 disposes");

                //###################################################

                string query4 = "select servicetype, count(servicetype)as Active_count from servicemaster where servicetype='WLR Single Line' and linestatus = 'Active' group by servicetype;";
                Console.WriteLine("SSEQuery 4 Consumed");
                if (!string.IsNullOrEmpty(query1))
                {
                    Console.WriteLine("SSEQuery4.txt isn't NULL");

                }

                cmd4 = new SqlCommand(query4, con1);
                Console.WriteLine("cmd4 initiated");
                dr4 = cmd4.ExecuteReader();
                Console.WriteLine("DR4 executed");

                if (dr4 == null)
                {
                    File.WriteAllText("DataReader4NULL.txt", "DataReader brought nothing; check connection/query");
                    Console.WriteLine("DataReader4 brought nothing");
                }

                while (dr4.Read())
                {
                    Console.WriteLine("DR4 read begins");
                    string SSE4results_column1 = "";
                    string SSE4results_column2 = "";

                    SSE4results_column1 = dr4["servicetype"].ToString();
                    SSE4results_column2 = dr4["Active_count"].ToString();                    

                    string SSE4results_rows = SSE4results_column1 + "#" + SSE4results_column2 + "#";
                    Console.WriteLine("Result of Query4 " + SSE4results_rows);

                    if (SSE4results_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result4.txt", "NULL1" + "#" + "NULL2" + "#");
                        Console.WriteLine("Result in NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result4.txt", SSE4results_rows);
                        Console.WriteLine("Obtained result from Query4 and written to file");
                    }
                }
                Console.WriteLine("DR4 ends");
                dr4.Close();
                Console.WriteLine("DR4 closes");
                cmd4.Dispose();
                Console.WriteLine("cmd4 disposes");
                con1.Close();
                Console.WriteLine("Connection 1 ends");

            }
            catch (Exception ex1)
            {
                File.WriteAllText("Exception1.txt", ex1.ToString());
                Console.WriteLine("Exception message 1" + ex1.ToString());
            }

            //###################################################

            con2.Open();
            Console.WriteLine("Connection 2 opens");
            try
            {
                string query5 = "select servicetype, count(servicetype) as Active_Count from servicemaster where "+
                    "servicetype='WLR Single Line' and linestatus = 'Active' group by servicetype;";
                Console.WriteLine("ORQuery 1 Consumed");
                if (!string.IsNullOrEmpty(query5))
                {
                    Console.WriteLine("ORQuery1.txt isn't NULL");

                }
                cmd5 = new SqlCommand(query5, con2);
                Console.WriteLine("cmd5 initiated");
                dr5 = cmd5.ExecuteReader();
                Console.WriteLine("dr5 executed");

                if (dr5 == null)
                {
                    File.WriteAllText("DataReader5NULL.txt", "DataReader brought nothing; check connection/query");
                    Console.WriteLine("DataReader5 brought nothing");
                }

                while (dr5.Read())
                {
                    Console.WriteLine("DR5 read begins");
                    string ORresults_column1 = "";
                    string ORresults_column2 = "";

                    ORresults_column1 = dr5["servicetype"].ToString();
                    ORresults_column2 = dr5["Active_count"].ToString();

                    string ORresults_rows = ORresults_column1 + "#" + ORresults_column2 + "#";
                    Console.WriteLine("Result of Query5 " + ORresults_rows);

                    if (ORresults_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result5.txt", "NULL1" + "#" + "NULL2" + "#");
                        Console.WriteLine("Result in NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result5.txt", ORresults_rows);
                        Console.WriteLine("Obtained result from Query5 and written to file");
                    }

                }
                Console.WriteLine("DR5 ends");
                dr5.Close();
                Console.WriteLine("DR5 closes");
                cmd5.Dispose();
                Console.WriteLine("cmd5 disposes");

            }
            catch(Exception ex2)
            {
                File.WriteAllText("Exception2.txt", ex2.ToString());
                Console.WriteLine("Exception message 2" + ex2.ToString());
            }

            //###################################################

            string database="wlr3crm_HQN";
            con2.ChangeDatabase(database);

            try
            {
                string query6 = "select servicetype, count(servicetype) as Active_Count from servicemaster where "+
                    "servicetype='WLR Single Line' and linestatus = 'Active' group by servicetype;";
                Console.WriteLine("HQNQuery1  Consumed");
                if (!string.IsNullOrEmpty(query6))
                {
                    Console.WriteLine("HQNQuery1.txt isn't NULL");

                }
                cmd6 = new SqlCommand(query6, con2);
                Console.WriteLine("cmd6 initiated");
                dr6 = cmd6.ExecuteReader();
                Console.WriteLine("dr6 executed");

                if (dr6 == null)
                {
                    File.WriteAllText("DataReader6NULL.txt", "DataReader brought nothing; check connection/query");
                    Console.WriteLine("DataReader6 brought nothing");
                }

                while (dr6.Read())
                {
                    Console.WriteLine("DR6 read begins");
                    string HQNresults_column1, HQNresults_column2 = "";
                    HQNresults_column1 = dr6["servicetype"].ToString();
                    HQNresults_column2 = dr6["Active_count"].ToString();

                    string HQNresults_rows = HQNresults_column1 + "#" + HQNresults_column2 + "#";
                    Console.WriteLine("Result of Query6 " + HQNresults_rows);

                    if (HQNresults_rows == "")
                    {
                        File.AppendAllText("D:/TOC/Debug/Result6.txt", "NULL1" + "#" + "NULL2" + "#");
                        Console.WriteLine("Result is NULL");
                    }

                    else
                    {
                        File.AppendAllText("D:/TOC/Debug/Result6.txt", HQNresults_rows);
                        Console.WriteLine("Obtained result from Query6 and written to file");
                    }

                }
                Console.WriteLine("DR6 ends");
                dr6.Close();
                Console.WriteLine("DR6 closes");
                cmd6.Dispose();
                Console.WriteLine("cmd6 disposes");
                con2.Close();
                Console.WriteLine("Connection 3 ends");

            }
            catch(Exception ex3)
            {
                File.WriteAllText("Exception3.txt", ex3.ToString());
                Console.WriteLine("Exception message 3" + ex3.ToString());
                Console.ReadKey();               

            }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

你的代码中有拼写错误。您正多次执行第一次SqlCommand

cmd2 = new SqlCommand(query2, con1);
Console.WriteLine("cmd2 initiated");
dr2 = cmd1.ExecuteReader();

...

cmd3 = new SqlCommand(query3, con1);
Console.WriteLine("cmd3 initiated");
dr3 = cmd1.ExecuteReader();

当然,你在第一次执行它之后也会调用cmd1.Dispose();,所以我不确定你是如何得到例外的......

您的代码中还有其他一些拼写错误,例如您在每次调用数据库之前生成的信息控制台消息:

if (!string.IsNullOrEmpty(query1))  // <-- should be query3?
{
    Console.WriteLine("SSEQuery3.txt isn't NULL");
}

答案 1 :(得分:1)

您为命令2和3重新调用cmd1

cmd3 = new SqlCommand(query3, con1);
Console.WriteLine("cmd3 initiated");
dr3 = cmd1.ExecuteReader();

将其更新为:

dr2 = cmd2.ExecuteReader();

dr3 = cmd3.ExecuteReader();

答案 2 :(得分:0)

解决您的问题,明智地使用复制/粘贴。

 dr2 = cmd1.ExecuteReader();
 Console.WriteLine("DR2 executed");

您已将cmd1的读者分配给dr2和dr3。请将其更正为cmd2和cmd3。

我也看到了其他错误。代码编写得不好。