本文介绍了从单独的方法/服务中分离运行SqlCommand以解决超时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我从excel到sql上传了一个上传函数,它在执行的最后一个存储过程中收到超时。 Hi All,I made an upload function from excel to sql that receives a timeout on the last stored procedure it executes.new SqlCommand(query, con).ExecuteNonQuery(); - >这会调用超时。 这是我所期待的,因为存储过程需要几分钟才能完成。 我需要的是可以从网页调用该函数,并且该函数将在单独的服务中运行,以便用户在处理函数时仍然可以工作。 例如: --> this invokes a timeout.It's something I was expecting cause the stored procedure takes a few minutes before it's done.What I need is that the function can be called from a webpage, and will run in a seperate service so that the user can still work while the function is processing.As example:public static void Upload(string filePath, string tagName, string user){ var query = "Exec ExcelImport.sp_ExcelImport '" + filePath + "', '" + user + "', '" + tagName + "'"; var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataSourceConnectionString"].ConnectionString); con.Open(); new SqlCommand(query, con).ExecuteNonQuery(); con.Close();} 这应该单独运行而不返回超时,如果可能的话,返回一个已完成或失败的答案。 b $ b 我最好的方法是什么? 提前致谢。 This should run seperate without returning a timeout, and if possible return an answer that it's done or failed.What would be my best approach?Thanks in advance.推荐答案 你可以试试这个... public static void Upload(string filePath,string tagName,string user) { var query =Exec ExcelImport.sp_ExcelImport'+ filePath + ','+ user +','+ tagName +'; var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [DataSourceConnectionString]。 ConnectionString); con.Open(); SqlCommand com = new SqlCommand(query,con) com.Timeout = 0; com.ExecuteNonQuery(); con.Close(); } Hi,You can try this one...public static void Upload(string filePath, string tagName, string user){ var query = "Exec ExcelImport.sp_ExcelImport '" + filePath + "', '" + user + "', '" + tagName + "'"; var con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DataSourceConnectionString"].ConnectionString); con.Open();SqlCommand com = new SqlCommand(query, con)com.Timeout = 0;com.ExecuteNonQuery(); con.Close();} 通过添加代码abhishek_singh向我提供了超时解决。 但是我还添加了一个线程,所以它现在可以与其余部分分开运行。 By Adding the code that abhishek_singh provided me the timeout was resolved.But I also added a thread so it could run seperate from the rest now.protected void btnUpload_Click(object sender, EventArgs e){ if (excelUpload.HasFile) { string fileName = Path.GetFileName(excelUpload.PostedFile.FileName); string fileLocation = Server.MapPath("~/App_Data/" + fileName); excelUpload.SaveAs(fileLocation); var thread = new Thread(() => Performthread(fileLocation, fileName)); thread.Start(); }}private void Performthread(string fileLocation, string fileName){ ExcelService.Upload(fileLocation, "BILLING", "tom"); File.Delete(Server.MapPath("~/App_Data/" + fileName)); _syncId = 0;} 这篇关于从单独的方法/服务中分离运行SqlCommand以解决超时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 16:46