如何在创建新的SqlParameter时设置值和数据类型?

时间:2014-07-20 07:16:38

标签: sql-server

我的代码为QuestionsCount设置参数,然后为Questions:

设置
    parameterList.Add(new SqlParameter("@QuestionsCount", chunkSize));

    var p = new SqlParameter("@Questions", questions);
    p.TypeName = "dbo.QuestionList";
    parameterList.Add(p);

有没有办法可以组合最后三行并使用typeName创建一个新的SqlParameter。我正在查看定义但找不到值为(questions)和TypeName "dbo.QuestionList"的定义。

2 个答案:

答案 0 :(得分:3)

SqlParameter的构造函数有很多重载,其中一些允许您指定数据类型和可选的长度:

var p = new SqlParameter("@Questions", SqlDbType.VarChar, 100);
p.Value = ":.......";
parameterList.Add(p);

查看MSDN documentation on SqlParameter了解详情。

答案 1 :(得分:1)

一种方法是使用初始化程序提供构造函数重载中不可用的属性值:

parameterList.Add(new SqlParameter( "@Questions", SqlDbType.Structured ) { TypeName = "dbo.QuestionList", Value = questions });