如何使用Microsoft.Practices.EnterpriseLibrary将类类型数组传递到SQL Server数据库

时间:2017-03-24 13:39:16

标签: asp.net-mvc

使用Enterprise Library(版本= 6.0.0.0)在VS2015 MVC项目中工作。我需要将类型数组作为参数发送到SQL存储过程。

public static Database CoreDB
    {
        get
        {
            return _coreDatabase;
        }
    }

    public BaseRepository()
    {
        DatabaseProviderFactory factory = new DatabaseProviderFactory();
        Database coreDB = factory.Create("DB_Name");
        _coreDatabase = coreDB;
    }

在视图模型中,我创建了类abc的数组,其中包含要插入数据库的ID的集合。

public class viewModelClass
{
  public abc[] Details { get; set; }
  public int param1 { get; set; }
  public int param2 { get; set; }
  public int param3 { get; set; }
}

public class abc
{
    public int ID { get; set; }

    public string Name { get; set; }

    public Boolean IsActive { get; set; }

}

POST后,我将值从模型传递给存储过程(我在SQL服务器中创建了一个用户定义的表类型来处理这个数组参数)。

 public int CreateAdminUser(viewModelClass userInfo)
    {
        string insertUserSP = Resources.InsertUser;
        DbCommand cmd = CoreDB.GetStoredProcCommand(insertUserSP);
        CoreDB.AddInParameter(cmd, "@param1", DbType.Int32, userInfo.param1);
        CoreDB.AddInParameter(cmd, "@param2", DbType.Int32, userInfo.param2);
        CoreDB.AddInParameter(cmd, "@param3", DbType.Int32, userInfo.param3);

        SqlParameter temp = new SqlParameter("@details", userInfo.Details);
        temp.SqlDbType = SqlDbType.Structured;
        temp.TypeName = "dbo.UserDefinedTypeName";
        cmd.Parameters.Add(temp);

        int result = Convert.ToInt32(CoreDB.ExecuteNonQuery(cmd));
        return result;
    }

将异常作为"无法将参数值从abc []转换为IEnumerable' 1"和内部异常作为"对象必须实现IConvertible"执行ExecuteNonQuery()时。

我的问题是将此UserInfo.Details数组作为参数传递给sql server。

这是我的桌子类型......

CREATE TYPE [dbo].[UserDefinedTypeName] AS TABLE( 
  [Id] [int] NOT NULL, 
  [Name] [varchar](200) NULL, 
  [IsActive] [bit] NULL )

1 个答案:

答案 0 :(得分:1)

您无法将类对象数组传递给表类型的参数。您需要创建一个dataTable并使用要传递给

的数据填充它

考虑一下。

public int CreateAdminUser(viewModelClass userInfo)
{
    //Creating table with the same structure as defined table type in SQL Server
    var table = new DataTable();
    var dataColumn = new DataColumn("Id", typeof(int));
    table.Columns.Add(dataColumn);

    dataColumn = new DataColumn("Name", typeof(string));
    table.Columns.Add(dataColumn);

    dataColumn = new DataColumn("IsActive", typeof(bool));
    table.Columns.Add(dataColumn);

    // Populating table from the userInfo.Details array
    foreach (var item in userInfo.Details)
    {
        var row = table.NewRow();

        row["Id"] = item.ID;
        row["Name"] = item.Name;
        row["IsActive"] = item.IsActive;

        table.Rows.Add(row);
    }

    string insertUserSP = Resources.InsertUser;
    DbCommand cmd = CoreDB.GetStoredProcCommand(insertUserSP);
    CoreDB.AddInParameter(cmd, "@param1", DbType.Int32, userInfo.param1);
    CoreDB.AddInParameter(cmd, "@param2", DbType.Int32, userInfo.param2);
    CoreDB.AddInParameter(cmd, "@param3", DbType.Int32, userInfo.param3);

    SqlParameter temp = new SqlParameter("@details", SqlDbType.Structured);
    temp.TypeName = "dbo.UserDefinedTypeName";
    temp.Value = table;
    cmd.Parameters.Add(temp);

    int result = Convert.ToInt32(CoreDB.ExecuteNonQuery(cmd));
    return result;
}

这可以解决您的问题。