Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何使用Microsoft.Practices.EnterpriseLibrary将类类型数组传递到SQL server数据库_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 如何使用Microsoft.Practices.EnterpriseLibrary将类类型数组传递到SQL server数据库

Asp.net mvc 如何使用Microsoft.Practices.EnterpriseLibrary将类类型数组传递到SQL server数据库,asp.net-mvc,Asp.net Mvc,我在VS2015 MVC项目中使用企业库(版本=6.0.0.0)。我需要将类类型数组作为参数发送到SQL存储过程 public static Database CoreDB { get { return _coreDatabase; } } public BaseRepository() { DatabaseProviderFactory factory = new Data

我在VS2015 MVC项目中使用企业库(版本=6.0.0.0)。我需要将类类型数组作为参数发送到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之后,我将值从模型传递到存储过程(我在SQLServer中创建了一个用户定义的表类型,用于处理这个数组参数)

执行ExecuteOnQuery()时,获取异常为“无法将参数值从abc[]转换为IEnumerable'1”,获取内部异常为“对象必须实现IConvertible”

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

这是我的桌子类型

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

不能将类对象数组传递给表类型的参数。您需要创建一个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;
}

这应该可以解决您的问题。

您错过了
userInfo.param1
-
userInfo.param3
的类型。另外,
userInfo.abc
也不存在(我猜你指的是细节)。那个存储过程的签名是什么?实际上,公共类viewModelClass{public abc[]Details{get;set;}public int param1{get;set;}public string param2{get;set;}public int param3{get;set;}}和SqlParameter temp=new SqlParameter(@Details),userInfo.Details);我的问题是将此UserInfo.Details数组作为参数传递给sql Server这是我的表类型。。。将类型[dbo].[UserDefinedTypeName]创建为表([Id][int]NOT NULL,[Name][varchar](200)NULL,[IsActive][bit]NULL),以后请编辑您的帖子,而不是在评论中添加代码和信息(您可以留下一条更新后的评论)。代码很难在注释中读取,信息可能会丢失。我已经为你在帖子中添加了你的评论。
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;
}