好吧,我一直在看一堆其他的问题,但似乎无法在这里找到正确的答案...Windows服务无法运行存储过程
我写一窗口服务C#使用存储过程将一些数据上传到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'来代替? –
我有,是的。无济于事。 –