简体   繁体   English

使用C#从数据库保存并检索rtf文本

[英]Save and retrive rtf text from database using c#

I just finished to save a text from a richtextbox into sql database. 我刚完成将文本从Richtextbox保存到sql数据库中。 Here you have the code: 这里有代码:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=MARIA-PC;Initial Catalog=Account;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[FISIER] (File_name,The_text) VALUES (@File_name,@The_text)", con);
        cmd.Parameters.Add("@File_name", textBox1.Text);
        cmd.Parameters.AddWithValue("@The_text", richTextBox1.Rtf);
        cmd.ExecuteNonQuery();
        con.Close();
    }

Here is the table Click here for table 这是桌子点击这里桌子

Now, my problem is that I don't know how to retrive the text from the database. 现在,我的问题是我不知道如何从数据库中检索文本。 As you see there the text I had saved into The_text and the data type is Text . 如您所见,我已保存到The_textText ,数据类型为Text I want to retrive the data back into richtextbox. 我想将数据检索回richtextbox。

A quick search would have revealed the answer to this. 快速搜索就会找到答案。 For example, here is some sample code that queries a database table... 例如,下面是一些查询数据库表的示例代码...

// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);

// 2. Call Execute reader to get query results
SqlDataReader rdr = cmd.ExecuteReader();

You can change the table name and fields to match your database. 您可以更改表名和字段以匹配您的数据库。

This code was found at http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson03 , but there are millions of other sites that will teach you this stuff. 可以在http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson03找到此代码,但是还有数百万其他网站可以教您这些知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM