如何在 C# 中将 Npgsql 与 TableAdapter 一起使用

发布于 2024-07-26 02:47:49 字数 93 浏览 3 评论 0原文

我有PostgresQL数据库,并且我使用C#和Npgsql开发接口应用程序来连接数据库,如何将Npgsql连接分配给TableAdapter?

谢谢,

I have PostgresQL database, and I develop interface application using C# and Npgsql to connect to the database, how can I assign the Npgsql connection to TableAdapter?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-08-02 02:47:49

我和 PostgreSQL 的处境完全相同。

我不相信这是可能的。 Npgsql 有自己的数据适配器(请参阅 http://npgsql.projects.postgresql。 org/docs/manual/UserManual.html 并搜索文本“适配器”)。 这样做的缺点是您无法使用 Visual Studio 设计器。

因此,我使用 .NET 的 ODBC 数据源。 为此,您需要安装 postgresql odbc 驱动程序,可在此处获取: http://www.postgresql.org/ftp/odbc/versions/msi/。 安装完成后,您可以进入控制面板-> 管理工具 -> 数据源 (ODBC) 添加 DSN(数据源名称)。 最后,在Visual Studio中转到服务器资源管理器,右键单击“数据连接”并选择“添加连接...”,并更改Microsoft ODBC数据源的数据源。 在这里你可以选择你之前提供的DSN,还有中提琴! 你在做生意。

(请注意,由于某些疯狂的原因,布尔值以字符串形式出现。您可以在 ODBC 数据源管理器中更改此设置,方法是单击 PostgreSQL 数据源上的“配置”,转到数据源选项,然后取消选中“Bools as Char”。)

I'm in the exact same boat with PostgreSQL.

I don't believe that's possible. Npgsql has its own data adapter (see http://npgsql.projects.postgresql.org/docs/manual/UserManual.html and search for text "adapter"). The downside to this is that you can't use the Visual Studio Designer.

So instead, I'm using .NET's ODBC DataSource. For this to work, you will need to install the postgresql odbc driver, which is available here: http://www.postgresql.org/ftp/odbc/versions/msi/. After installing, you can go to Control Panel -> Administrative Tools -> Data Soruces (ODBC) to add a DSN (Data Source Name). Finally, in Visual Studio go to the Server Explorer, right click "Data Connections" and select "Add Connection...", and change the data source of Microsoft ODBC Data Source. Here you can select the DSN you provided earlier, and viola! You're in business.

(Note that for some crazy reason bools come in as strings. You can change this in the ODBC Data Source Administrator by clicking "Configure" on your PostgreSQL datasource, going to Datasource options, and unchecking "Bools as Char".)

我喜欢麦丽素 2024-08-02 02:47:49

抱歉,有点晚了,但是您可以这样做,使用数据适配器。

创建一个数据网格
创建一个数据表

用dataAdapter填充数据表, // 提供了函数 - dataTableName.fill()
设置数据网格以显示数据表的默认视图鲍勃

    try
        {
           using (var Connection = new NpgsqlConnection(PG_Connection_String))
           NpgsqlDataAdapter da = new NpgsqlDataAdapter("myQuery", connectionString))

 {
                    Connection.Open();

                    myTable = new System.Data.DataTable();
                    da.Fill(myTable);

                    postgresql_dataGrid.DataSource = myTable.DefaultView;

                    Connection.Close();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Your Error", "Connection Error");
            }
        }

是你的叔叔,我正在成功使用它。 我建议,如果您希望为自定义标题等准备网格,请在设置数据网格的默认视图之前执行此操作

Sorry its a bit late however here is how you would do it, use the data adapter.

create a datagrid
create a datatable

fill the datatable with the dataAdapter, // function is provided - dataTableName.fill()
set the datagrid to display default view of the dataTable

    try
        {
           using (var Connection = new NpgsqlConnection(PG_Connection_String))
           NpgsqlDataAdapter da = new NpgsqlDataAdapter("myQuery", connectionString))

 {
                    Connection.Open();

                    myTable = new System.Data.DataTable();
                    da.Fill(myTable);

                    postgresql_dataGrid.DataSource = myTable.DefaultView;

                    Connection.Close();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Your Error", "Connection Error");
            }
        }

Bob's your uncle, I'm using it successfully. I would suggest that if you wish to prepare the grid for custom headers etc, do it BEFORE setting the default view of your datagrid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文