Tuesday, November 18, 2008

Uplaoding Resume to Sql server 2000 using c#.net

CREATE TABLE [dbo].[tbl_documents]
(
document_id int IDENTITY (1, 1) NOT NULL ,
document image,
docContentType nvarchar (20),
docFileName nvarchar(255) ,
docFileSize nvarchar(7)
)

CREATE PROCEDURE [dbo].[uploadDoc]
(
@docContentType NVARCHAR(20),
@docFileName NVARCHAR(255),
@docFileSize NVARCHAR(7),
@document IMAGE
)

AS

INSERT INTO dbo.tbl_documents
(docContentType, docFileName, docFileSize,document)
VALUES
(@docContentType, @docFileName, @docFileSize,@document)
GO

GRANT EXEC ON [dbo].[uploadDoc] TO
GO

CREATE PROCEDURE [dbo].[downloadDoc]
(
@document_id INT,
)

AS

SELECT docContentType, docFileName, docFileSize,document)
FROM tbl_documents
WHERE document_id = @document_id

GO

GRANT EXEC ON [dbo].[downloadDoc] TO
GO





UPLOAD: (In button event handler)

string contenttype = "";
string filename = "";
int filesize = 0;
byte[] DocBuffer = null;
System.IO.Stream str;
//fuDoc is a fileupload control
if(this.fuDoc.HasFile)
{
contenttype = this.fuDoc.PostedFile.ContentType;
filename = this.fuDoc.PostedFile.FileName;
filesize = this.fuDoc.PostedFile.ContentLength;
DocBuffer = new byte[filesize];
str = this.fuDoc.PostedFile.InputStream;
str.Read(DocBuffer,0,filesize);
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["<storedconnectionstring>"].ToString());
SqlCommand cmdNewDocument = new SqlCommanduploadDoc",conn);
cmdNewDocument.CommandType = CommandType.StoredProcedure;
cmdNewDocument.Parameters.Add("@document_id", SqlDbType.Int);
cmdNewDocument.Parameters.Add("@docContentType", SqlDbType.NVarChar,20);
cmdNewDocument.Parameters.Add("@docFileName", SqlDbType.NVarChar,110);
cmdNewDocument.Parameters.Add("@docFileSize", SqlDbType.NVarChar,7);
cmdNewDocument.Parameters.Add("@document", SqlDbType.Image);
cmdNewDocument.Parameters["@document_id"].Value = DBNull.Value;
cmdNewDocument.Parameters["@docContentType"].Value = contenttype;
cmdNewDocument.Parameters["@docFileName"].Value = filename;
cmdNewDocument.Parameters["@docFileSize"].Value = filesize.ToString();
cmdNewDocument.Parameters["@document"].Value = DocBuffer;

try
{
conn.Open();
cmdNewDocument.ExecuteNonQuery();
}
catch (SqlException sqle)
{
//appropriate code
}
catch (Exception gen)
{
//appropriate code
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
conn.Dispose();
}DOWNLOAD: (I put this code in a Page_Load Event)

Response.Buffer = true;
string docid = Request.QueryString["docid"];

SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["<storedconnectionstring>"].ToString();
SqlCommand cmdExport = new SqlCommand();
cmdExport.CommandType = CommandType.StoredProcedure;
cmdExport.CommandText = "downloadDoc";cmdExport.Connection = conn;

try
{
conn.Open();
SqlDataReader rdrExport = cmdExport.ExecuteReader();
if (rdrExport.HasRows)
{
rdrExport.Read();
string fileName = rdrExport.GetValue(rdrExport.GetOrdinal("docFileName")).ToString();
byte[] buffer = new byte[Convert.ToInt32(rdrExport.GetValue(rdrExport.GetOrdinal("docFileSize")).ToString())];
buffer = rdrExport.GetSqlBytes(rdrExport.GetOrdinal("document")).Buffer;
Response.ContentType = rdrExport.GetValue(rdrExport.GetOrdinal("docContentType")).ToString();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
Response.AddHeader("Content-Length", rdrExport.GetValue(rdrExport.GetOrdinal("docFileSize")).ToString());

Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
rdrExport.Close();
}
catch (SqlException sqle)
{
//appropriate code
}
catch (Exception gen)
{
//appropriate code
}
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
conn.Dispose();
}

No comments:

 
Feedback Form