我试图通过使用ADO.NET调用一个存储过程来测试SQL2008新的表值参数功能,但是我遇到了一个问题,当参数到达存储过程时,它似乎不包含任何行。UDT如下所示:
CREATE TYPE [dbo].[PersonType] AS TABLE(
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Birthdate] [date] NULL)存储的proc如下所示:
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调试器就会发现,我传入的集合包含了我放入其中的两个值。有什么想法吗?我遗漏了什么?
发布于 2009-12-08 06:02:27
添加以下内容:
cmd.CommandType = CommandType.StoredProcedure;https://stackoverflow.com/questions/1863057
复制相似问题