【问题标题】:How to load data from PostgreSQL to RichTextBox?如何将数据从 PostgreSQL 加载到 RichTextBox?
【发布时间】:2022-01-20 19:38:03
【问题描述】:

我正在尝试将数据转换为 RichTextBox,但仍然无法做到。下面是我的代码。我的数据测试并有数据,现在只需将其读入RichTextBox。

 private void btnConvert_Click(object sender, EventArgs e)
        {
            ListViewItem selectedComune = lvResultCommune.SelectedItems[0];
            string selectedCommuneId = selectedComune.Text;
            if (selectedCommuneId != null)
            {
                NpgsqlDataAdapter adapter = new NpgsqlDataAdapter();
                NpgsqlCommand cmd = new NpgsqlCommand("SELECT ST_AsText(geom) as geometry FROM hanhchinhxa where id_4 = " + selectedCommuneId , conn);
                adapter.SelectCommand = cmd;
                cmd.Connection = conn;
                DataTable data = new DataTable();
                adapter.Fill(data);

                richTextBox.Text = data.ToString();
            }
            else
            {
                throw new Exception("\n" + "Geometry illegal !!");
            }

        }

【问题讨论】:

  • 尝试将其转换为 Base 64 字符串并将其设置在文本框中,每次都对我有用!
  • 如何转换? @runtimeTerror
  • 请不要使用字符串连接来创建sql命令,你正在打开自己对Sql Injection攻击的开放。
  • 别担心,我只是在尝试得到结果
  • richTextBox.Text = data.ToString(); ... 并没有按照您的想法行事。 dataDataTable ... 它有行和列。

标签: c# postgresql


【解决方案1】:

我自己找到了答案!

richTextBox.Text = data.Rows[0].ItemArray[0].ToString();

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多