【问题标题】:What is the execute command to use here这里使用的执行命令是什么
【发布时间】:2015-06-15 08:56:21
【问题描述】:
protected void Page_Load(object sender, EventArgs e)
    {
        string sqlConnectionString = @"Data Source=phriz-webapp01;Initial Catalog=PFTracking;Integrated Security=True";
        string script = "if not exists(select * from sys.servers where name=N'CNCTC-WEB01')begin exec sp_addlinkedserver @server='CNCTC-WEB01'exec sp_addlinkedsrvlogin 'CNCTC-WEB01','false',null,'svc_Phils','Apple@6' end INSERT INTO [PFTracking].[dbo].[TempTable] SELECT c.[pf_id],a.[RequestDate],c.[pf_carrierUsed],b.[PiecePrice] * b.[PartQuantity] as [Amount] ,c.[pf_type],c.[pf_resSupplier] ,c.[pf_resCustomer],c.[pf_trailerNum] ,b.[PartDesc]  ,c.[pf_chargeBack]  ,c.[pf_chargetoPlant] FROM [CNCTC-WEB01].[NOP_PR].[dbo].[Requests] a JOIN [CNCTC-WEB01].[NOP_PR].[dbo].[Parts] b on a.[RequestID] = b.[RequestID] JOIN [PHRIZ-WEBAPP01].[PFTracking].[dbo].[Tbl_PFExcel] c on  b.[PartNumber] like '%'+c.pf_id+'%'where a.[EntityName] like '%PTA' AND a.[RequestDate] between '2015-04-20 00:00:00.000' AND GETDATE() ";
        SqlConnection conn = new SqlConnection(sqlConnectionString);
        conn.Open();
        SqlCommand comm = new SqlCommand(script, conn);
        comm.ExecuteNonQuery();
        conn.Close();
    }

我想对服务器执行这个命令,以便每次访问页面时加载数据库。

comm.ExecuteNonQuery()

补充:

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

附加信息:超时已过期。操作完成前超时时间已过或服务器未运行 回应。`

Line 21:             conn.Open();
Line 22:             SqlCommand comm = new SqlCommand(script, conn);
Line 23:             **comm.ExecuteNonQuery();**
Line 24:             conn.Close();
Line 25:         }

这就是它给出的错误..有什么帮助吗?

【问题讨论】:

  • 问题似乎是由于超时,尝试使用 comm.CommandTimeout = ...; 增加默认超时您还应该检查工作室经理,以了解命令格式是否正确以及执行需要多长时间
  • 请将您的 SQL 命令打包到存储过程中!!!!
  • @Simone 我将在我的 comm.CommandTimeout 中投入多少?
  • @ughai 服务器没有关闭.. 我现在正在使用它。
  • @Anaiah 我已经添加了一个答案供您尝试 - 它有 try catch 和 CommandTimeout

标签: sql sql-server sql-server-2008 tsql


【解决方案1】:

看看这个并告诉我这是否适合你 - 我添加了一个 try catch 来检查命令是否超时。

只是一些提示:

我总是会使用 try catch,因为它会准确地告诉您需要知道什么以及为什么您的代码无法运行。

我也会一直使用using 语句,因为using 语句只是确保释放非托管资源,它们无法处理异常。

另外,SqlCommand 实现了IDisposable,所以我建议也将它放在 using 块中。

protected void Page_Load(object sender, EventArgs e)
{
   using (SqlConnection conn = new SqlConnection("Data Source=phriz-webapp01;Initial Catalog=PFTracking;Integrated Security=True";))
   {
      conn.Open();
      string script = "if not exists(select * from sys.servers where name=N'CNCTC-WEB01')begin exec sp_addlinkedserver @server='CNCTC-WEB01'exec sp_addlinkedsrvlogin 'CNCTC-WEB01','false',null,'svc_Phils','Apple@6' end INSERT INTO [PFTracking].[dbo].[TempTable] SELECT c.[pf_id],a.[RequestDate],c.[pf_carrierUsed],b.[PiecePrice] * b.[PartQuantity] as [Amount] ,c.[pf_type],c.[pf_resSupplier] ,c.[pf_resCustomer],c.[pf_trailerNum] ,b.[PartDesc]  ,c.[pf_chargeBack]  ,c.[pf_chargetoPlant] FROM [CNCTC-WEB01].[NOP_PR].[dbo].[Requests] a JOIN [CNCTC-WEB01].[NOP_PR].[dbo].[Parts] b on a.[RequestID] = b.[RequestID] JOIN [PHRIZ-WEBAPP01].[PFTracking].[dbo].[Tbl_PFExcel] c on  b.[PartNumber] like '%'+c.pf_id+'%'where a.[EntityName] like '%PTA' AND a.[RequestDate] between '2015-04-20 00:00:00.000' AND GETDATE() ";

      using (var comm = new SqlCommand(script, conn))
      {
        // Setting command timeout to 2 minutes
        comm.CommandTimeout = 120;
        try 
        {
           command.ExecuteNonQuery();
        }
        catch (SqlException e) 
        {
           Console.WriteLine("Got expected SqlException due to command timeout ");
           Console.WriteLine(e);
        }
      }
      conn.Close();
   }
}

【讨论】:

  • @Anaiah 检查我的更新答案,我刚刚为您添加了一些提示
  • 感谢您的提示..!这都注意到了!
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2016-08-06
相关资源
最近更新 更多