【发布时间】: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