访问表值参数

时间:2014-04-16 19:04:08

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

我有一个关于如何将多个表参数发送到存储过程的问题。

create table dbtest.Company_verified([CompanyAddr] varchar NULL,[ID] [int] NULL主键,[Address1] varchar NULL,[city] varchar NULL,         [state] varchar NULL,[zip] varchar NULL,         [country] varchar NULL,[updDate] [datetime] NULL     )

create table dbtest.detailaddrTypes([ID] [int] NULL外键引用dbtest.Company_verified(ID),         [updDate] [datetime] NULL,         [type] [int] NULL     )     创建过程dbo.usp_Insert_test     @MainObj as dbtest.Info_Verified readonly,@ DetailObj as dbtest.DetailInfo_Verified readonly

CREATE TYPE dbtest.Info_Verified AS TABLE(
    [CompanyAddr] [varchar](1000) NULL,
                [ID] [int] NULL,
    [Address1] [varchar](200) NULL,
    [city] [varchar](100) NULL,
    [state] [varchar](100) NULL,
    [zip] [varchar](50) NULL,
    [country] [varchar](50) NULL,
    [updDate] [datetime] NULL
)
CREATE TYPE [dbtest].[AddrTypes] AS TABLE(
    [ID] [int] NULL,
    [updDate] [datetime] NULL,
    [type] [int] NULL
)

C#控制台程序尝试通过调用dbo.usp_Insert_test过程一次插入10000个Company_verified记录。每个Company_verified记录可能有多个detailaddrTypes记录。为此,我创建了这些类

public class AddressVerifiedTbl
    {
        public string CompanyAddr { get; set; }
        public string id { get; set; }
        public string Address1 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string country { get; set; }
        public DateTime updDate { get; set; }
    }
 public class AddrComponentsTbl
    {
        public string id { get; set; }
        public DateTime updDate { get; set; }
        public string type { get; set; }
    }
public class Program
{
 private static void insertAddrVerifiedComp(List<VerifiedTbl> newMaintbl, (List<DetailTbl> newDetailtbl)
         {

            DataSet ds = new DataSet();
            DataTable dtAddrVerified = CreateDatatableAddrVerified();
            DataTable dtAddrVerified1 = dtAddrVerified.Clone();
            foreach (AddressVerifiedTbl tmp in newMaintbl)
            {
                DataRow drAddr = dtAddrVerified1.NewRow();
                drAddr["CompanyAddr"] = tmp.CompanyAddr;
                drAddr["id"] = tmp.id;
                drAddr["Address1"] = tmp.Address1;
                drAddr["city"] = tmp.city;
                drAddr["state"] = tmp.state;
                drAddr["zip"] = tmp.zip;
                drAddr["country"] = tmp.country;
                drAddr["updDate"] = tmp.updDate;
                dtAddrVerified1.ImportRow(drAddr);
                dtAddrVerified1.Rows.Add(drAddr);

            }
                string connectionString = GetConnectionString();
                SqlConnection connection =
                            new SqlConnection(connectionString);
                connection.Open();
                SqlCommand cmd = new SqlCommand(
                    "dbo.usp_Insert_test", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 120;
                SqlParameter parameter1 = new SqlParameter(); 
                parameter1.ParameterName = "@MainObj";
                SqlParameter parameter2 = new SqlParameter(); 
                parameter2.ParameterName = "@DetailObj";
                parameter1.SqlDbType = System.Data.SqlDbType.Structured;
                parameter1.Value = dtAddrVerified;
                parameter1.TypeName = "dbtest.Info_Verified";
                cmd.Parameters.Add(parameter1); 
               connection.Close();
            }
private static DataTable CreateDatatableAddrVerified()
        {
            DataTable myDataTable = new DataTable();
            DataColumn myDataColumn;
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "CompanyAddr";
            myDataTable.Columns.Add(myDataColumn);
           // added other columns)

}

在方法insertAddrVerifiedComp for循环中,我对如何继续而感到遗憾。任何建议。

由于 [R

0 个答案:

没有答案