数据库中的富文本框值存储

rich text box value store in database

我有一个名为 placeofstudent1 的富文本框。我想将富文本框中写入的所有内容存储在名为 Place 的变量中,以便我可以将该信息传递到数据库中。

try
    {

        string name = nameofstudent.Text;
        string father = fatherofstudent.Text;
        string mother = motherofstudent.Text;
        string id = stud_id.Text;
        string gender;
        if (male.IsChecked == true)
        {
            gender = "M";
        }
        else
            gender = "F";

        string studentAge = ageofstudent.Text;

        DateTime dt = Convert.ToDateTime(dob.Text);
        int Age = Convert.ToInt16(ageofstudent.Text);
                   
        string Place = placeofstudent1.Text;
    
        connect();
        con.Open();
    
        string saved = "insert into student_details (student_id,student_name,father_name,mother_name,gender,age,dob,place,class)values('" + id + "', '" + name + "','" + father + "','" + mother + "','" + gender + "','" + Age + "','" + dt + "','" + Place + "','" + x + "')";
    
        SqlCommand cmd = new SqlCommand(saved, con);
    
        cmd.ExecuteReader();
    
        con.Close();

如果我尝试使用 .Text 填充变量 Place,它不起作用。我可以使用什么方法使这成为可能?感谢您的帮助。

您使用了错误的控件。目前您使用的是 WinForms 控件,而不是 WPF 控件。

这是正确的 System.Windows.Controls.RichTextBox(注意不同的命名空间)。您会注意到 WPF 实现没有 Text 属性,而是 Document 属性.
您必须手动将内容(RichTextBox.Document,即FlowDocument)转换为字符串。

<Window>
  <RichTextBox x:Name="MyRichTextBox" />
</Window>


var richTextBoxContentRange = new TextRange(
  this.MyRichTextBox.Document.ContentStart, 
  this.MyRichTextBox.Document.ContentEnd);

string richTextBoxText = richTextBoxContentRange.Text;