Stream sDocStream;
int docLen;
string contentType;
string uploadDocName;
string loc = "test";
sDocStream = uploadfile.PostedFile.InputStream;
docLen = uploadfile.PostedFile.ContentLength;
uploadDocName = uploadfile.PostedFile.FileName;
byte[] docBinaryData=new byte[docLen];
sDocStream.Read(docBinaryData, 0, docLen);
contentType = "doc";
String connectionString = ConfigurationSettings.AppSettings["dbconn"].ToString();
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "INSERT INTO [test] ([Title], [Loc], [contentType], [docLen], [docData]) VALUES (@Title, @Loc, @contentType, @docLen, @docData)";
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(queryString, sqlConnection);
uploadDocName = uploadDocName.Substring(uploadDocName.LastIndexOf(@"\")+1);
sqlCommand.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar).Value = uploadDocName;
sqlCommand.Parameters.Add("@Loc", System.Data.SqlDbType.NVarChar).Value = loc;
sqlCommand.Parameters.Add("@contentType",System.Data.SqlDbType.VarChar).Value = contentType;
sqlCommand.Parameters.Add("@docLen", System.Data.SqlDbType.BigInt).Value = docBinaryData.Length;
sqlCommand.Parameters.Add("@docData", System.Data.SqlDbType.Image).Value = docBinaryData;
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message + "<HR>");
}
finally
{
sqlConnection.Close();
}