C# .NET - how to save html file in windows application
Asked By Reena Jain
04-Feb-10 11:25 PM
I am craeating a windows application with ms access database.
I have to save an html file which is created by tinemce. So how to store it in database.
Thanks in advance
Web Star replied to Reena Jain
u can store a file as stream in database
FileStream st = new FileStream(@"C:\filename.html", FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();
Note above is just an example to show how it works, when you deal with a big file integer may be too short, in which case you should read the file more than once into the byte array.
Finally, pass the byte array into the database:
Code Snippet
SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand("UPDATE SomeTable SET
colnmae=@vlauename WHERE ID = 1", conn);
cmd.Parameters.AddWithValue("@
vlauename", buffer);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
Santhosh N replied to Reena Jain
you caould actually have a blob column in the table in the sql server and store the encoded html file content inthe column with the respective filename as other column..while retrieving the html content decode the same and display in the page...
This is how you can encode and decode the html strings in c#
// Encode the string.
encdString = HttpUtility.HtmlEncode(htmlString);
you can save this in DB...
while you want to get the html string just bring it from Db and decode as
StringWriter myWriter = new StringWriter();
// Decode the encoded string.
HttpUtility.HtmlDecode(encStringFromDB, myWriter);
myWriter.ToString() gives the html...
Huggy Bear replied to Reena Jain
The definition for BLOB is BinaryLargeObject and doing a HtmlEncode will not provide you any kind of blob data rather it would only give you a lengthy Html encoded string. So you should be reading into a byte[] and sending it to the database.
I think the column datatype for storing BLOB data in database is OLE Object, if I am not wrong.
Use FileStream to read the HtmlFile as a byte[]
//provide the htmlfilename for the stream
FileStream fs = File.OpenRead("filename.html");
//instanciate the byte[] and set the length based on the filestream length
byte[] data = new byte[fs.Length];
//Read the file content into blob
fs.Read (data, 0, data.Length);
Here is a good http://www.codeproject.com/KB/database/AccessBlob.aspx, which speaks about upload blob data to access.

summary> / / / <param name = "command"> The SqlCommand to be prepared< / param> / / / <param name = "connection"> A valid SqlConnection, on which to execute this command< / param> / / / <param name = "transaction"> A valid SqlTransaction, or 'null was opened by the method, otherwose is false.< / param> private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if( command = = null are provided if (commandParameters ! = null) { AttachParameters(command, commandParameters); } return; } #endregion private utility methods & constructors #region ExecuteNonQuery / / / <summary> / / / Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in / / / the connection string / / / < / summary> / / / <remarks> / / / e.g.: / / / int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders"); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "commandType"> The CommandType (stored procedure, text, etc.)< / param> / / / <param name = "commandText"> The An int representing the number of rows affected by the command< / returns> public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText) { / / Pass through the call providing null for the set of SqlParameters return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null); } / / / <summary> / / / Execute a SqlCommand (that returns no resultset) against the specified in the connection string / / / using the provided parameters / / / < / summary> / / / <remarks> / / / e.g.: / / / int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); / / / < / remarks> / / / <param name = "connectionString"> A valid connection
ReadOnly = true ; SetValue(empid); } } private void SetValue( string empid) { string constr = ConfigurationManager .ConnectionStrings[ "ConStr" ].ToString(); SqlConnection con = new SqlConnection (constr); con.Open(); SqlCommand cmd = new SqlCommand ( "Usp_SetEmpById" , con); cmd.Parameters.AddWithValue( "@empid" , empid); cmd.CommandType = CommandType .StoredProcedure; SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { txtEmpName.Text EventArgs e) { string empid = ( string )Request.QueryString[ "empid" ]; try { string constr = ConfigurationManager .ConnectionStrings[ "ConStr" ].ToString(); SqlConnection con = new SqlConnection (constr); con.Open(); SqlCommand cmd = new SqlCommand ( "[Usp_UpdateProfile]" , con); cmd.Parameters.AddWithValue( "@name" , txtEmpName.Text); cmd.Parameters.AddWithValue( "@mysiteurl" , txtMySiteURL.Text); cmd.Parameters.AddWithValue( "@ro" , txtRO.Text); cmd.Parameters.AddWithValue( "@email" , txtEMail.Text); / / cmd.Parameters.AddWithValue("@resume", FileUpload1. . ToString
empid ! = null ) { SetValue(empid); } } private void SetValue( string empid) { string constr = ConfigurationManager .ConnectionStrings[ "ConStr" ].ToString(); SqlConnection con = new SqlConnection (constr); con.Open(); SqlCommand cmd = new SqlCommand ( "Usp_SelEmpById" , con); cmd.Parameters.AddWithValue( "@empid" , empid); cmd.CommandType = CommandType .StoredProcedure; SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { txtname.Text EventArgs e) { string empid = ( string )Request.QueryString[ "empid" ]; { try { string constr = ConfigurationManager .ConnectionStrings[ "ConStr" ].ToString(); SqlConnection con = new SqlConnection (constr); con.Open(); SqlCommand cmd = new SqlCommand ( "[Usp_UpdateEmployee]" , con); cmd.Parameters.AddWithValue( "@name" , txtname.Text); cmd.Parameters.AddWithValue( "@salary" , txtsalary.Text); cmd.Parameters.AddWithValue( "@doj" , Convert .ToDateTime(txtDOJ.Text)); cmd.Parameters.AddWithValue( "@dob" , Convert .ToDateTime(txtDOB.Text)); cmd.Parameters
string parameter that holds the command you want to execute and a reference to a SqlConnection object. SqlCommand has a few overloads, which you will see in the examples of this section later in this lesson. Inserting Data To insert data into a database, use the ExecuteNonQuery method of the SqlCommand object. The following code shows how to insert data into a new command with a query and connection SqlCommand cmd = new SqlCommand(insertString, conn); / / 2. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery(); The SqlCommand instantiation is just a little different from what you've seen before, but such as CategoryID, will generate an exception. To execute this command, we simply call the ExecuteNonQuery method on the SqlCommand instance, cmd. This code is part of the Insertdata method of 1 in the Putting it All Together section later in this lesson. Updating Data The ExecuteNonQuery method is also used for updating data. The following code shows how to update data SqlCommand cmd = new SqlCommand(updateString); / / 2. Set the Connection property cmd.Connection = conn; / / 3. Call ExecuteNonQuery to send command cmd.ExecuteNonQuery(); Again, we put the SQL command into a string variable, but this time we used