G
Guest
I try to follow Steve's paper to build a database, and store a small text
file into SQL Server database and retrieve it later. Only difference between
my table and Steve's table is that I use NTEXT datatype for the file instead
of using IMAGE datatype. I can not use SqlDataReader to read the data. I need
your help, Thanks.
-David
(1) I have a table TestFile for testing:
ID int
FileName navrchar(255)
FileData ntext
ContentType nvarchar 32
FileSize int
UploadDate datetime
(2) I use the stored procedure to store the file. It works:
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
File1.PostedFile.InputStream.Read(bytContent, 0, iLength);
this.sqlCommand1.Parameters["@FileName"].Value=sFileName;
this.sqlCommand1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand1.Parameters["@FileData"].Value=encoding.GetString(bytContent);
this.sqlCommand1.Parameters["@ContentType"].Value=sContentType;
--------------------
(3) The problem is how to read the file from the table. The following is my
read process:
SqlDataReader dr;
cmdGetFile.Parameters["@ID"].Value=Request["ID"].ToString();
Response.Write(Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["ContentType"].ToString();
Response.OutputStream.Write((byte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHeader("Content-Disposition", "Attachment;filename=" +
dr["FileName"].ToString());
}
else
{
Response.Write("File Not Found."+ dr.Read().ToString());
}
dr.Close();
dbConn.Close();
file into SQL Server database and retrieve it later. Only difference between
my table and Steve's table is that I use NTEXT datatype for the file instead
of using IMAGE datatype. I can not use SqlDataReader to read the data. I need
your help, Thanks.
-David
(1) I have a table TestFile for testing:
ID int
FileName navrchar(255)
FileData ntext
ContentType nvarchar 32
FileSize int
UploadDate datetime
(2) I use the stored procedure to store the file. It works:
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
File1.PostedFile.InputStream.Read(bytContent, 0, iLength);
this.sqlCommand1.Parameters["@FileName"].Value=sFileName;
this.sqlCommand1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand1.Parameters["@FileData"].Value=encoding.GetString(bytContent);
this.sqlCommand1.Parameters["@ContentType"].Value=sContentType;
--------------------
(3) The problem is how to read the file from the table. The following is my
read process:
SqlDataReader dr;
cmdGetFile.Parameters["@ID"].Value=Request["ID"].ToString();
Response.Write(Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["ContentType"].ToString();
Response.OutputStream.Write((byte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHeader("Content-Disposition", "Attachment;filename=" +
dr["FileName"].ToString());
}
else
{
Response.Write("File Not Found."+ dr.Read().ToString());
}
dr.Close();
dbConn.Close();