我正在尝试以编程的方式创建一个postGres数据库c#,以前我已经手动创建了DB和teh连接字符串来打开它并对其进行操作,而且它运行得很好。
现在,我必须首先检查DBB是否已经存在,如果不存在,那么首先创建它,然后执行其他操作,然后对其进行liek读写。
我这样做的代码是:
internal void createDataBaseIfDoNotExist()
{
string connStr = string.Empty;
connStr =
"Server=" + "localhost"
+ ";Port=" + "5432"
+ ";User Id=" + "openpg"
+ ";Password=" + "oppwd"
+ ";";
var m_conn = new NpgsqlConnection(connStr);
var m_createdb_cmd = new NpgsqlCommand(@"CREATE DATABASE IF NOT EXISTS testDb ;", m_conn);
try
{
m_conn.Open();
m_createdb_cmd.ExecuteNonQuery();
m_conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Errr on createDataBaseIfDoNotExist query :" + ex);
}
}
And it gives followign error:
database "openpg" does not exist Severity: FATAL Code: 3D000
而我保留的数据库名为"testDb",而不是"openpg“
以前,我创建了一个DB名称"NewDB“,并创建了如下所示的连接字符串:
connStr =
"Database=" + "NewDB"
+ ";Server=" + "localhost"
+ ";Port=" + "5432"
+ ";User Id=" + "openpg"
+ ";Password=" + "oppwd"
+ ";";
而且它工作得很好,那么为什么现在就有错误,如何补救呢?
发布于 2015-12-09 10:09:49
您需要在连接字符串中添加Database=postgres
connStr = "Server=localhost;Port=5432;User Id=postgres;Password=password_of_the_user;Database=postgres";
因为postgres
是默认的PostgreSQL管理连接数据库
https://stackoverflow.com/questions/34175836
复制相似问题