将对象列表传递给SQL Server存储过程并使用asp.net核心插入记录

时间:2017-02-07 18:27:57

标签: c# sql-server ado.net

我正在开发asp.net应用程序。我使用ado.net访问SQL Server数据库并执行CRUD。我想一次插入多个记录。为此,我添加了一个自定义Type in数据库和一个存储过程,如下所示:

CREATE TYPE [dbo].[UserTableType] AS TABLE
(
    [FirstName] [nvarchar](100) NULL,
    [LastName] [nvarchar](100) NULL,
    [UserName] [nvarchar](100) NULL,
    [Password] [nvarchar](100) NULL,
    [IsActive] [bit] NULL,
    [IsDeleted] [bit] NULL,
    [CreatedBy] [int] NULL,
    [CreatedAt] [datetime] NULL,
    [UpdatedBy] [int] NULL,
    [UpdatedAt] [datetime] NULL,
    [Email] [nvarchar](100) NULL
)
GO

和存储过程是这样的:

CREATE PROCEDURE [dbo].[sp_InsertUser] 
    @UserTableType UserTableType READONLY
AS
BEGIN
    INSERT INTO tblUser
        SELECT * FROM UserTableType
END

在C#中,我正在使用此代码:

public void CreateMultipleUser()
{
    var cs = "Server=.;Database=Clicon;Trusted_Connection=True;";

    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("exec sp_InsertUser @UserTableType", con);            

        var pList = new SqlParameter("@UserTableType", SqlDbType.Structured);
        pList.TypeName = "dbo.UserTableType";
        pList.Value = GetAddUserList();              
        cmd.Parameters.Add(pList);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

和此函数创建一个SqlDataRecord的List并传递给存储过程:

private List<SqlDataRecord> GetAddUserList()
{
    List<SqlDataRecord> datatable = new List<SqlDataRecord>();
    SqlMetaData[] sqlMetaData = new SqlMetaData[11];
    sqlMetaData[0] = new SqlMetaData("FirstName", SqlDbType.VarChar, 100);
    sqlMetaData[1] = new SqlMetaData("LastName", SqlDbType.VarChar, 100);
    sqlMetaData[2] = new SqlMetaData("UserName", SqlDbType.VarChar, 100);
    sqlMetaData[3] = new SqlMetaData("Password", SqlDbType.VarChar, 100);
    sqlMetaData[4] = new SqlMetaData("isActive", SqlDbType.Bit);
    sqlMetaData[5] = new SqlMetaData("isDeleted", SqlDbType.Bit);
    sqlMetaData[6] = new SqlMetaData("CreatedBy", SqlDbType.Int);
    sqlMetaData[7] = new SqlMetaData("CreatedAt", SqlDbType.DateTime);
    sqlMetaData[8] = new SqlMetaData("UpdatedBy", SqlDbType.Int);
    sqlMetaData[9] = new SqlMetaData("UpdatedAt", SqlDbType.DateTime);
    sqlMetaData[10] = new SqlMetaData("Email", SqlDbType.VarChar, 100);
    SqlDataRecord row = new SqlDataRecord(sqlMetaData);

    row = new SqlDataRecord(sqlMetaData);
    row.SetValues(new object[] { "Asif", "hameed", "asif371", "123", true,false,1,DateTime.Now,1,DateTime.Now, "asif1@gmail.com" });
    datatable.Add(row);
    row = new SqlDataRecord(sqlMetaData);
    row.SetValues(new object[] { "Asif1", "hameed1", "asif372", "123", true, false, 1, DateTime.Now, 1, DateTime.Now, "asif2@gmail.com" });
    datatable.Add(row);
    row = new SqlDataRecord(sqlMetaData);
    row.SetValues(new object[] { "Asif2", "hameed2", "asif373", "123", true, false, 1, DateTime.Now, 1, DateTime.Now, "asif3@gmail.com" });
    datatable.Add(row);
    row = new SqlDataRecord(sqlMetaData);
    row.SetValues(new object[] { "Asif4", "hameed4", "asif375", "123", true, false, 1, DateTime.Now, 1, DateTime.Now, "asif5@gmail.com" });

    datatable.Add(row);
    row = new SqlDataRecord(sqlMetaData);
    row.SetValues(new object[] { "Asif5", "hameed4", "asif376", "123", true, false, 1, DateTime.Now, 1, DateTime.Now, "asif6@gmail.com" });

    datatable.Add(row);

    return datatable;
}

但我得到了这个例外:

  

类型&#39; System.Data.SqlClient.SqlException&#39;的异常发生在System.Data.SqlClient.dll中但未在用户代码中处理

     

其他信息:无效的对象名称&#39; UserTableType&#39;

请建议解决此问题。

1 个答案:

答案 0 :(得分:3)

您正在从“类型”定义中进行选择,而不是参数。试试这个:

CREATE PROCEDURE [dbo].[sp_InsertUser] 
    -- Add the parameters for the stored procedure here
    @UserTableType UserTableType READONLY

AS
BEGIN
    INSERT INTO tblUser
        SELECT * FROM @UserTableType
END