数据库到富文本框问题

发布于 2024-10-27 19:56:48 字数 1431 浏览 5 评论 0原文

我在将 Access 数据库中的数据绑定到 Visual C# 表单上的富文本框中时遇到一些问题。 这是我的代码:

private void Form2_Load(object sender, EventArgs e)

        {
            string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
            OleDbConnection conGet = new OleDbConnection(connectionString);
            OleDbCommand cmdGet = new OleDbCommand();

            try
            {
                //open connection
                conGet.Open();

                cmdGet.CommandType = CommandType.Text;
                cmdGet.Connection = conGet;
                cmdGet.CommandText = "SELECT * FROM Paragraph";

                richTextBox.Rtf = cmdGet.ExecuteScalar().ToString();

                conGet.Close();

                MessageBox.Show("Data loaded from Database");
            }
            catch (Exception ex)
            {
                //display generic error message back to user
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //check if connection is still open then attempt to close it
                if (conGet.State == ConnectionState.Open)
                {
                    conGet.Close();
                }
            }
        }

当我按下按钮加载将包含富文本框的表单时,我会收到一个弹出框,显示“文件格式无效” 基本上在我的数据库中,有 1 列包含数据(该列中每行 1 个单词)

我上面的代码是从互联网上获取的,其他人已经成功使用它,我现在才知道是什么出错

I am having some trouble in binding my data from my Access database into my rich text box on my Visual C# form.
Here is my code:

private void Form2_Load(object sender, EventArgs e)

        {
            string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
            OleDbConnection conGet = new OleDbConnection(connectionString);
            OleDbCommand cmdGet = new OleDbCommand();

            try
            {
                //open connection
                conGet.Open();

                cmdGet.CommandType = CommandType.Text;
                cmdGet.Connection = conGet;
                cmdGet.CommandText = "SELECT * FROM Paragraph";

                richTextBox.Rtf = cmdGet.ExecuteScalar().ToString();

                conGet.Close();

                MessageBox.Show("Data loaded from Database");
            }
            catch (Exception ex)
            {
                //display generic error message back to user
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //check if connection is still open then attempt to close it
                if (conGet.State == ConnectionState.Open)
                {
                    conGet.Close();
                }
            }
        }

When I press the button to load up the form that will contain the rich text box, I get a pop up box saying 'File Format Not Valid'
Essentially in my database, there is 1 column that has data in it (1 word per row in that column)

The code I have above was taken from the Internet and other people have had success in using it, i'm just now sure what's going wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

镜花水月 2024-11-03 19:56:48

您无法将任何文本添加到 RichTextBo.Rtf 属性,它必须采用 RTF 格式

例如尝试一下:

richTextBox.Rtf = "Hello world";

它会抛出相同的异常。

您可以使用 RichTextBox.Text<相反, /code>属性可以接受纯文本。

You can't add any text to a RichTextBo.Rtf property, it has to be in the RTF Format.

Try this for example:

richTextBox.Rtf = "Hello world";

it will throw the the same exception.

You can use the RichTextBox.Text property instead, that can accept a plain text.

贪恋 2024-11-03 19:56:48

在我的数据库中,有 1 列包含数据(该列中每行 1 个单词)

所以您需要很多行?那么你不应该使用.ExecuteScalar()。这样做:

private void Form2_Load(object sender, EventArgs e)
{
    string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
    using (OleDbConnection conGet = new OleDbConnection(connectionString))
    using (OleDbCommand cmdGet = new OleDbCommand())
    try
    {
        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;
        cmdGet.CommandText = "SELECT * FROM Paragraph";

        StringBuilder paragraph = new StringBuilder();

        //open connection
        conGet.Open() 
        using (OleDbDataReader rdr = cmdGet.ExecuteReader())
        {
            while (rdr.Read())
            {
               parapraph.Append(rdr.GetString(0)).Append(" ");
            }
            rdr.Close();
        }

        richTextBox.Rtf = paragraph.ToString();
        // OR...
        richTextBox.Text = paragraph.ToString();

    }
    catch (Exception ex)
    {
        //display generic error message back to user
        MessageBox.Show(ex.Message);
    }
}

请注意,我还移动了其他一些东西 - 别担心,我确实正确地关闭了您的连接,即使您看不到它。

in my database, there is 1 column that has data in it (1 word per row in that column)

So you need many rows? Than you shouldn't use .ExecuteScalar(). Do this:

private void Form2_Load(object sender, EventArgs e)
{
    string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
    using (OleDbConnection conGet = new OleDbConnection(connectionString))
    using (OleDbCommand cmdGet = new OleDbCommand())
    try
    {
        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;
        cmdGet.CommandText = "SELECT * FROM Paragraph";

        StringBuilder paragraph = new StringBuilder();

        //open connection
        conGet.Open() 
        using (OleDbDataReader rdr = cmdGet.ExecuteReader())
        {
            while (rdr.Read())
            {
               parapraph.Append(rdr.GetString(0)).Append(" ");
            }
            rdr.Close();
        }

        richTextBox.Rtf = paragraph.ToString();
        // OR...
        richTextBox.Text = paragraph.ToString();

    }
    catch (Exception ex)
    {
        //display generic error message back to user
        MessageBox.Show(ex.Message);
    }
}

Notice that I moved a few other things around as well - and don't worry, I do close your connection properly, even though you can't see it.

抱着落日 2024-11-03 19:56:48

尝试使用 Text 属性,因为它只是一个字符串。

richTextBox.Text = cmdGet.ExecuteScalar().ToString();

Try using the Text property instead as it's just a string.

richTextBox.Text = cmdGet.ExecuteScalar().ToString();
未蓝澄海的烟 2024-11-03 19:56:48

您确定要选择数据库表中的所有列吗?
像这样:

cmdGet.CommandText = "SELECT * FROM Paragraph";

我想说这是不正确的,您只需要获取保存文本的一列,例如:

cmdGet.CommandText = "SELECT MyTextColumn FROM Paragraph"; //Maybe even som Where comparison!!

并且最好使用 richTextBox 的 Text 属性来填充它并使用 Scalar 上的 ToStirng 方法:

this.richTextBox1.Text = cmd.ExecuteScalar.Tostring();

Are you sure you want to select all columns from the dataBase table?
Like this:

cmdGet.CommandText = "SELECT * FROM Paragraph";

I would say this is not correct, you only need to get one column where the text is saved, like:

cmdGet.CommandText = "SELECT MyTextColumn FROM Paragraph"; //Maybe even som Where comparison!!

And it would be better to use Text property of the richTextBox to fill into it and ToStirng method on Scalar:

this.richTextBox1.Text = cmd.ExecuteScalar.Tostring();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文