我想从我的 .Net 框架连接到 Tableau PostgreSQL 服务器,以列出在 Tableau 服务器中发布的所有报告和数据源。
为此,我完成了以下步骤。
- 添加了我在网上下载的npgsql.dll引用
在我的类文件中添加了以下两个命名空间
使用 NpgsqlTypes; 使用 Npgsql;
我添加了如下连接字符串
我还尝试使用端口值修改连接字符串,并将数据源重命名为服务器,将初始目录重命名为数据库,并将提供程序名称重命名为 Npgsqll
我的方法如下:
public DataTable getAllDataSourceNames() { DataTable dataSourceNames = new DataTable(); NpgsqlConnection conServer = new NpgsqlConnection(conString); conServer.Open(); string command = @"select * from datasources"; NpgsqlDataAdapter sqlcmd = new NpgsqlDataAdapter(command,conServer); sqlcmd.Fill(dataSourceNames); return dataSourceNames; }`
没有错误。我可以成功构建并运行网站中的其他链接。但是无法与我的 postgresql 服务器建立连接。
知道如何建立连接吗?
最佳答案
在 C# 中使用 Postgres 连接:
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void llOpenConnAndSelect_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
try
{
// 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);
// i always reset DataSet before i do
// something with it.... i don't know why :-)
ds.Reset();
// filling DataSet with result from NpgsqlDataAdapter
da.Fill(ds);
// since it C# DataSet can handle multiple tables, we will select first
dt = ds.Tables[0];
// connect grid to DataTable
dataGridView1.DataSource = dt;
// since we only showing the result we don't need connection anymore
conn.Close();
}
catch (Exception msg)
{
// something went wrong, and you wanna know why
MessageBox.Show(msg.ToString());
throw;
}
}
以下链接可能对您有所帮助:Using PostgreSQL in your C# .NET application
关于c# - 在 C# 中连接到 tableau postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46903914/