我正在尝试将PostgreSQL数据库连接到C#应用程序。为此,我创建了ODBC数据源,现在我需要连接字符串包含在C#中。
问题:如何在C#中为PostgreSQL编写DSN连接字符串?
答案 0 :(得分:2)
试试这个
// PostgeSQL-style connection string
string connstring = String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database={4};",
tbHost.Text, tbPort.Text, tbUser.Text,
tbPass.Text, tbDataBaseName.Text );
// Making connection with Npgsql provider
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
// quite complex sql statement
string sql = "SELECT * FROM simple_table";
// data adapter making request from our connection
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
此处有更多详情http://www.codeproject.com/Articles/30989/Using-PostgreSQL-in-your-C-NET-application-An-intr