0
火我創建一個Windows服務,下面這段代碼。不知道爲什麼它不會觸發最後的SQL Server存儲過程。如果我在代碼中沒有任何內容,但是存儲過程比它啓動正常。沒有錯誤。存儲過程無法在Windows服務
using (SqlConnection SqlConnection = new SqlConnection(connectionString))
{
SqlConnection.Open();
using (SqlCommand cmdCreateITableSP = new SqlCommand("CreateSP", SqlConnection))
{
cmdCreateITableSP.CommandType = CommandType.StoredProcedure;
cmdCreateTableSP.ExecuteNonQuery();
}
string INTable = "IN";
string XMLPackagesDir = "D:\\Cle\\";
// Create a datatable with two columns:
DataTable INHReponseDT = new DataTable("INHReponseDT");
// Create Columns:
INHReponseDT.Columns.Add("XM");
INHReponseDT.Columns.Add("Cl");
INHReponseDT.Columns.Add("I");
INHReponseDT.Columns.Add("INH");
INHReponseDT.Columns.Add("IN");
DirectoryInfo DirInfo = new DirectoryInfo(XMLPackagesDir);
DataRow INHReponseRow = INHReponseDT.NewRow();
foreach (FileInfo fi in DirInfo.GetFiles("*.*", SearchOption.AllDirectories))
{
XmlSerializer serializer = new XmlSerializer(typeof(Response));
Response i;
FileStream fs = null;
fs = new FileStream(Path.Combine(XMLPackagesDir, fi.Name), FileMode.Open);
using (TextReader tr = new StreamReader(fs))
{
i = (Response)serializer.Deserialize(tr);
INHReponseRow = INHReponseDT.NewRow();
INHReponseRow["XM"] = fi.Name;
INHReponseRow["Cl"] = i.ClientCorrelationID;
INHReponseRow["I"] = i.StatusInformation.StatusItem.MessageText;
INHReponseRow["INH"] = i.ResponseStatus;
INHReponseRow["IN"] = i.RequestProcessedTime.ToString();
INHReponseDT.Rows.Add(INHReponseRow);
}
//Insert into SQL Table
using (SqlBulkCopy s = new SqlBulkCopy(SqlConnection))
{
s.DestinationTableName = INTable;
s.BatchSize = INHReponseDT.Rows.Count;
s.WriteToServer(INHReponseDT);
s.Close();
}
}
using (SqlCommand cmdUpdateCaseInformationINHResponseSP = new SqlCommand("UpdateCaseSP", SqlConnection))
{
cmdUpdateCaseInformationINHResponseSP.CommandType = CommandType.StoredProcedure;
cmdUpdateCaseInformationINHResponseSP.ExecuteNonQuery();
}
}
命名你connecion''SqlConnection的是不是一個好主意。你有'try/catch'這個代碼嗎? – Fred
在您第一次叫你的名字''SqlCommand''cmdCreateINHResponseTableSP尚未使用使用塊內'cmdCreateITableSP' – Fred
所以在所有的「其他代碼」的錯誤。你應該找到它。由於您正在導入文件,所以一眼就能看出存在一些數據質量問題。你的Windows服務崩潰了嗎?您應該捕獲錯誤並將它們寫入Windows事件日誌。 –