In my previous blog i discussed how can we maintain rich text format in SQL.
I got better and easier way to maintain rich text format in SQL.
Here is the code to save the rich text format in SQL (Create a table myRTF with column name Descriptionrtf):
string conString = "DB Connection string";
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter("Select Descriptionrtf From myRTF ", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
char[] arr = richTextBox1.Rtf.ToCharArray();
string str = string.Empty;
for(int i = 0; i < arr.Length; i++)
str += arr[i].ToString();
da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Descriptionrtf"] = str;
ds.Tables["MyImages"].Rows.Add(myRow);
da.Update(ds, "MyImages");
con.Close();
Code to load date from SQL to rich text box :
string conString = "DB Connection string";
SqlConnection connection = new SqlConnection(conString );
SqlCommand command = new SqlCommand("Select Descriptionrtf From MyImages", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
try
{
richTextBox1.Rtf = reader.GetString(0) ;
}
catch(Exception ex1)
{
MessageBox.Show(ex1.Message);
}
}
reader.close();
Happy Coding :)
Thursday, June 26, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
I think everyone work on SQL so this post is useful for everyone. This post explains a better way to maintain a rich text format in SQL. The method is very simple to understand. The logic behind the code is easily understandable. Thanks for sharing your experience with us.
Post a Comment