在数据库中存储注释
Store Annotations in database
下面是打开文件注释的代码
如何加载注释并将其保存到数据库中?
数据库是 MS SQL 服务器,我知道如何读写文本、xml 和二进制文件。只需要知道使用什么样的流以及如何使用它。
我宁愿不保持与数据库的连接打开。像读取然后保存在内存中然后写入之类的东西?
using System.Windows.Annotations;
private FileStream streamAnno;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
/// Creates an annotation service based on
/// a FlowDocumentReader control.
AnnotationService service = AnnotationService.GetService(FlowDocumentPageViewer1);
/// Checks if the service has not been instantiated.
if (service == null)
{
/// Instantiates a FileStream object.
streamAnno = new FileStream("tip100annotations.xml", FileMode.OpenOrCreate);
/// Instantiates the annotation service.
service = new AnnotationService(FlowDocumentPageViewer1);
/// Creates an annotation store using the derived
/// XmlStreamStore object and the FileStream object.
System.Windows.Annotations.Storage.AnnotationStore store = new System.Windows.Annotations.Storage.XmlStreamStore(streamAnno);
/// Sets the AutoFlush property to true to ensure
/// that every annotation is flushed to the store automatically.
store.AutoFlush = true;
/// Enables the annotation service.
service.Enable(store);
}
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
/// Tries to get the annotation service.
AnnotationService service = AnnotationService.GetService(FlowDocumentPageViewer1);
/// If the annotation service is valid and is enabled
if (service != null && service.IsEnabled)
{
/// Disables the service.
service.Disable();
/// Closes the stream
///
if (streamAnno != null)
{
streamAnno.Close();
}
}
}
以为我得到的是用内存流解决的
流存储在 SQL 中作为 varbinary(max)
streamAnnoMem = new MemoryStream();
if (viewRawTextSdocsID != null)
{
byte[] annotation = AnnotationGetUser((int)viewRawTextSdocsID);
if (annotation.Length > 0)
{
streamAnnoMem.Write(annotation, 0, annotation.Length);
streamAnnoMem.Position = 0;
}
}
/// Instantiates the annotation service.
service = new AnnotationService(FlowDocumentPageViewer1);
/// Creates an annotation store using the derived
/// XmlStreamStore object and the FileStream object.
System.Windows.Annotations.Storage.AnnotationStore store = new System.Windows.Annotations.Storage.XmlStreamStore(streamAnnoMem);
/// Sets the AutoFlush property to true to ensure
/// that every annotation is flushed to the store automatically.
store.AutoFlush = true;
/// Enables the annotation service.
service.Enable(store);
AnnotationServiceActive = true;
下面是打开文件注释的代码
如何加载注释并将其保存到数据库中?
数据库是 MS SQL 服务器,我知道如何读写文本、xml 和二进制文件。只需要知道使用什么样的流以及如何使用它。
我宁愿不保持与数据库的连接打开。像读取然后保存在内存中然后写入之类的东西?
using System.Windows.Annotations;
private FileStream streamAnno;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
/// Creates an annotation service based on
/// a FlowDocumentReader control.
AnnotationService service = AnnotationService.GetService(FlowDocumentPageViewer1);
/// Checks if the service has not been instantiated.
if (service == null)
{
/// Instantiates a FileStream object.
streamAnno = new FileStream("tip100annotations.xml", FileMode.OpenOrCreate);
/// Instantiates the annotation service.
service = new AnnotationService(FlowDocumentPageViewer1);
/// Creates an annotation store using the derived
/// XmlStreamStore object and the FileStream object.
System.Windows.Annotations.Storage.AnnotationStore store = new System.Windows.Annotations.Storage.XmlStreamStore(streamAnno);
/// Sets the AutoFlush property to true to ensure
/// that every annotation is flushed to the store automatically.
store.AutoFlush = true;
/// Enables the annotation service.
service.Enable(store);
}
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
/// Tries to get the annotation service.
AnnotationService service = AnnotationService.GetService(FlowDocumentPageViewer1);
/// If the annotation service is valid and is enabled
if (service != null && service.IsEnabled)
{
/// Disables the service.
service.Disable();
/// Closes the stream
///
if (streamAnno != null)
{
streamAnno.Close();
}
}
}
以为我得到的是用内存流解决的
流存储在 SQL 中作为 varbinary(max)
streamAnnoMem = new MemoryStream();
if (viewRawTextSdocsID != null)
{
byte[] annotation = AnnotationGetUser((int)viewRawTextSdocsID);
if (annotation.Length > 0)
{
streamAnnoMem.Write(annotation, 0, annotation.Length);
streamAnnoMem.Position = 0;
}
}
/// Instantiates the annotation service.
service = new AnnotationService(FlowDocumentPageViewer1);
/// Creates an annotation store using the derived
/// XmlStreamStore object and the FileStream object.
System.Windows.Annotations.Storage.AnnotationStore store = new System.Windows.Annotations.Storage.XmlStreamStore(streamAnnoMem);
/// Sets the AutoFlush property to true to ensure
/// that every annotation is flushed to the store automatically.
store.AutoFlush = true;
/// Enables the annotation service.
service.Enable(store);
AnnotationServiceActive = true;