如何往access数据库中插入/读取richtext内容?
建好的数据库中的字段“内容”,用来保存的内容包含特殊字符和格式,现在采取的办法是用richtextbox控件,把其中的所有内容(包括格式)保存到数据库。但不清楚在数据库设计时应该用哪种数据类型?如何用程序将这些内容保存到access数据库中?怎样从access中读取出来?
[解决办法]
保存到数据库的内容:
byte[] b=System.Text.Encoding.Default.GetBytes(this.richTextBox1.Rtf);
读取:
SqlConnection con=new SqlConnection( "... ");
string str= "select 内容 from 数据库 where ... ";//确保只返回一行
SqlCommand comm=new SqlCommand(str,con);
if (con.State==ConnectionState.Closed)
con.Open();
byte[] b=(byte[])comm.ExecuteScalar();
MemoryStream stream=new MemoryStream(b,true);
stream.Write(b,0,b.Length);
this.richTextBox1.Clear();
string s=System.Text.Encoding.Default.GetString(b,0,b.Length);
this.richTextBox1.Rtf=@s;
stream.Close();
——以上C#程序 by bitsbird(一瓢 在路上...)
[解决办法]
使用参数对象。