473,735 Members | 10,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Enc oding encoding = System.Text.Enc oding.UTF8;
File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength);
this.sqlCommand 1.Parameters["@FileName"].Value=sFileNam e;
this.sqlCommand 1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand 1.Parameters["@FileData"].Value=encoding .GetString(bytC ontent);
this.sqlCommand 1.Parameters["@ContentTy pe"].Value=sContent Type;

--------------------

(3) The problem is how to read the file from the table. The following is my
read process:

SqlDataReader dr;
cmdGetFile.Para meters["@ID"].Value=Request["ID"].ToString();
Response.Write( Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.Exe cuteReader();
if(dr.Read())
{
Response.Conten tType = dr["ContentTyp e"].ToString();
Response.Output Stream.Write((b yte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHea der("Content-Disposition", "Attachment;fil ename=" +
dr["FileName"].ToString());
}
else
{
Response.Write( "File Not Found."+ dr.Read().ToStr ing());
}

dr.Close();
dbConn.Close();
--------------------

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

Feb 26 '07 #1
3 2959
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***@discussi ons.microsoft.c omwrote in message
news:1D******** *************** ***********@mic rosoft.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.Enc oding encoding = System.Text.Enc oding.UTF8;
File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength);
this.sqlCommand 1.Parameters["@FileName"].Value=sFileNam e;
this.sqlCommand 1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand 1.Parameters["@FileData"].Value=encoding .GetString(bytC ontent);
this.sqlCommand 1.Parameters["@ContentTy pe"].Value=sContent Type;

--------------------

(3) The problem is how to read the file from the table. The following is
my
read process:

SqlDataReader dr;
cmdGetFile.Para meters["@ID"].Value=Request["ID"].ToString();
Response.Write( Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.Exe cuteReader();
if(dr.Read())
{
Response.Conten tType = dr["ContentTyp e"].ToString();
Response.Output Stream.Write((b yte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHea der("Content-Disposition", "Attachment;fil ename=" +
dr["FileName"].ToString());
}
else
{
Response.Write( "File Not Found."+ dr.Read().ToStr ing());
}

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***@discussi ons.microsoft.c omwrote in message
news:1D******** *************** ***********@mic rosoft.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.Enc oding encoding = System.Text.Enc oding.UTF8;
File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength);
this.sqlCommand 1.Parameters["@FileName"].Value=sFileNam e;
this.sqlCommand 1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand 1.Parameters["@FileData"].Value=encoding .GetString(bytC ontent);
this.sqlCommand 1.Parameters["@ContentTy pe"].Value=sContent Type;

--------------------

(3) The problem is how to read the file from the table. The following is
my
read process:

SqlDataReader dr;
cmdGetFile.Para meters["@ID"].Value=Request["ID"].ToString();
Response.Write( Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.Exe cuteReader();
if(dr.Read())
{
Response.Conten tType = dr["ContentTyp e"].ToString();
Response.Output Stream.Write((b yte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHea der("Content-Disposition", "Attachment;fil ename=" +
dr["FileName"].ToString());
}
else
{
Response.Write( "File Not Found."+ dr.Read().ToStr ing());
}

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***@discussi ons.microsoft.c omwrote in message
news:5B******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c omwrote in message
news:1D******* *************** ************@mi crosoft.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.Enc oding encoding = System.Text.Enc oding.UTF8;
File1.PostedFil e.InputStream.R ead(bytContent, 0, iLength);
this.sqlCommand 1.Parameters["@FileName"].Value=sFileNam e;
this.sqlCommand 1.Parameters["@FileSize"].Value=iLength;
this.sqlCommand 1.Parameters["@FileData"].Value=encoding .GetString(bytC ontent);
this.sqlCommand 1.Parameters["@ContentTy pe"].Value=sContent Type;

--------------------

(3) The problem is how to read the file from the table. The following
is
my
read process:

SqlDataReader dr;
cmdGetFile.Para meters["@ID"].Value=Request["ID"].ToString();
Response.Write( Request["ID"].ToString()); //test message
dbConn.Open();
dr =cmdGetFile.Exe cuteReader();
if(dr.Read())
{
Response.Conten tType = dr["ContentTyp e"].ToString();
Response.Output Stream.Write((b yte[])dr["FileData"], 0,
(int)dr["FileSize"]);
Response.AddHea der("Content-Disposition", "Attachment;fil ename=" +
dr["FileName"].ToString());
}
else
{
Response.Write( "File Not Found."+ dr.Read().ToStr ing());
}

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
4485
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 below code works fine. However once I changed the datatype of "Body" from nvarchar to ntext, it no longer is displayed. Changing the field back to nvarchar and it is fine again. Is there a special way for me to retrieve ntext fields? Any help...
1
11341
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 works fine. Here is that statement: dim objCmdCategories as new SQLDataAdapter ("select distinct N'product_category' from " & _ "tblproducts where product_status=1 order by product_category", objConn) However, we can't seem to get another SELECT...
5
18535
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 tried the following and playing around with the following: declare @valuePointer varbinary(16)
2
8775
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. in sql server 2000 I ahve a stored procedure in which i am trying to insert the same data as new record with column "Number" changed.
4
8776
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 ntext can't be in group by... i hope you understend what i want to say here :), so is there any solution to this problem or what is the best workaraund you would use? example: TABLE projects project_id int
14
5770
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 entities, and French/German special characters are lost and/or replaced by a question mark. This is really frustrating. I have tried using StreamReader with ALL the encodings available and none work correctly. Each encoding handles some characters...
3
2576
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 having some issues. I have a MS Access front end connected to SQL back end. Connected through ODBC. The filed is called the data type on the SQL table is ntext. The field is located on . The main issues is: the comments will not print in...
1
6808
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 I understand a text field is just too big to go into a varchar (Elephant into a mini was the analagy used) The back ground is; I have been provided a database that has a text field for address information. This field has multiple lines of address...
3
1813
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 dataset in VB.Net. How to do it in VB.Net. I am using Winforms and VB.Net in VS 2005. Below i have given the xml file content. <?xml version="1.0" encoding="Windows-1252" ?> - <SummaryPageComponent...
0
8785
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4559
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4822
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.