Postgresql – 动态创建数据库和表
此代码无效。 任何人都可以指导我在哪里可以找到使用C#动态创建Postgresql数据库和表的示例?
const string connStr = "Server=localhost;Port=5432; User Id=postgres;Password=enter;Database=postgres"; var m_conn = new NpgsqlConnection(connStr); // creating a database in Postgresql m_createdb_cmd = new NpgsqlCommand("CREATE DATABASE IF NOT EXISTS \"testDb\" " + "WITH OWNER = \"postgres\" " + "ENCODING = 'UTF8' " + "CONNECTION LIMIT = -1;", m_conn); // creating a table in Postgresql m_createtbl_cmd = new NpgsqlCommand( "CREATE TABLE MyTable(CompanyName VARCHAR(150))"; m_conn.Open(); m_createdb_cmd.ExecuteNonQuery(); m_createtbl_cmd.Connection = m_conn; m_conn.Close();
db已创建,但在创建表时出现静默失败。
我会这样做:
string connStr = "Server=localhost;Port=5432;User Id=postgres;Password=enter;"; var m_conn = new NpgsqlConnection(connStr); var m_createdb_cmd = new NpgsqlCommand(@" CREATE DATABASE IF NOT EXISTS testDb WITH OWNER = postgres ENCODING = 'UTF8' CONNECTION LIMIT = -1; ", m_conn); m_conn.Open(); m_createdb_cmd.ExecuteNonQuery(); m_conn.Close(); connStr = "Server=localhost;Port=5432;User Id=postgres;Password=enter;Database=testDb"; m_conn = new NpgsqlConnection(connStr); m_createtbl_cmd = new NpgsqlCommand( "CREATE TABLE table1(ID CHAR(256) CONSTRAINT id PRIMARY KEY, Title CHAR)" , m_conn); m_conn.Open(); m_createtbl_cmd.ExecuteNonQuery(); m_conn.Close();
这里不建议使用var
。 我使用它,因为我不知道返回的类型是什么,但你应该。
注意使用原始字符串( @
)。 它使字符串构建变得简单。
除非标识符是非法的,否则不要在Postgresql中使用双引号括起来的标识符。 它会让你的生活更加艰难。
好像你只是忘了调用m_createtbl_cmd:
ExecuteNonQuery
方法m_createtbl_cmd:
m_createtbl_cmd.ExecuteNonQuery();
您也可以使用DynORM库简化它: http ://dynorm.codeplex.com/
希望能帮助到你!
解:
// 1. Connect to server to create database: const string connStr = "Server=localhost;Port=5432;User Id=postgres;Password=enter;"; // 2. Connect to server to create table: const string connStr2 = "Server=localhost;Port=5432;User Id=postgres;Password=enter;Database=testDb"; var m_conn = new NpgsqlConnection(connStr); // db connction var m_conn2 = new NpgsqlConnection(connStr2); // table connection // creating a database in Postgresql m_createdb_cmd = new NpgsqlCommand("CREATE DATABASE IF NOT EXISTS \"testDb\" " + "WITH OWNER = \"postgres\" " + "ENCODING = 'UTF8' " + "CONNECTION LIMIT = -1;", m_conn); // creating a table in Postgresql m_createtbl_cmd = new NpgsqlCommand { CommandText ="CREATE TABLE table1(ID CHAR(256) CONSTRAINT id PRIMARY KEY, Title CHAR)" }; m_createtbl_cmd.Connection = m_conn2; // 3.. Make connection and create // open connection to create DB m_conn.Open(); m_createdb_cmd.ExecuteNonQuery(); m_conn.Close(); // open connection to create table m_conn2.Open(); m_createtbl_cmd.ExecuteNonQuery(); m_conn2.Close();
这有效,但有更短的方法吗? 我不得不创建两个Npgsql连接。 我不知道,对我来说只是看起来不是很优雅。
这对我来说用C#来validation任何Postgres数据库的存在是有用的:
private bool chkDBExists(string connectionStr, string dbname) { using (NpgsqlConnection conn = new NpgsqlConnection(connectionStr)) { using (NpgsqlCommand command = new NpgsqlCommand ($"SELECT DATNAME FROM pg_catalog.pg_database WHERE DATNAME = '{dbname}'", conn)) { try { conn.Open(); var i = command.ExecuteScalar(); conn.Close(); if (i.ToString().Equals(dbname)) //always 'true' (if it exists) or 'null' (if it doesn't) return true; else return false; } catch (Exception e) { return false; } } } }
** try-catch语句中使用的if可以简单地检查ExecuteScalar的返回值对于不存在的DB是否为null,如果存在则不为null。