Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为存储过程设置参数时,是否可以在new SqlParameter的构造函数中设置TypeName?_C#_Asp.net_Sql Server - Fatal编程技术网

C# 为存储过程设置参数时,是否可以在new SqlParameter的构造函数中设置TypeName?

C# 为存储过程设置参数时,是否可以在new SqlParameter的构造函数中设置TypeName?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我使用以下代码设置调用存储过程的参数: List<SqlParameter> parameterList = new List<SqlParameter>(); parameterList.Add(new SqlParameter("@Title", adminTest.Title)); parameterList.Add(new SqlParameter("@Text", adminTest.Text));

我使用以下代码设置调用存储过程的参数:

        List<SqlParameter> parameterList = new List<SqlParameter>();
        parameterList.Add(new SqlParameter("@Title", adminTest.Title));
        parameterList.Add(new SqlParameter("@Text", adminTest.Text));

        var questionsList = new SqlParameter("@Questions", questions);
        questionsList.TypeName = "dbo.QuestionList";
        parameterList.Add(questionsList);
List参数List=newlist();
parameterList.Add(新的SqlParameter(“@Title”,adminTest.Title));
parameterList.Add(新的SqlParameter(“@Text”,adminTest.Text));
var questionsList=新的SqlParameter(“@Questions”,Questions);
questionsList.TypeName=“dbo.QuestionList”;
参数列表。添加(问题列表);
代码片段可以工作,但我想知道的是,是否有人找到了在新的SqlParameter构造函数中设置
TypeName
的方法?我试着查看文档,但唯一能找到的是在之后添加类型名。

尝试如下:

在C中创建数据表#

创建sql参数

  string conStr = "Server=localhost;Database=master;Trusted_Connection=True;";
        con = new SqlConnection(conStr);
        con.Open();
  using (con)
        {
            SqlCommand insertCommand = new SqlCommand("InsertQuestionList", con);
            SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
                "@QuestionList", myDataTable);
            insertCommand.CommandType = CommandType.StoredProcedure;

            tvpParam.SqlDbType = SqlDbType.Structured;
            tvpParam.TypeName = "dbo.QuestionList";
            tvpParam.Value = myDataTable;
            insertCommand.ExecuteNonQuery();
        }

        con.Close();
创建表类型

 CREATE TYPE dbo.QuestionList AS TABLE
    ( Title varchar(50), TEXT nvarchar(50) )
插入产品

ALTER PROCEDURE  InsertQuestionList
   @QuestionList QuestionList READONLY
AS
BEGIN
 --insert into Your table
 select * from @QuestionList
END
GO

您可以使用初始值设定项来实现这一点。下面的示例指定了强类型参数和变量值的最大长度,从SQL性能的角度来看,这是一种很好的做法

    List<SqlParameter> parameterList = new List<SqlParameter>()
        {
            new SqlParameter("@Title", SqlDbType.VarChar) {Size = 30, Value = adminTest.Title},
            new SqlParameter("@Text", SqlDbType.VarChar) {Size = 30, Value = adminTest.Text},
            new SqlParameter("@Questions", SqlDbType.Structured) {TypeName = "dbo.QuestionList", Value = questions}
        };
List参数List=newlist()
{
新的SqlParameter(“@Title”,SqlDbType.VarChar){Size=30,Value=adminTest.Title},
新的SqlParameter(“@Text”,SqlDbType.VarChar){Size=30,Value=adminTest.Text},
新的SqlParameter(“@Questions”,SqlDbType.Structured){TypeName=“dbo.QuestionList”,Value=Questions}
};

问题列表
是您的表类型吗???-1这不起作用,至少我的测试显示不起作用。如果
CommandType
设置为
StoredProcedure
,则不需要
TypeName
属性,因此您看不到数据表的名称根本没有被使用。如果将
CommandType
设置为
Text
,则需要
TypeName
属性,并且您可以看到,无论将
DataTable
设置为什么,都不会使用该名称。此外,如果要使用它,它至少需要是“dbo.QuestionList”,但即使修复它也没有帮助。@srutzky已在我的pc上更新并测试,现在工作正常感谢您的更新和验证。剩下的唯一问题是,问题具体到没有指定
TypeName
属性作为附加步骤。此外,当代码运行时,您可以删除设置
TypeName
的行,它仍然可以运行,因为
CommandType
StoredProcedure
。但这表明它并不是在看
数据表的名称,因为这确实有效,而且提到
大小的显式设置肯定是很好的(尽管也可以在构造函数中设置,并且不需要使用初始值设定项),但仍应注意,如果将
CommandType
设置为
StoredProcedure
,则不需要或不需要
TypeName
属性。
    List<SqlParameter> parameterList = new List<SqlParameter>()
        {
            new SqlParameter("@Title", SqlDbType.VarChar) {Size = 30, Value = adminTest.Title},
            new SqlParameter("@Text", SqlDbType.VarChar) {Size = 30, Value = adminTest.Text},
            new SqlParameter("@Questions", SqlDbType.Structured) {TypeName = "dbo.QuestionList", Value = questions}
        };