I'm trying to run a test case, and this won't even work... What am I doing wrong here?
Here's the 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
And code to connect and execute the procedure:
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();
}
I'm not getting any errors. Just 0 rows affected.
This works in SQL Server, though:
DECLARE @dt Playground.DateTimeTable
INSERT INTO @dt VALUES ('2004-10-27')
EXEC Playground.InsertDate @dt
What am I supposed to be doing here?
EXECfrom the command and settingcommand.CommandType = CommandType.StoredProcedure.EXEC Playground.InsertDate @dt. Thanks for the clue!SqlCommandproperties, but that might work too. Let me know...CommandType.StoredProcedurejust means that it will get the definition before executing, and use the parameter names in the definition, meaning it essentially resolves the command text toEXEC Playground.InsertDate @dt. Adding parameters just makes them available to your command text.