472,958 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 2885
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.