已經編寫了一個C#代碼,用於連接兩個SQL服務器以獲得包含兩個連接,三個數據庫和六個查詢的6個不同查詢的結果。 問題:第一個查詢和最後三個查詢都正確地帶來了結果,但是第二個和第三個查詢帶來了查詢1的結果。 請幫助我。SQL連接 - 在同一連接下的DataReaders不會帶來不同的結果
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();
}
}
}
}
非常感謝您的快速響應。我沒有注意到這一點。再次感謝。 – Sparrow
如果人們開始使用(){}來編碼生活將會變得如此簡單... – Darek
這是一個很棒的點@Darek。用[使用語句](http://msdn.microsoft.com/en-us/library/yh598w02.aspx)來包圍'SqlConnection','SqlCommand'等是確定你的想法的好主意即使拋出異常,資源也會正確放置。 –