如何在实体框架中使用表值和标量参数调用存储过程

我正在尝试调用具有两个参数的存储过程:

  • ID :int

  • 数据 :table user defined type

我想使用实体框架调用这个存储过程。

请帮我。


倚天杖
浏览 127回答 1
1回答

蝴蝶刀刀

请检查以下代码:public DataTable CallStoredProcedure(System.String procedureName, Dictionary<string, object> parameters)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cmd = CreateStoredProcCallCommand(procedureName, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < parameters.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var param = parameters.ElementAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (param.Value.GetType() == typeof(DataTable))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddTableValuedParameter(cmd, "@" + param.Key, 0, ParameterDirection.Input, param.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddParameter(cmd, "@" + param.Key, 2147483647, ParameterDirection.Input, param.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var toReturn = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateAndSetupAdapter(cmd).Fill(toReturn);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return toReturn;&nbsp; &nbsp; &nbsp; &nbsp; }private DbCommand CreateStoredProcCallCommand(string storedProcedureToCall, bool openConnection)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cmd = _factoryToUse.CreateCommand();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandType = CommandType.StoredProcedure;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandText = storedProcedureToCall;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return SetupCommand(cmd, openConnection);&nbsp; &nbsp; &nbsp; &nbsp; }private DbDataAdapter CreateAndSetupAdapter(DbCommand selectCommand){&nbsp; &nbsp; var adapter = _factoryToUse.CreateDataAdapter();&nbsp; &nbsp; adapter.SelectCommand = selectCommand;&nbsp; &nbsp; return adapter;}&nbsp;private static void AddTableValuedParameter(DbCommand cmd, string parameterName, int length, ParameterDirection direction, object value)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlParameter param = new SqlParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.ParameterName = parameterName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.SqlDbType = SqlDbType.Structured;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.Size = length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.Value = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; param.Direction = direction;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(param);&nbsp; &nbsp; &nbsp; &nbsp; }private static void AddParameter(DbCommand cmd, string parameterName, int length, ParameterDirection direction, object value)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dummyParam = new EntityParameter() { Value = value };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var parameter = cmd.CreateParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter.ParameterName = parameterName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter.Direction = direction;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter.Size = length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter.Value = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter.DbType = dummyParam.DbType;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(parameter);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP