使用Postgres以.NET形式显示数据

时间:2017-01-31 10:03:41

标签: c# .net postgresql

我尝试使用此代码在表单中显示来自数据库的数据但是我在DataAdapter.Fill中出错了,我试图删除它但是当我执行程序时没有出现,我不知道我还可以做些什么。这里有错误的代码和屏幕截图:

https://i.stack.imgur.com/faQ32.png

public partial class ShowData : Form
    {
        public ShowData()
        {
            InitializeComponent();

            NpgsqlConnection conn = new NpgsqlConnection();
            conn.ConnectionString = "Server=localhost;Port=5432;Database=firstdatabase;User Id=postgres";                      
            conn.Open();

            string sql = "SELECT * FROM first_schema.customers";

            NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);           

            NpgsqlDataReader dr = cmd.ExecuteReader();
            NpgsqlDataAdapter ad = new NpgsqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();


            while (dr.Read())
            {
                for (int i = 0; i < dr.FieldCount; i++)
                    Console.Write("{0} \t \n", dr[i].ToString());
            }

            ad.Fill(dt);
            // ad.Fill(ds);

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    Console.Write("{0} \t \n", row[i].ToString());
                }
            }


            conn.Close();
        }
    }

0 个答案:

没有答案