[英]System.ArgumentException: The table type parameter must have a valid type name
I am trying to pass in a user defined table type into a query in C#. 我试图将用户定义的表类型传递给C#中的查询。
the type is defined with 2 columns (org and sub org) 类型定义为2列(org和sub org)
this is what my code looks like: 这就是我的代码:
DataSet ds = new DataSet();
try
{
DataTable FilteredOrgSubOrg = new DataTable("OrgSubOrgValueType");
FilteredOrgSubOrg.Columns.Add("org", typeof(string));
FilteredOrgSubOrg.Columns.Add("subOrg", typeof(string));
FilteredOrgSubOrg.Rows.Add(org, orgsub);
using (SqlConnection conn = new SqlConnection(cCon.getConn()))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText =
"select * from myTable ex where year = @year' and qtr = @qtr" +
" and EXISTS(SELECT 1 FROM @OrgSubOrg tt WHERE ex.org like tt.org" +
" AND ex.orgsub = tt.suborg )"+
" order by ex.org,year, qtr DESC";
// 2. set the command object so it knows
// to execute a stored procedure
// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@OrgSubOrg", FilteredOrgSubOrg));
cmd.Parameters.Add(new SqlParameter("@year", year));
cmd.Parameters.Add(new SqlParameter("@qtr", qtr));
conn.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = cmd;
sqlDA.Fill(ds);
}
}
am i passing the parameters in incorrectly? 我错误地传递了参数吗?
when i do it in SQL server like so: 当我在SQL服务器中这样做时:
declare @OrgSubOrg OrgSubOrgValueType
insert into @OrgSubOrg values ('05%','00000000')
insert into @OrgSubOrg values ('03%','00000000')
------------ complete -----------------------------------
select * from myTable ex
where
year = '2013' and qtr = '1'
and EXISTS(
SELECT 1
FROM @OrgSubOrg tt
WHERE ex.org like tt.org
AND ex.orgsub = tt.suborg )
order by ex.org,year, qtr DESC
everything works like it should.
i also tried passing it in like so: 我也试过这样传递它:
SqlParameter p = cmd.Parameters.Add(new SqlParameter("@OrgSubOrg", SqlDbType.Structured));
p.Value = FilteredOrgSubOrg;
but am getting the same error 但我得到了同样的错误
The table type parameter '@OrgSubOrg' must have a valid type name.
could it be that i can't pass it to a SQL command, i have similar code in another place, that works great with a stored procedure...? 可能是我不能将它传递给SQL命令,我在另一个地方有类似的代码,这对存储过程很有用......?
Set mapping to your type in SqlServer using TypeName property that: Gets or sets the type name for a table-valued parameter, that has to fix . 使用TypeName属性在SqlServer中设置映射到您的类型:获取或设置必须修复的表值参数的类型名称。
p.TypeName = "dbo.MyType";
Check as well Table-Valued Parameters post 检查表格值参数后
Note that this may also happen when you're executing a stored procedure and you don't have the SqlCommand.CommandType set to CommandType.StoredProcedure, as such: 请注意,当您执行存储过程并且没有将SqlCommand.CommandType设置为CommandType.StoredProcedure时,也可能发生这种情况,如下所示:
using (SqlCommand cmd = new SqlCommand("StoredProcName", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
You can get this error also when you wanna pass table params into stored procedure. 当您想将表参数传递给存储过程时,也可以获得此错误。 There is happen if you use entity famework Context.Database.SqlQuery().
如果使用实体famework Context.Database.SqlQuery(),就会发生这种情况。 You must necessary set TypeName property for your table params.
您必须为表params设置TypeName属性。
SqlParameter codesParam = new SqlParameter(CODES_PARAM, SqlDbType.Structured);
SqlParameter factoriesParam = new SqlParameter(FACTORIES_PARAM, SqlDbType.Structured);
codesParam.Value = tbCodes;
codesParam.TypeName = "[dbo].[MES_CodesType]";
factoriesParam.Value = tbfactories;
factoriesParam.TypeName = "[dbo].[MES_FactoriesType]";
var list = _context.Database.SqlQuery<MESGoodsRemain>($"{SP_NAME} {CODES_PARAM}, {FACTORIES_PARAM}"
, new SqlParameter[] {
codesParam,
factoriesParam
}
).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.