Read/Write RichEditBox 到通用的 Sqlite3 Windows 使用 C# 的平台
Read/Write RichEditBox to Sqlite3 for Universal Windows Platform using C#
寻求一些方便的方法将 RichEditBox 控制文档保存/加载到 SQLite3 Blob 字段(也可以在文本字段中吗?)
(如果可能不丢失格式。如果不能,不格式化。)
它的 Windows IOT 和我使用 Sqlite3.PCL nuget
这是您可以参考的解决方案。我使用 Microsoft.Data.Sqlite
nuget 包并在 Raspberry Pi 3 上使用 Windows 10 IoT Core(版本:10.0.16299.192)对其进行测试。
MainPage.xaml
:
<StackPanel VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<RichEditBox x:Name="editor" Height="200" />
<Button Content="Save" Click="Save_Click" />
<Button Content="Load" Click="Load_Click" />
</StackPanel>
MainPage.xaml.cs
:
private async void SaveContent()
{
byte[] bytes;
StorageFile file = await DownloadsFolder.CreateFileAsync("file.rtf", CreationCollisionOption.GenerateUniqueName);
if (file != null)
{
// write to file
using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);
randAccStream.Seek(0);
var dataReader = new DataReader(randAccStream);
await dataReader.LoadAsync((uint)randAccStream.Size);
bytes = new byte[randAccStream.Size];
dataReader.ReadBytes(bytes);
}
SqliteConnection connection = new SqliteConnection("Filename=richEditBox.db");
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS myTable (
content BLOB
)
";
command.ExecuteNonQuery();
var insertCommand = connection.CreateCommand();
insertCommand.CommandText =
@"
INSERT INTO myTable (content)
VALUES (@content)
";
insertCommand.Parameters.Add("@content", SqliteType.Blob, bytes.Length);
insertCommand.Parameters["@content"].Value = bytes;
var task1 = insertCommand.ExecuteNonQueryAsync();
}
}
private async void LoadContent()
{
object[] values = new object[2];
SqliteConnection connection = new SqliteConnection("Filename=richEditBox.db");
connection.Open();
var retrieveCommand = connection.CreateCommand();
retrieveCommand.CommandText =
@"SELECT * from myTable";
var reader = retrieveCommand.ExecuteReader();
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine(reader.GetValues(values));
}
var dataBytes = values[0] as byte[];
var dataBuffer = dataBytes.AsBuffer();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await randomAccessStream.WriteAsync(dataBuffer);
randomAccessStream.Seek(0);
editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randomAccessStream);
}
private void Save_Click(object sender, RoutedEventArgs e)
{
SaveContent();
}
private void Load_Click(object sender, RoutedEventArgs e)
{
LoadContent();
}
寻求一些方便的方法将 RichEditBox 控制文档保存/加载到 SQLite3 Blob 字段(也可以在文本字段中吗?)
(如果可能不丢失格式。如果不能,不格式化。)
它的 Windows IOT 和我使用 Sqlite3.PCL nuget
这是您可以参考的解决方案。我使用 Microsoft.Data.Sqlite
nuget 包并在 Raspberry Pi 3 上使用 Windows 10 IoT Core(版本:10.0.16299.192)对其进行测试。
MainPage.xaml
:
<StackPanel VerticalAlignment="Center" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<RichEditBox x:Name="editor" Height="200" />
<Button Content="Save" Click="Save_Click" />
<Button Content="Load" Click="Load_Click" />
</StackPanel>
MainPage.xaml.cs
:
private async void SaveContent()
{
byte[] bytes;
StorageFile file = await DownloadsFolder.CreateFileAsync("file.rtf", CreationCollisionOption.GenerateUniqueName);
if (file != null)
{
// write to file
using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);
randAccStream.Seek(0);
var dataReader = new DataReader(randAccStream);
await dataReader.LoadAsync((uint)randAccStream.Size);
bytes = new byte[randAccStream.Size];
dataReader.ReadBytes(bytes);
}
SqliteConnection connection = new SqliteConnection("Filename=richEditBox.db");
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS myTable (
content BLOB
)
";
command.ExecuteNonQuery();
var insertCommand = connection.CreateCommand();
insertCommand.CommandText =
@"
INSERT INTO myTable (content)
VALUES (@content)
";
insertCommand.Parameters.Add("@content", SqliteType.Blob, bytes.Length);
insertCommand.Parameters["@content"].Value = bytes;
var task1 = insertCommand.ExecuteNonQueryAsync();
}
}
private async void LoadContent()
{
object[] values = new object[2];
SqliteConnection connection = new SqliteConnection("Filename=richEditBox.db");
connection.Open();
var retrieveCommand = connection.CreateCommand();
retrieveCommand.CommandText =
@"SELECT * from myTable";
var reader = retrieveCommand.ExecuteReader();
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine(reader.GetValues(values));
}
var dataBytes = values[0] as byte[];
var dataBuffer = dataBytes.AsBuffer();
InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
await randomAccessStream.WriteAsync(dataBuffer);
randomAccessStream.Seek(0);
editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randomAccessStream);
}
private void Save_Click(object sender, RoutedEventArgs e)
{
SaveContent();
}
private void Load_Click(object sender, RoutedEventArgs e)
{
LoadContent();
}