我在数据库中声明了以下内容:
CREATE TYPE [dbo].[QuestionList] AS TABLE (
[QuestionUId] UNIQUEIDENTIFIER NULL);
我在同一个数据库中使用这样的数据类型:
CREATE PROCEDURE dbo.sp_ins
@Title NVARCHAR (100) ,
@Questions QuestionList READONLY,
@TopicId INT
AS
BEGIN
在我的C#调用程序中,我有:
DataTable questions = new DataTable("QUIDs");
questions.Columns.Add("testId", typeof(Guid));
foreach (Guid id in tqh) {
questions.Rows.Add(id);
}
我用以下方法调用存储过程:
parameterList.Add(new SqlParameter ("@Questions", questions));
问题是我收到以下消息:
exceptionMessage=The table type parameter '@Questions' must have a valid type name.
有谁能让我知道我可能做错了什么?
答案 0 :(得分:2)
问题是我在SqlParameter上缺少指定TypeName。我纠正了这个:
var p = new SqlParameter ("@Questions", questions);
p.TypeName = "dbo.QuestionList";
答案 1 :(得分:0)
您可以通过以下查询从SSMS执行存储过程,没有任何问题。
DECLARE @Questions QuestionList
INSERT INTO @Questions( QuestionUId )VALUES (NEWID())
EXEC dbo.sp_ins N'', @Questions, 0
您还可以通过以下代码(示例)执行C#代码中的存储过程:
将数据包含在DataTable中:
public class Record
{
public Guid QuestionUId { get; set; }
}
private DataTable CreateDataTable(IEnumerable<Record> records)
{
var table = new DataTable();
table.Columns.Add("QuestionUId", typeof(Guid));
foreach (Record record in records)
{
table.Rows.Add(record.QuestionUId);
}
return table;
}
表值SP调用者
public static IEnumerable<Record> TableValuedSPCaller(DataTable inputTable)
{
using (var context = new MyContext(MyConnectionString))
{
var tableParam = new SqlParameter
{
ParameterName="@Questions",
Value = inputTable,
TypeName = "dbo.QuestionList"
};
return context.Database.SqlQuery<Record/*ResultType*/>
("dbo.sp_ins @Questions", tableParam);
}
}
调用表值SP:
var records = new List<Record>
{
new Record{QuestionUId=Guid1},
new Record{QuestionUId=Guid1},
new Record{QuestionUId=Guid1}
};
DataTable tableParameter = CreateDataTable(records);
var result = TableValuedSPCaller (tableParameter);
答案 2 :(得分:0)
您可以在存储过程中尝试以下一次,
CREATE PROCEDURE dbo.sp_ins
@Title NVARCHAR (100) ,
--@Questions QuestionList READONLY,
@Questions AS QuestionList READONLY, <-------CHANGED
@TopicId INT
AS
BEGIN