首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的表值参数在到达数据库时为空?

为什么我的表值参数在到达数据库时为空?
EN

Stack Overflow用户
提问于 2009-12-08 05:33:41
回答 1查看 700关注 0票数 2

我试图通过使用ADO.NET调用一个存储过程来测试SQL2008新的表值参数功能,但是我遇到了一个问题,当参数到达存储过程时,它似乎不包含任何行。UDT如下所示:

代码语言:javascript
运行
复制
CREATE TYPE [dbo].[PersonType] AS TABLE(
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Birthdate] [date] NULL)

存储的proc如下所示:

代码语言:javascript
运行
复制
 CREATE PROCEDURE [dbo].[AddPeople]
 (@peopleToAdd dbo.PersonType READONLY)
 AS
 BEGIN
      IF (SELECT COUNT(*) FROM @peopleToAdd) > 0
      BEGIN
           SELECT 'Has rows'
      END
      ELSE
      BEGIN
           SELECT 'Does NOT have rows'
      END
 END

最后,.NET代码是这样的(振作起来,它很多):

代码语言:javascript
运行
复制
public class Program
{
    static void Main(string[] args)
    {
        PersonCollection people = 
            new PersonCollection()
              {
                 new Person
                 {
                    FirstName = "John",
                    LastName = "Doe",
                    Birthdate = new DateTime(1975, 12, 1)
                 },
                 new Person
                 {
                    FirstName = "Randall",
                    LastName = "Stevens",
                    Birthdate = new DateTime(1935, 7, 10)
                 }
               };

        using(SqlConnection conn = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=TVPExample;Integrated Security=SSPI;"))
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("AddPeople", conn);

            SqlParameter parameter = cmd.Parameters.AddWithValue("@peopleToAdd", people);
            parameter.SqlDbType = SqlDbType.Structured;
            parameter.TypeName = "dbo.PersonType";

            string result = cmd.ExecuteScalar().ToString();

            Console.WriteLine(result);
        }
    }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthdate { get; set; }
}

public class PersonCollection : List<Person>, IEnumerable<SqlDataRecord>
{
    #region Implementation of IEnumerable<SqlDataRecord>
    IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
    {
        SqlDataRecord rec = new SqlDataRecord(
            new SqlMetaData("FirstName", SqlDbType.VarChar, 50), 
            new SqlMetaData("LastName", SqlDbType.VarChar, 50), 
            new SqlMetaData("Birthdate",SqlDbType.Date));

        foreach (Person person in this)
        {
            rec.SetString(0, person.FirstName);
            rec.SetString(1, person.LastName);
            rec.SetDateTime(2, person.Birthdate);
            yield return rec;
        }
    }
    #endregion
}

我以this博客文章为例。我总是得到“不包含行”的结果,但是查看Visual Studio调试器就会发现,我传入的集合包含了我放入其中的两个值。有什么想法吗?我遗漏了什么?

EN

Stack Overflow用户

回答已采纳

发布于 2009-12-08 06:02:27

添加以下内容:

代码语言:javascript
运行
复制
cmd.CommandType = CommandType.StoredProcedure;
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1863057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档