c# - C#从红移连接和SQL查询异常填充datagridview
问题描述
我有一个按钮,当按下该按钮时,应该使用来自红移连接的 SQL 查询的结果填充 datagridview,但是当它运行时,我得到了异常并且没有填充任何内容。
private void Button1_Click(object sender, EventArgs e)
{
string connString = "Server=" + Properties.Settings.Default.awsconstring
+ ";Port=" + Properties.Settings.Default.awsport
+ "; User Id=" + Properties.Settings.Default.awsusername
+ ";Password=" + Properties.Settings.Default.awspassword
+ ";Database=" + Properties.Settings.Default.awsdb
+ "";
string query = "SELECT * FROM schema.Table";
NpgsqlConnection conn = new NpgsqlConnection(connString);
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
try
{
NpgsqlDataAdapter da = new NpgsqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
testdgv.DataSource = dt;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Connection error.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
解决方案
您没有打开连接。尝试:
...
NpgsqlConnection conn = new NpgsqlConnection(connString);
conn.Open();
...
推荐阅读
- javascript - 不区分大小写删除具有特定数据的行
- regex - sed 多行搜索替换
- kotlin - 具有默认非常量参数的测试函数
- json - 由新的 json 数据触发的 Firebase 云函数(在 url 上提供)
- angular - 在 Angular 10 中监听模板驱动的表单更改
- c++ - Flex & Bison C++
- angular - 在 NX monorepo angular 中运行离子生成页面
- c# - 如何使用 NodaTime 检查“现在”是否位于两个 OffsetDateTime 对象之间?
- c# - 如何在 C# 中将列表拆分为视图
- raspberry-pi - 如何使用 RPI 停止使用 MFRC522 读取