我正在尝试将数据表作为参数传递到 sql 存储过程中。
我有一个助手类为我执行代码:
我正在使用的辅助类方法:
public int ExecNonQueryProc(string proc, params object[] args)
{
using (SqlCommand cmd = CreateCommand(proc, CommandType.StoredProcedure, args))
{
return cmd.ExecuteNonQuery();
}
}
public SqlCommand CreateCommand(string qry, CommandType type, params object[] args)
{
SqlCommand cmd = new SqlCommand(qry, _conn);
// Associate with current transaction, if any
if (_trans != null)
cmd.Transaction = _trans;
// Set command type
cmd.CommandType = type;
// Construct SQL parameters
for (int i = 0; i < args.Length; i++)
{
if (args[i] is string && i < (args.Length - 1))
{
SqlParameter parm = new SqlParameter();
parm.ParameterName = (string)args[i];
parm.Value = args[++i];
cmd.Parameters.Add(parm);
}
else if (args[i] is SqlParameter)
{
cmd.Parameters.Add((SqlParameter)args[i]);
}
else throw new ArgumentException("Invalid number or type of arguments supplied");
}
return cmd;
}
我的 SQL 存储过程:
ALTER PROCEDURE [dbo].[sp_insert_input_portfolio_metrics]
@TYPE_INPUT_PORTFOLIO_METRICS TYPE_INPUT_PORTFOLIO_METRICS readonly
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
delete from dbo.INPUT_PORTFOLIO_METRICS
where ID in (select ID from dbo.INPUT_PORTFOLIO_METRICS a
inner join @TYPE_INPUT_PORTFOLIO_METRICS b
on a.Portfolio = b.portfolio and a.Portfolio_Val_date = b.portfolio_val_date)
END
我是如何执行的:
private void simpleButton_UploadAll_Click(object sender, EventArgs e)
{
AdoHelper adocommand = new AdoHelper();
var dt = new DataTable();
dt = wizard_tables.PerformanceDetail.Copy();
adocommand.ExecNonQueryProc("sp_insert_input_portfolio_metrics",
"@TYPE_INPUT_PORTFOLIO_METRICS", dt);
}
我收到的错误:
不存在从对象类型 Risk_Performance_Platform.Datasets.DS_Wizard_Tables+PerformanceDetailDataTable 到已知托管提供程序本机类型的映射。
我试图按照 Navid Farhadi 关于 如何将数据表插入 SQL Server 数据库表中的示例进行操作?