C# 如何向存储过程发送不同的参数?

C# 如何向存储过程发送不同的参数?,c#,stored-procedures,jqgrid,C#,Stored Procedures,Jqgrid,我正在尝试开发动态jqgrid,其中所有字段都由数据库使用存储过程驱动,这很好,但当用户在jqgrid上编辑时,在保存特定记录方面我遇到了问题。下面是我的代码 Javascript C# [HttpPost] 公共JsonResult编辑(字符串数据) { System.Web.Script.Serialization.JavaScriptSerializer js=new System.Web.Script.Serialization.JavaScriptSeriali

我正在尝试开发动态jqgrid,其中所有字段都由数据库使用存储过程驱动,这很好,但当用户在jqgrid上编辑时,在保存特定记录方面我遇到了问题。下面是我的代码

Javascript C#

[HttpPost]
公共JsonResult编辑(字符串数据)
{            
System.Web.Script.Serialization.JavaScriptSerializer js=new System.Web.Script.Serialization.JavaScriptSerializer();
var formData=js。将(数据)反序列化为IDictionary;
List SQLParm=新列表();
foreach(formData中的var项)
{
如果(item.Key!=“oper”)
{
SqlParameter param=新的SqlParameter();
param.ParameterName=“@”+项.Key;
参数值=项目值;
SQLParm.Add(参数);
}
}
返回null;
}
现在由于动态jqgrid,我被卡住了,它返回各种数据,我创建了一个列表,然后将这些数据传递给SP,但问题是存储过程如何计算这些参数


是否有其他方法可以做到这一点?

假设您的数据是动态的,但您知道它可能是什么(否则,您的proc如何知道如何处理它?)。如果是这样,您可以使用表值参数[TVP](自定义用户表类型,其中包含过程可能需要接收的所有属性)。然后,您可以在发送到存储过程之前填充适用的属性

有关TVPs的更多信息:

用法:

    [HttpPost]
    public JsonResult Edit(string Data)
    {
    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
    var formData = js.Deserialize<object>(Data) as IDictionary<string, object>;

    List<SqlDataRecord> sqlDataRecords = new List<SqlDataRecord>(); 

    foreach (var item in formData)
    {
        // Create sql data record with appropriate properties populated and add to list if applicable
    }

    SqlParameter[] sqlParameters = new SqlParameter("YourTableTypeParamName", SqlDbType.Structured)
                        {
                            Value = sqlDataRecords,
                            TypeName = "dbo.YourTableType"
                        };

        SqlConnection conn = ... // define connection here
        try
        {
            using (SqlCommand command = conn.CreateCommand())
            {
                command.Connection = conn;
                conn.Open();

                command.CommandText = "YourStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddRange(sqlParameters);

                using (DbDataReader reader = command.ExecuteReader())
                {
                    // Do something with results
                }
            }
        }
        finally
        {
            conn.Close();
        }

    return null;
}
[HttpPost]
公共JsonResult编辑(字符串数据)
{
System.Web.Script.Serialization.JavaScriptSerializer js=new System.Web.Script.Serialization.JavaScriptSerializer();
var formData=js。将(数据)反序列化为IDictionary;
List sqlDataRecords=新列表();
foreach(formData中的var项)
{
//创建填充了适当属性的sql数据记录,并添加到列表(如果适用)
}
SqlParameter[]sqlParameters=新的SqlParameter(“YourTableTypeParamName”,SqlDbType.Structured)
{
Value=sqlDataRecords,
TypeName=“dbo.YourTableType”
};
SqlConnection conn=…//在此处定义连接
尝试
{
使用(SqlCommand=conn.CreateCommand())
{
command.Connection=conn;
conn.Open();
command.CommandText=“YourStoredProcedureName”;
command.CommandType=CommandType.storedProcess;
command.Parameters.AddRange(sqlParameters);
使用(DbDataReader=command.ExecuteReader())
{
//对结果做点什么
}
}
}
最后
{
康涅狄格州关闭();
}
返回null;
}

我不确定您的问题是否清楚。您是说您的SQL列和JSON值的类型不匹配吗?例如,JSON中填充了一个字符串,用于需要整数的列(反之亦然)?我认为,如果您将一个正确类型的模型传递给jqgrid,那么它应该在生成JSON时保持该类型。您得到的确切错误是什么?最终,您不应该期望任何传入的JSON都是有效的。您应该反序列化为强类型类,而不是字典。然后通过读取每个属性将该类保存到DB。通过这种方式,您可以确保您的SQL参数始终是正确的类型。感谢您的回复。基本上,我有带选项卡的jqgrid,每个选项卡都有不同的表,我使用存储过程获取这些数据,然后使用存储过程构建动态jqgrid,但我的问题是在保存特定记录并传回存储过程时希望可以处理所有表的更新,而不是装入不同的SP。希望这有帮助
[HttpPost]
public JsonResult Edit(string Data)
{            
   System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
   var formData = js.Deserialize<object>(Data) as IDictionary<string, object>;

   List<SqlParameter> SQLParm = new List<SqlParameter>(); 

   foreach (var item in formData)
   {
      if (item.Key != "oper")
      {
         SqlParameter param = new SqlParameter();
         param.ParameterName = "@" + item.Key;
         param.Value = item.Value;
         SQLParm.Add(param);
      }
   }
   return null;
}
    [HttpPost]
    public JsonResult Edit(string Data)
    {
    System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
    var formData = js.Deserialize<object>(Data) as IDictionary<string, object>;

    List<SqlDataRecord> sqlDataRecords = new List<SqlDataRecord>(); 

    foreach (var item in formData)
    {
        // Create sql data record with appropriate properties populated and add to list if applicable
    }

    SqlParameter[] sqlParameters = new SqlParameter("YourTableTypeParamName", SqlDbType.Structured)
                        {
                            Value = sqlDataRecords,
                            TypeName = "dbo.YourTableType"
                        };

        SqlConnection conn = ... // define connection here
        try
        {
            using (SqlCommand command = conn.CreateCommand())
            {
                command.Connection = conn;
                conn.Open();

                command.CommandText = "YourStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddRange(sqlParameters);

                using (DbDataReader reader = command.ExecuteReader())
                {
                    // Do something with results
                }
            }
        }
        finally
        {
            conn.Close();
        }

    return null;
}