代码之家  ›  专栏  ›  技术社区  ›  Brian Sullivan

为什么到达数据库时表值参数为空?

  •  1
  • Brian Sullivan  · 技术社区  · 15 年前

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

    CREATE TYPE [dbo].[PersonType] AS TABLE(
    [FirstName] [varchar](50) NULL,
    [LastName] [varchar](50) NULL,
    [Birthdate] [date] NULL)
    

    存储过程如下:

     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代码是这样的(振作起来,它是很多):

    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调试器会发现我传递的集合包含我在其中放入的两个值。有什么想法吗?我错过了什么?

    1 回复  |  直到 15 年前
        1
  •  4
  •   Remus Rusanu    15 年前

    添加:

    cmd.CommandType = CommandType.StoredProcedure;