我想通过Dapper在MySQL中删除并创建一个数据库,如下所示:
public class GenericSqlExecutionRepository
{
private IDbConnection _db;
public void RecreateDb()
{
_db.Execute("DROP DATABASE IF EXISTS `my_db`;");
_db.Execute("CREATE DATABASE `my_db`;");
}
}
Drop数据库工作正常,但是当你尝试执行create database时:
“未知数据库'my_db'”
at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.ReadOk(Boolean read) at MySql.Data.MySqlClient.NativeDriver.SetDatabase(String dbName) at MySql.Data.MySqlClient.Driver.SetDatabase(String dbName) at MySql.Data.MySqlClient.MySqlConnection.ChangeDatabase(String databaseName) at MySql.Data.MySqlClient.MySqlConnection.Open() 在Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn,CommandDefinition& command,Action'2 paramReader) 在Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn,CommandDefinition& command) 在Dapper.SqlMapper.Execute(IDbConnection cnn,String sql,Object param,IDbTransaction事务,Nullable'1 commandTimeout,Nullable'1 commandType)
当然我的IDbConnection
绑定到一个数据库(在连接字符串中指定)。那么:是否可以绕过这个问题并通过Dapper进行drop + create?
附加信息
var _db
的生成如下:
public static MySqlConnection GetConnection(string connectionStringId)
{
var connectionString = ConfigurationManager.ConnectionStrings[connectionStringId].ConnectionString;
var connection = new MySqlConnection();
connection.ConnectionString = connectionString;
return connection;
}
这是App.config
中的连接字符串:
<add name="myDbId" connectionString="Server=localhost;Port=3306;Database=my_db;User=yyyyyyy;Password=zzzzzzz;Convert Zero Datetime=True;Compress=True;" providerName="System.Data.SqlClient"/>
答案 0 :(得分:1)
看起来只需从连接字符串中删除Database=my_db;
,Dapper就可以了:
<add name="myDbId" connectionString="Server=localhost;Port=3306;User=yyyyyyy;Password=zzzzzzz;Convert Zero Datetime=True;Compress=True;" providerName="System.Data.SqlClient"/>
现在两个查询都有效。