在C# winform中使用 richtextbox 向access保存及读取 图文混排数据(有源码,帮忙改一下)
情况:我现有ACCESS数据库ziliao里面有"OLE对象“型字段bbb目标:使用richTextBox向数据库保存及读取图文混排数据我现在使用的代码:我使用数据库操作...
情况:我现有ACCESS数据库ziliao 里面有"OLE对象“型字段bbb
目标:使用richTextBox向数据库保存及读取图文混排数据
我现在使用的代码:
我使用数据库操作类进行数据库操作,如下
public static int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows; //>0代表执行成功 =0执行失败(没有添加数据)
}
catch (System.Data.OleDb.OleDbException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
从网上找到了保存用的代码
保存
private void button2_Click(object sender, EventArgs e) {
string mdbPath = System.IO.Path.Combine(Application.StartupPath, "rtf.mdb");
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbPath;
using (OleDbConnection cn = new OleDbConnection(connectionString)) {
cn.Open();
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO [demo] (rtf) VALUES (@rtf)", cn)) {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
rtbDemo.SaveFile(ms, RichTextBoxStreamType.RichText);
cmd.Parameters.Add("@rtf", OleDbType.VarBinary);
cmd.Parameters["@rtf"].Value = ms.ToArray();
cmd.ExecuteNonQuery();
}
}
cn.Close();
}
MessageBox.Show("保存成功!");
}
读取:
private void button3_Click(object sender, EventArgs e) {
string mdbPath = System.IO.Path.Combine(Application.StartupPath, "rtf.mdb");
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbPath;
using (OleDbConnection cn = new OleDbConnection(connectionString)) {
cn.Open();
using (OleDbCommand cmd = new OleDbCommand("SELECT TOP 1 rtf FROM [demo] ORDER BY [id] desc", cn)) {
byte[] btRTF = (byte[])cmd.ExecuteScalar();
if (btRTF == null) {
MessageBox.Show("数据库中目前没有数据,请先存入!");
}
else {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(btRTF)) {
rtbDemo.LoadFile(ms, RichTextBoxStreamType.RichText);
}
}
}
cn.Close();
}
}
请根据我使用的类改造一下保存及读取代码,谢(另个提问分也给你)
我在这里
http://zhidao.baidu.com/question/279279469.html
也问了相同的问题,如果问题解决的话,请您也到此处回答一下,好让我处理两个提问的分数 展开
目标:使用richTextBox向数据库保存及读取图文混排数据
我现在使用的代码:
我使用数据库操作类进行数据库操作,如下
public static int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows; //>0代表执行成功 =0执行失败(没有添加数据)
}
catch (System.Data.OleDb.OleDbException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
从网上找到了保存用的代码
保存
private void button2_Click(object sender, EventArgs e) {
string mdbPath = System.IO.Path.Combine(Application.StartupPath, "rtf.mdb");
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbPath;
using (OleDbConnection cn = new OleDbConnection(connectionString)) {
cn.Open();
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO [demo] (rtf) VALUES (@rtf)", cn)) {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
rtbDemo.SaveFile(ms, RichTextBoxStreamType.RichText);
cmd.Parameters.Add("@rtf", OleDbType.VarBinary);
cmd.Parameters["@rtf"].Value = ms.ToArray();
cmd.ExecuteNonQuery();
}
}
cn.Close();
}
MessageBox.Show("保存成功!");
}
读取:
private void button3_Click(object sender, EventArgs e) {
string mdbPath = System.IO.Path.Combine(Application.StartupPath, "rtf.mdb");
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + mdbPath;
using (OleDbConnection cn = new OleDbConnection(connectionString)) {
cn.Open();
using (OleDbCommand cmd = new OleDbCommand("SELECT TOP 1 rtf FROM [demo] ORDER BY [id] desc", cn)) {
byte[] btRTF = (byte[])cmd.ExecuteScalar();
if (btRTF == null) {
MessageBox.Show("数据库中目前没有数据,请先存入!");
}
else {
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(btRTF)) {
rtbDemo.LoadFile(ms, RichTextBoxStreamType.RichText);
}
}
}
cn.Close();
}
}
请根据我使用的类改造一下保存及读取代码,谢(另个提问分也给你)
我在这里
http://zhidao.baidu.com/question/279279469.html
也问了相同的问题,如果问题解决的话,请您也到此处回答一下,好让我处理两个提问的分数 展开
3个回答
展开全部
先重载方法,前一个是Sql,后一个是Sql参数集合
插入方法:
public static int ExecuteSql(string SQLString,OleDbParameterCollection parms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
cmd.Parameters.AddRange(parms);
cmd.ExecuteNonQuery();
}
connection.Close();
}
}
读取方法:
public object SqlRead(string sqlstring)
{
using (OleDbConnection cn = new OleDbConnection(connectionString))
{
cn.Open();
using (OleDbCommand cmd = new OleDbCommand(sqlstring, cn))
{
object obj=cmd.ExecuteScalar();
if (obj == null)
{
MessageBox.Show("数据库中目前没有数据,请先存入!");
}
else
{
return obj;
}
}
cn.Close();
}
}
上面两个方法呢,写到你的那个啥类里面的
调用的时候呢,就
插入:
System.IO.MemoryStream ms = new System.IO.MemoryStream();
this.richTextBox1.SaveFile(ms, RichTextBoxStreamType.RichText);
OleDbParameterCollection param= new OleDbParameterCollection();
param.Add(new OleDbParameter("@rtf",ms));
XX类的.ExecuteSql("insert into [demo] (rtf) values (@rtf)",param);
读取
byte[] buff=(byte[])XX类的.SqlRead("select top 1 rtf from [demo] order by [id] desc");
System.IO.MemoryStream ms = new System.IO.MemoryStream(btRTF);
rtbDemo.LoadFile(ms, RichTextBoxStreamType.RichText);
其中XX类是你那个啥类
插入方法:
public static int ExecuteSql(string SQLString,OleDbParameterCollection parms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
cmd.Parameters.AddRange(parms);
cmd.ExecuteNonQuery();
}
connection.Close();
}
}
读取方法:
public object SqlRead(string sqlstring)
{
using (OleDbConnection cn = new OleDbConnection(connectionString))
{
cn.Open();
using (OleDbCommand cmd = new OleDbCommand(sqlstring, cn))
{
object obj=cmd.ExecuteScalar();
if (obj == null)
{
MessageBox.Show("数据库中目前没有数据,请先存入!");
}
else
{
return obj;
}
}
cn.Close();
}
}
上面两个方法呢,写到你的那个啥类里面的
调用的时候呢,就
插入:
System.IO.MemoryStream ms = new System.IO.MemoryStream();
this.richTextBox1.SaveFile(ms, RichTextBoxStreamType.RichText);
OleDbParameterCollection param= new OleDbParameterCollection();
param.Add(new OleDbParameter("@rtf",ms));
XX类的.ExecuteSql("insert into [demo] (rtf) values (@rtf)",param);
读取
byte[] buff=(byte[])XX类的.SqlRead("select top 1 rtf from [demo] order by [id] desc");
System.IO.MemoryStream ms = new System.IO.MemoryStream(btRTF);
rtbDemo.LoadFile(ms, RichTextBoxStreamType.RichText);
其中XX类是你那个啥类
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询