我试图运行一个测试用例,这甚至不能正常工作...... 我在这里做错了什么?
这是SQL:
CREATE TABLE
Playground.Test (saved DateTime)
GO
CREATE TYPE
Playground.DateTimeTable AS TABLE
([time] DATETIME);
GO
CREATE PROCEDURE
Playground.InsertDate
@dt Playground.DateTimeTable READONLY
AS
BEGIN
INSERT INTO Playground.Test (saved)
SELECT [time]
FROM @dt
END
GO
连接并执行程序的代码:
const String connString =
"server = SERVER; database = DB; UID = myUserID; pwd = myPassword;";
static void Main(string[] args)
{
SqlCommand command =
new SqlCommand(
"EXEC Playground.InsertDate",
new SqlConnection(connString));
DataTable table = new DataTable("DateTimeTable");
table.Columns.Add("[time]", typeof(DateTime));
table.Rows.Add(DateTime.Parse("10/27/2004"));
SqlParameter tvp = command.Parameters.AddWithValue("@dt", table);
tvp.SqlDbType = SqlDbType.Structured;
tvp.TypeName = "Playground.DateTimeTable";
command.Connection.Open();
int affected = command.ExecuteNonQuery();
command.Connection.Close();
Console.WriteLine(affected);
Console.ReadKey();
}
我没有收到任何错误。只有0行受影响。
这适用于SQL Server,但是:
DECLARE @dt Playground.DateTimeTable
INSERT INTO @dt VALUES ('2004-10-27')
EXEC Playground.InsertDate @dt
我应该在这做什么?
答案 0 :(得分:2)
您没有将SqlCommand
对象设置为存储过程。你应该做几件事:
EXEC
前缀
将command
设置为存储过程:
command.CommandType = CommandType.StoredProcedure;
DataTable
列名称周围的方括号是如何影响这一点的,但我怀疑删除它们会更好。