【发布时间】:2016-08-23 15:20:24
【问题描述】:
好吧,我一直在研究一堆其他问题,但似乎无法在此处找到正确答案...
我正在用 C# 编写一个 Windows 服务来运行一些使用存储过程上传到 SQL Server Express 的数据。该过程相当长,但它包括将数据从 TSV 读取到临时表,然后更新该数据并将其插入到单独的永久表中。目前,存储过程由在 Windows 任务调度程序上运行的批处理文件触发。我正在尝试过渡到可以完成这项工作以及更多其他工作的 Windows 服务。
我遇到的问题是这个存储过程不会从服务中运行。这是我用于服务的调用:
SqlConnection conn = new SqlConnection("user id=reports; Password=*****; Integrated Security=false;" +
"database=data_warehouse; Server=(serverName); ");
try
{ conn.Open();
}
catch (Exception ex)
{
Library.WriteErrorLog(ex);
}
SqlCommand GetSalesCount = new SqlCommand("select count (*) from [sales data] where EventEndDate = convert(date,getdate())");
GetSalesCount.CommandTimeout = 0;
GetSalesCount.Connection = conn;
int? preCount = (int?)GetSalesCount.ExecuteScalar();
Library.WriteErrorLog("started with " + preCount.ToString() + " records.");
SqlCommand sql = new SqlCommand("exec LoadSalesData", conn);
sql.CommandTimeout = 0;
try
{
sql.ExecuteNonQuery();
}
catch (Exception ex)
{
Library.WriteErrorLog(ex);
}
// Getting the nuber of sales afterward
SqlCommand getOffloadCount = new SqlCommand("Select count (*) from [sales data] where EventEndDate = convert(date,getdate())", conn);
int? post = (int?)getOffloadCount.ExecuteScalar();
Library.WriteErrorLog("Had " + post.ToString() + " records at End");
conn.Close();
现在,这不起作用。代码块完成,但没有记录更新,即存储过程没有运行。如果我保存executeNonQuery 的结果,我得到(-1)。如果我使用相同的凭据从 smss 运行相同的exec LoadSalesData,它可以正常工作,最多可能需要 30 秒来处理。
我可以用另一个存储过程替换存储过程,它只是在相同的时间内循环向表中添加行,它运行得很好。
我认为这可能与从服务而不是 ssms 运行的请求有关?权限?我不知道去哪里找。
任何想法都会很棒。谢谢。
【问题讨论】:
-
您是否尝试过从您的命令中删除
exec并改用sql.CommandType = CommandType.StoredProcedure? -
我有,是的。无济于事。
标签: c# windows stored-procedures service sqlclient