C # - create custom SQL table type using SQLCommand Fails

I am trying to create a database on the fly for my unit testing script.

Using SQL Server LocalDB 11.00.3000 .NET 4.0

I can create / drop a database and add SPs, Views, Tables, etc ... everything is fine using a straight SQL script.

The problem occurs when I try to create a custom TYPE.

CREATE TYPE test as TABLE( [Action_Item_Id] INT,[Shift_Id] NVARCHAR (34))

      

.... works fine in the SQL console. But in the code ....

SqlConnection conn = new SqlConnection(connString);
conn.Open();
var cmd = new SqlCommand("CREATE DATABASE UnitTestingDatabase",conn);
conn.ChangeDatabase("master");
cmd.ExecuteNonQuery();
conn.ChangeDatabase("UnitTestingDatabase");
cmd.CommandText = "CREATE TYPE test as TABLE( [Action_Item_Id] INT,[Shift_Id] NVARCHAR (34))";
cmd.ExecuteNonQuery();

      

I am getting an exception:

{"Incorrect syntax near ''."}
[System.Data.SqlClient.SqlException]: {"Incorrect syntax near ''."}
_className: null
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: null
_exceptionMethod: {Void OnError(System.Data.SqlClient.SqlException, Boolean, System.Action`1[System.Action])}
_exceptionMethodString: null
_helpURL: null
_HResult: -2146232060
_innerException: null
_ipForWatsonBuckets: 247330783
_message: "Incorrect syntax near ''."
_remoteStackIndex: 0
_remoteStackTraceString: null
_safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
_source: null
_stackTrace: {sbyte[384]}
_stackTraceString: null
_watsonBuckets: null
_xcode: -532462766
_xptrs: 0
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
HResult: -2146232060
InnerException: null
IPForWatsonBuckets: 247330783
IsTransient: false
Message: "Incorrect syntax near ''."
RemoteStackTrace: null
Source: ".Net SqlClient Data Provider"
TargetSite: {Void OnError(System.Data.SqlClient.SqlException, Boolean, System.Action`1[System.Action])}
WatsonBuckets: null

      

It took me a little to understand that it was not. he had a problem, but a non-being between "what seems meaningless."

+3


source to share





All Articles