473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

I can not read a small file from NTEXT field in the database

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();
--------------------

dr.read() return FALSE! I do not why?

Feb 26 '07 #1
3 2915
Why did you use ntext to store a file instead of Image? That's the purpose
of Image, basically a binary large object. That wouldn't be a ntext datatype
because that's a double-byte character field and not a binary large object.
A text file should still be a blob, unless of course it's just plain text
with no other file information associated with it.

You are ensuring that the database is actually performing the query
correctly? If you're working with SQL Server locally, fire up the SQL
Profiler to see what is actually going on. You may find that there is a
problem with actually executing the query, or at the very least you can see
the exact call to the db and then run that yourself in the management studio
to determine if there really are results coming back.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"david" <da***@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
>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();
--------------------

dr.read() return FALSE! I do not why?

Feb 26 '07 #2
Thank you.

I store an XML file as a metadata for many complicated image files stored in
remote file systems (thety are in different formats, use different image
access/processing tools, and so on). Image tools will retrieve files and
process them based on the metadata. Those files are in special format.

Do you have some good way to handle it?

I will try to use binary format. However, is it searchable?

David

"Mark Fitzpatrick" wrote:
Why did you use ntext to store a file instead of Image? That's the purpose
of Image, basically a binary large object. That wouldn't be a ntext datatype
because that's a double-byte character field and not a binary large object.
A text file should still be a blob, unless of course it's just plain text
with no other file information associated with it.

You are ensuring that the database is actually performing the query
correctly? If you're working with SQL Server locally, fire up the SQL
Profiler to see what is actually going on. You may find that there is a
problem with actually executing the query, or at the very least you can see
the exact call to the db and then run that yourself in the management studio
to determine if there really are results coming back.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"david" <da***@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
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();
--------------------

dr.read() return FALSE! I do not why?



Feb 26 '07 #3
Which version of SQL Server are you on? If it's SQL 2005 you could use the
new XML datatype instead.

Are you simply storing the contents of the XML file, or the XML file itself?
Keep in mind, that a file is a collection of contents, plus other
information, which is why a blob format is better for true files. If you're
just storing the contents of a file, then the ntext or XML datatypes would
be fine.

One of the things I've found though when returning no results is to really
ensure that the query is executing like you think. Your issue may be very
simple. It could be that the querystring variable is coming up null because
of a goof in the URL, or that the where clause in the stored procedure has
some invalid condition.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"david" <da***@discussions.microsoft.comwrote in message
news:5B**********************************@microsof t.com...
Thank you.

I store an XML file as a metadata for many complicated image files stored
in
remote file systems (thety are in different formats, use different image
access/processing tools, and so on). Image tools will retrieve files and
process them based on the metadata. Those files are in special format.

Do you have some good way to handle it?

I will try to use binary format. However, is it searchable?

David

"Mark Fitzpatrick" wrote:
>Why did you use ntext to store a file instead of Image? That's the
purpose
of Image, basically a binary large object. That wouldn't be a ntext
datatype
because that's a double-byte character field and not a binary large
object.
A text file should still be a blob, unless of course it's just plain text
with no other file information associated with it.

You are ensuring that the database is actually performing the query
correctly? If you're working with SQL Server locally, fire up the SQL
Profiler to see what is actually going on. You may find that there is a
problem with actually executing the query, or at the very least you can
see
the exact call to the db and then run that yourself in the management
studio
to determine if there really are results coming back.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"david" <da***@discussions.microsoft.comwrote in message
news:1D**********************************@microso ft.com...
>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();
--------------------

dr.read() return FALSE! I do not why?



Feb 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tony | last post by:
I changed the datatype of a field in a SQL Server 2000 database from nvarchar to ntext, but now nothing is being displayed. The "Body" column of the datatable used to be nvarchar(8000) and the...
1
by: John Hall | last post by:
We need to read a SQL database containing a mix of English words and Chinese Characters. We think we need to use the N'xxxx' to read the Unicode. We have one place where the SELECT statement...
5
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
2
by: Sileesh | last post by:
HI I know this is not the right forum to post this question, but i think some one might have a suggestion. I have a Table "Test" with columns Id bigint (PK), Number Varchar(50), Notes ntext....
4
by: Igor | last post by:
I have one SELECT statement that needs to return one ntext field from one table and count something from other table, problem is that all fileds that are not in count have to be in group by and...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
3
bwesenberg
by: bwesenberg | last post by:
Hello, I am having and issue with an ntext field. It seems to have a limitation on it. Here is the history. I am supporting a database that was created by a prior employee and I am now...
1
by: leedatkinson | last post by:
Hi, I'm pretty new to using SQL as a long standing Access users all these field types are a bit much... I have a ntext field that I need to convert to a nvarchar. I have tried cast and convert and as...
3
by: nilaangel78 | last post by:
Hi All, I have SQL 2005 table with columns TypeId(int) & Description(ntext datatype). The Description column has XML file content as value in the table. Now i want to read the data into a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.