我正在尝试为我的单元测试脚本动态创建数据库。
使用SQL Server LocalDB 11.00.3000 .NET 4.0
我可以使用直接的SQL脚本创建/删除数据库并添加SP,视图,表等等。
当我尝试创建用户定义的TYPE时会出现问题。
CREATE TYPE test as TABLE( [Action_Item_Id] INT,[Shift_Id] NVARCHAR (34))
....在SQL控制台中完美执行。但是在代码中......
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();
我得到一个例外:
{"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
我花了一点时间才意识到这不是一个。它有一个问题,但“这似乎是无稽之谈”之间的虚无。