Add Birthday
File upload MS Sql server DB using asp.net
3 replies to this topic
Tinku Krishnan
#1
|
Member
14
Points
|
Posted on
29 Jul 2012 11:27 AM IST
Dear Friend,
Please tel me,how to upload files(zip,exe,jpeg)in msSQL DB using asp.net web form.
|
Dot Net
899
views
Reply to this topic
|
Jahnavi N
#2
|
Member
164
Points
|
Replied on
07 Aug 2012 06:03 AM IST
Hi,
.ASPX Code...
-------------------------
.ASPX.CS Code...
-------------------------
void btnUploadFile_Click(object sender, EventArgs e)
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
command.CommandText = @"insert into [File](FileName, FileType, FileSize, FileContent)
values(@FileName, @FileType, @FileSize, @FileContent)";
command.Parameters.Add("@FileName", SqlDbType.NVarChar, 100).Value = Path.GetFileName(ctrFile.PostedFile.FileName);
command.Parameters.Add("@FileType", SqlDbType.NVarChar, 100).Value = ctrFile.PostedFile.ContentType;
command.Parameters.Add("@FileSize", SqlDbType.Int).Value = ctrFile.PostedFile.ContentLength;
// filecontent, convert from stream to byte array
byte[] fileContent = new byte[ctrFile.PostedFile.ContentLength];
ctrFile.PostedFile.InputStream.Read(fileContent, 0, ctrFile.PostedFile.ContentLength);
command.Parameters.Add("@FileContent", SqlDbType.VarBinary, -1).Value = fileContent;
command.Connection.Open();
command.ExecuteNonQuery();
}
}
I hope this will help you.
Thanks,
Jahnavi
|
Reply to this topic
|
Vimlesh Kumar
#3
|
Member
14
Points
|
Replied on
16 Aug 2012 12:06 PM IST
File Uoload Button Click Event
try
{
if (FileUpload1.PostedFile.ContentLength > 0)
{
// Get the File name and Extension
string strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string strFileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
// Extract the content of the Document into a Byte array
int intlength = FileUpload1.PostedFile.ContentLength;
Byte[] byteData = new Byte[intlength];
FileUpload1.PostedFile.InputStream.Read(byteData, 0, intlength);
//
// Save the file to the DB
string connection = ConfigurationManager.AppSettings["sample"].ToString();
SqlConnection objConn = new SqlConnection(connection);
SqlCommand objCmd = new SqlCommand("InsertDocument", objConn);
//
objCmd.CommandType = CommandType.StoredProcedure;
//
SqlParameter pFileName = new SqlParameter("FileName", SqlDbType.VarChar);
pFileName.Size = 50;
pFileName.Direction = ParameterDirection.Input;
pFileName.Value = strFileName;
//
SqlParameter pExtension = new SqlParameter("Extension", SqlDbType.VarChar);
pExtension.Size = 5;
pExtension.Direction = ParameterDirection.Input;
pExtension.Value = strFileExtension;
//
SqlParameter pFileContent = new SqlParameter("FileContent", SqlDbType.VarBinary);
pFileContent.Size = byteData.Length;
pFileContent.Direction = ParameterDirection.Input;
pFileContent.Value = byteData;
//
objCmd.Parameters.Add(pFileName);
objCmd.Parameters.Add(pExtension);
objCmd.Parameters.Add(pFileContent);
//
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
//
lblMsg.Text = "Document Uploaded Succesfully";
}
}
catch (Exception ex)
{
lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
}
=========%%%%%%%%%Code Uoload File====%%%%%%%
|
Reply to this topic
|
Vimlesh Kumar
#4
|
Member
14
Points
|
Replied on
16 Aug 2012 12:06 PM IST
File Uoload Button Click Event
try
{
if (FileUpload1.PostedFile.ContentLength > 0)
{
// Get the File name and Extension
string strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string strFileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
// Extract the content of the Document into a Byte array
int intlength = FileUpload1.PostedFile.ContentLength;
Byte[] byteData = new Byte[intlength];
FileUpload1.PostedFile.InputStream.Read(byteData, 0, intlength);
//
// Save the file to the DB
string connection = ConfigurationManager.AppSettings["sample"].ToString();
SqlConnection objConn = new SqlConnection(connection);
SqlCommand objCmd = new SqlCommand("InsertDocument", objConn);
//
objCmd.CommandType = CommandType.StoredProcedure;
//
SqlParameter pFileName = new SqlParameter("FileName", SqlDbType.VarChar);
pFileName.Size = 50;
pFileName.Direction = ParameterDirection.Input;
pFileName.Value = strFileName;
//
SqlParameter pExtension = new SqlParameter("Extension", SqlDbType.VarChar);
pExtension.Size = 5;
pExtension.Direction = ParameterDirection.Input;
pExtension.Value = strFileExtension;
//
SqlParameter pFileContent = new SqlParameter("FileContent", SqlDbType.VarBinary);
pFileContent.Size = byteData.Length;
pFileContent.Direction = ParameterDirection.Input;
pFileContent.Value = byteData;
//
objCmd.Parameters.Add(pFileName);
objCmd.Parameters.Add(pExtension);
objCmd.Parameters.Add(pFileContent);
//
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
//
lblMsg.Text = "Document Uploaded Succesfully";
}
}
catch (Exception ex)
{
lblMsg.Text = " Error uploading Document: " + ex.Message.ToString();
}
|
Reply to this topic
|
|
|