Storing Word document in Sql table

Following code helps to insert doc file in sql table

>Check for attachment file if yes , insert fileid in master table and store document in child table

Sql Table structure:

Fileid [uniqueidentifier,not null]
filename [nvarchar(100),not null]
filetype[nvarchar(100),not null]
filesize[int,not null]
filecontent[varbinary(max),not null]

in aspx.cs:
protected void btnattachcheck_Click(object sender, EventArgs e)
{
if (fileupload1.HasFile)
{
if (fileupload1.FileName.ToUpper().EndsWith("DOC") || (fileupload1.FileName.ToUpper().EndsWith(".PDF")))
{
Guid Fileid = Guid.NewGuid();


cmd = new SqlCommand();
cmd.CommandText = @"insert into [Filestbl](FileId, filename, filetype, filesize, filecontent)
values(@Fileid, @filename, @filetype, @filesize, @filecontent)";
cmd.Parameters.Add("@Fileid", SqlDbType.UniqueIdentifier).Value = Fileid;
ViewState["fileid"] = Fileid;
cmd.Parameters.Add("@filename", SqlDbType.NVarChar, 100).Value = Path.GetFileName(fileupload1.PostedFile.FileName);
cmd.Parameters.Add("@filetype", SqlDbType.NVarChar, 100).Value = fileupload1.PostedFile.ContentType;
cmd.Parameters.Add("@filesize", SqlDbType.Int).Value = fileupload1.PostedFile.ContentLength;
byte[] fileContent = new byte[fileupload1.PostedFile.ContentLength];
fileupload1.PostedFile.InputStream.Read(fileContent, 0, fileupload1.PostedFile.ContentLength);
cmd.Parameters.Add("@filecontent", SqlDbType.VarBinary, -1).Value = fileContent;
cmd.Connection = new SqlConnection(constr);
cmd.Connection.Open();
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
lblattachname.Text = fileupload1.FileName;
}
}
else
{
lblattachname.Text = "please attach word or pdf file";
}

}
else
{
ViewState["fileid"] = "";
}

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: