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

时间:2016-05-01 16:50:23

标签: c# mysql sql-server database

我刚刚完成将文本从richtextbox保存到sql数据库中。这里有代码:

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();
    }

这是表格Click here for table

现在,我的问题是我不知道如何从数据库中检索文本。正如您在那里看到的那样,我保存到The_text的文本中,数据类型为Text。我想将数据恢复回richtextbox。

1 个答案:

答案 0 :(得分:0)

快速搜索可以揭示答案。例如,下面是一些查询数据库表的示例代码...

// 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();

您可以更改表名称和字段以匹配您的数据库。

此代码在http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson03找到,但有数百万其他网站会教你这些东西。

相关问题