如何使用实体框架将richtextbox存储到WPF中的sqlserver数据库 [英] How to store richtextbox to sqlserver database in WPF using entity framework

查看:70
本文介绍了如何使用实体框架将richtextbox存储到WPF中的sqlserver数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#和WPF有点新,并且遇到了一个非常令人沮丧的问题,即使用实体框架将带格式的richtextbox存储到我的数据库中。我在下面附上了一些代码。



请帮我调试一下,我在ConvertRtbToBytes函数上得到错误......'string'不包含'Document'的定义,没有可访问的扩展方法'Document'接受string类型的第一个参数(你是否缺少using指令或汇编引用?)



我也确定一般代码不好,任何帮助都会受到大力赞赏。



这是我的代码。



我尝试了什么:



I'm a bit new to C# and WPF and experiencing a very frustrating issue storing a richtextbox with formatting to my database using entity framework. I've attached some code below.

Please help me debug it, I'm getting the error on the ConvertRtbToBytes Function that... 'string' does not contain definition for 'Document' and no accessible extension method 'Document' accepting a first argument of type string could be found (are you missing a using directive or an assembly reference?)

I am also sure the general code is not Okay, any help will be massively appreciated.

Here is my code.

What I have tried:

namespace simpleton   
 public partial class AddNewOE : Window   
   {   
      //db connection           
      simpletonDBEntities _db = new simpletonDBEntities();  
      public AddNewOE()  
      {  
         InitializeComponent();  
       }  
  
       private void insertobBtn_Click(object sender, RoutedEventArgs e)  
       {  
   
           db_entry newdb_entries = new db_entry()   
          {   
             ReportDetails = ConvertRtbToBytes(rtfText)  
           };               
            _db.ob_entries.Add(newOb_entries);           
           _db.SaveChanges();   
        }  
       public static byte[] ConvertRtbToBytes(string richTextBox)   
       {   
            byte[] data = null; string rtfText; //string to save to db   
           TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);   
           using (MemoryStream ms = new MemoryStream())   
           {               
                   tr.Save(ms, DataFormats.Rtf);              
                   rtfText = Encoding.ASCII.GetString(ms.ToArray());  
             }   
            return data;  
         }  
     }  
  
}  

推荐答案

你的函数ConvertRtbToBytes( string richTextBox)请求一个字符串但是你不需要那里的RichtTextBox对象而不是字符串吗?该Object包含.Document属性。字符串没有
Your function ConvertRtbToBytes(string richTextBox) asks for a string but ain't you needing the RichtTextBox object there in stead of the string? That Object holds the .Document property. A string does not have that


这篇关于如何使用实体框架将richtextbox存储到WPF中的sqlserver数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆