簡體   English   中英

C#從紅移連接和SQL查詢異常填充datagridview

[英]C# populating datagridview from red shift connection and SQL query exception

我有一個按鈕,當按下該按鈕時,應該使用來自紅移連接的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();
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM