表值参数 - 超时

时间:2014-04-28 21:07:37

标签: c# sql-server table-valued-parameters

我已经实现了从C#程序填充的Table值参数,并使用存储过程最终存储在表中。我发现,如果我一次发送75条记录,我就没有时间,或者我没有。如何一次插入更多记录?

- 选择TOP 75 master recs where ....&每个主人都有几个细节记录

- 创建了Collection的列表并添加了这75个rec和他们的详细recs并称为insertComp。

private static void insertComp(List<MasterTbl> newMaintbl,List<DetailTbl> newDetailtbl)
      {
          Int32 rowsAffected = 0;
          try
          {
              DataSet ds = new DataSet();
              DataTable dtMasterVerified = CreateDatatableMaster();
              DataTable dtMasterVerified1 = dtMasterVerified.Clone();
              DataTable dtDetail = CreateDatatableDetail();
              DataTable dtDetail1 = dtDetail.Clone();
              foreach (AddressVerifiedTbl tmp in newMaintbl)
              {
                  DataRow drMaster = dtMasterVerified1.NewRow();
                  drMaster["ID"] = tmp.ID;
                  drMaster["Address1"] = tmp.Address1;
                  drMaster["Address2"] = tmp.Address2;
                  drMaster["city"] = tmp.city;
                  drMaster["state"] = tmp.state;
                  drMaster["zip"] = tmp.zip;
                  drMaster["country"] = tmp.country;
                  drMaster["Status"] = tmp.Status;
                  drMaster["errormsg"] = tmp.errormsg;

                  foreach (DetailTbl tmpDetail in newDetailtbl)
                  {
                      DataRow drDetail = dtDetail1.NewRow();
                      drDetail["ID"] = tmpDetail.ID;
                      drDetail["name"] = tmpDetail.name;
                      drDetail["updDate"] = tmpDetail.updDate;
                      dtDetail1.ImportRow(drDetail);
                      dtDetail1.Rows.Add(drDetail);
                  }
                  dtDetail1.ImportRow(drMaster);
                  dtDetail1.Rows.Add(drMaster);
              }

              string connectionString = GetConnectionString();
              using (SqlConnection connection = new SqlConnection(connectionString))
              {
                  connection.Open();
                  SqlCommand cmd = new SqlCommand("dbo.uspInsert_temp_AllRecs", connection);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.CommandTimeout = 2000;
                  SqlParameter parameter = new SqlParameter();
                  parameter.ParameterName = "@MasterObj";
                  parameter.SqlDbType = System.Data.SqlDbType.Structured;
                  parameter.Value = dtMasterVerified1;
                  parameter.TypeName = "db.Master_Verified";
                  cmd.Parameters.Add(parameter);

                  SqlParameter parameter2 = new SqlParameter();
                  parameter2.ParameterName = "@DetailObj";
                  parameter2.SqlDbType = System.Data.SqlDbType.Structured;
                  parameter2.Value = dtDetail1;
                  parameter2.TypeName = "db.Detail_verified";
                  cmd.Parameters.Add(parameter2);

                  try
                  {
                      rowsAffected = cmd.ExecuteNonQuery();
                  }
                  catch (Exception ep)
                  {   
                     Console.WriteLine(ep.Message);
                  }
                 }
              }
          catch (Exception e)
          {
             Console.WriteLine(e.StackTrace);
           }
          }


 CREATE PROCEDURE [db].[uspInsert_temp_AllRecs]
@MasterObj Master_Verified Readonly, @DetailObj Detail_verified Readonly
AS
 INSERT INTO db.temp_Master_tbl (....)
SELECT * from @MasterObj
 INSERT INTO db.temp_Detail_tbl (....)
Select * from @DetailObj

由于 [R

1 个答案:

答案 0 :(得分:0)

我刚遇到这个问题而且由于缺乏信息我不能确定我们有完全相同的问题,但我会在这里发布并希望它有所帮助。

我在联接中使用了我的表值参数。当我将它移动到存在语句时,超时就消失了。我希望除了预感查询规划器的工作原理之外,我还有更好的理由。

原始 - 导致超时:

select t.* 
from dbo.Table1 as t1 
inner join @table as t2 on t1.val = t2.val

修改 - 作品:

select t.* 
from dbo.Table1 as t1 
where exists(select null from @table where t2.val = t1.val)