我有一个用dapper创建数据库的代码:
public static bool CreateDatabase(string connectionString, string databaseName)
{
const string DATABASE_CREATE_QUERY = "CREATE DATABASE (@databaseName)";
using (var connection = new SqlConnection(connectionString))
{
var affectedRows = connection.Execute(DATABASE_CREATE_QUERY, new { databaseName });
return affectedRows > 0 ? true : false;
}
}
CreateDatabase("Server=10.10.10.10;User ID=user;Password=password;MultipleActiveResultSets=true;", "TEST_DB");
每次运行此方法时,都会出现错误:
'('附近的语法不正确。
有痕迹:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in E:\GitHub\Dapper\Dapper\SqlMapper.cs:line 2827
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in E:\GitHub\Dapper\Dapper\SqlMapper.cs:line 570
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in E:\GitHub\Dapper\Dapper\SqlMapper.cs:line 443
at DbCreator.DbUtils.CreateDatabase(String connectionString, String databaseName) in E:\src_projects\DbCreator\DbUtils.cs:line 48
at DbCreator.***.Run(String connectionString) in E:\src_projects\DbCreator\***.cs:line 23
如果我将查询设置为丑陋的方式
const string DATABASE_CREATE_QUERY = $"CREATE DATABASE {databaseName}";
然后运行
connection.Execute(DATABASE_CREATE_QUERY);
一切正常。
如果我不使用QUERY中的方括号()
const string DATABASE_CREATE_QUERY = "CREATE DATABASE @databaseName";
我收到一个错误:
“ @ databaseName”附近的语法不正确
我也尝试过ExecuteScalar,Query方法,但结果相同。
有什么想法为什么不能使用Dapper方式?
答案 0 :(得分:3)
这是因为使用sp_executesql进行参数设置。这是通过Dapper解释在数据库中执行的内容;
exec sp_executesql N'Create Database (@DBName)',N'@DBName nvarchar(4000)',@DBName=N'Test'
看看为什么不能使用sp_executesql:Table name as variable
您最好的选择是创建一个存储过程,或使用字符串替换方法。
答案 1 :(得分:-1)
因为创建数据库语句不需要()。
尝试:
const string DATABASE_CREATE_QUERY = "CREATE DATABASE @databaseName";