数据库到富文本框问题
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法将任何文本添加到
RichTextBo.Rtf
属性,它必须采用 RTF 格式。例如尝试一下:
它会抛出相同的异常。
您可以使用
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:
it will throw the the same exception.
You can use the
RichTextBox.Text
property instead, that can accept a plain text.所以您需要很多行?那么你不应该使用
.ExecuteScalar()
。这样做:请注意,我还移动了其他一些东西 - 别担心,我确实正确地关闭了您的连接,即使您看不到它。
So you need many rows? Than you shouldn't use
.ExecuteScalar()
. Do this: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.
尝试使用 Text 属性,因为它只是一个字符串。
Try using the Text property instead as it's just a string.
您确定要选择数据库表中的所有列吗?
像这样:
我想说这是不正确的,您只需要获取保存文本的一列,例如:
并且最好使用 richTextBox 的 Text 属性来填充它并使用 Scalar 上的 ToStirng 方法:
Are you sure you want to select all columns from the dataBase table?
Like this:
I would say this is not correct, you only need to get one column where the text is saved, like:
And it would be better to use Text property of the richTextBox to fill into it and ToStirng method on Scalar: