
一。将特殊格式的文字内容插入到Sql Server 2000数据库中。
string strconn = "server=StarK;database=Super_Sub_Test;uid=sa;pwd=sa";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
//执行存储语句
string sqlStr = "insert into rtf(rtfRead) values(@rtfRead)";
SqlCommand myCommand ;
try
{
myCommand = new SqlCommand(sqlStr, conn);
myCommand.Parameters.Add(new SqlParameter("@rtfRead", SqlDbType.Image));
}
catch
{
return ;
}
try
{
byte[] biStr = new byte[Convert.ToInt32(richTextBox1.Text.ToString().Length )];///????
biStr=System.Text.Encoding.Unicode.GetBytes(richTextBox1.Rtf);
myCommand.Parameters["@rtfRead"].Value = biStr;
}
catch
{
return ;
}
try
{
myCommand.ExecuteNonQuery();
}
catch
{
return ;
}
conn.Close();
二,将特殊字符从数据库中读取出来
richTextBox2.Text = "";
string strconn = "server=StarK;database=Super_Sub_Test;uid=sa;pwd=sa";
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
byte[] DocBody = null;
string selStr = "select rtfRead from rtf where ID=6";
SqlCommand myCommand = new SqlCommand(selStr, conn);
System.Data.SqlClient.SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
System.Data.SqlTypes.SqlBinary sqlB;
sqlB = myReader.GetSqlBinary(0);
if (sqlB.Value.Length > 0)
{
DocBody = new Byte[sqlB.Value.Length];
sqlB.Value.CopyTo(DocBody, 0);
}
//richTextBox2.Text = System.Text.UnicodeEncoding.Unicode.GetBytes(DocBody);
//biStr=System.Text.EncodingUnicode.GetBytes(richTextBox1.Rtf);
this.richTextBox2.Rtf = System.Text.UnicodeEncoding.Unicode.GetString(DocBody);
}
myReader.Close();
conn.Close();
代码讲述的实现,而没有讲求代码实现的效率。可以在这个基础上优化代码
