下面是打开/来自文件
的注释的代码如何将注释加载并保存到数据库?
数据库是MS SQL Server,我知道如何读写text,xml和binary。只需要知道使用什么类型的流以及如何使用它。
我宁愿不保持与数据库打开的连接。有点像读取然后保存在内存然后写?
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();
}
}
}
答案 0 :(得分:0)
认为我用内存流解决了
流作为varbinary(max)
存储在SQL中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;