如何通过datagridview从任何表中获取列数据类型并将数据插入表中?

时间:2017-05-06 10:49:34

标签: c# postgresql datagridview

我有一个很大的问题。现在我在C#中有一个Windows窗体应用程序,它允许我登录任何PostgreSQL数据库并在datagridview中查看数据库表内容。我现在需要完成的是找到一种从datagridview列值中读取数据类型的方法,并找到一种在表中插入新的正确数据的方法。

这是我读取数据的代码,如果有帮助的话。 (我将表名放在列表框中,并在选择时显示数据)

public void readdata()
    {
        DataTable tabulusaraksts = new DataTable();
        string connectionstring = String.Format("Server={0};Port={1};" + "User Id={2};Password={3};Database={4};", serveris, port.ToString(), user.ToString(), password.ToString(), database.ToString());
        ncon = new NpgsqlConnection(connectionstring);
        string tab = listBox1.GetItemText(listBox1.SelectedItem);
        NpgsqlCommand listfill = new NpgsqlCommand("select * from pg_catalog.pg_tables where schemaname != 'pg_catalog' AND schemaname != 'information_schema';", ncon);
        adapter.SelectCommand = listfill;
        adapter.Fill(tabulusaraksts);
        listBox1.DisplayMember = "tablename";
        listBox1.ValueMember = "tablename";
        listBox1.DataSource = tabulusaraksts;
    }

        public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable tabula = new DataTable();
        dataGridView1.DataSource = false;
        string tab = listBox1.GetItemText(listBox1.SelectedItem);
        NpgsqlCommand showtable = new NpgsqlCommand("select * from " + tab + ";", ncon);
        adapter2.SelectCommand = showtable;
        adapter2.Fill(tabula);
        dataGridView1.DataSource = tabula;
        dataGridView1.Columns[0].Visible = false;
    }

0 个答案:

没有答案