Reading a XML file from SQL Server in ASP.NET - c#

An XML file was saved into a SQL Server database with the following code:
USE XML
GO
INSERT ENTERTAINMENT (content)
SELECT * FROM
OPENROWSET(bulk 'c:\entertainment-jist.xml', single_blob ) AS X
The file was saved in a column with an xml datatype.
How can the whole content of this file be read in an asp.net page using C#?

You can use SqlDataReader.GetSqlXml to read the XML field.

Related

Extract images from SQL Server table

I have a table that has 10M records and growing
in this table there is one fields called documentbody it is of type varchar(max)
in this field we store images data
this is way overload on the database
I would like to extract these images into a .jpg file and save them some where on the server instead of the database
the new jpg file will be named after each record index
e.g. 506613.jpg , 3177092.jpg , etc.
can I do that using SQL script? do I need .Net programming with that?

Output Linq Query Result to XML

In the SQL Server Management studio, I can do this:
SELECT * FROM tableName FOR XML RAW
to produce a list of XML entries for the data in my table.
How do I do it in C# using LINQ queries?
var itemCollection = from entry in dataContextTableName select entry for xml raw
doesn't work (it doesn't compile). Any ideas?
You could write a stored procedure, return the XML as an OUTPUT parameter and access that through the LinqToSql DataContext.
For example:
CREATE PROCEDURE dbo.GenerateXML
(
#xmlOutput nvarchar(MAX) OUTPUT
)
AS
BEGIN
SET #xmlOutput = (
SELECT * FROM tableName FOR XML RAW
)
END
From here you'd have to drag and drop the stored procedure onto your DBML file using Server Explorer, then in code it's simply:
string xml = "";
dataContext.GenerateXML(ref xml)
The source on this can be found here
As James said, you can't do it using raw LinqToSql syntax; alternatively you could serialize the result you get from a standard Linq query (eg: from entry in dataContextTableName select entry) into XML in code, see here for a good starting point.

How to fix "String or binary data would be truncated" when convert .docx file to binary and insert into varbinary(Max)?

I Use FileStream to Insert file into column with Varbinaray(Max).
This is my sample code :
byte[] dataBytes = File.ReadAllBytes(fileNameAndPath);
string insertCommand = string.Format(#" INSERT INTO [dbo].[ts000Attachments] ([Data]) values (#Data)");
command.Parameters.Add("#Data",SqlDbType.VarBinary,-1).Value = dataBytes;
command.ExecuteNonQuery();
above code work with .doc file.
How to fix this Problem.
According to below links, this issue is all about column size in sql:
StackOverflow:
SQL Server String or binary data would be truncated
SQL Server Error : String or binary data would be truncated
Microsoft:
String or binary data would be truncated" and field specifications
CodeProject:
String or binary data would be truncated. The statement has been terminated.
also make sure that FileStream feature is activated on your table.
for me I don't know why my table did not create with FileStream Feature. so, I dropped the table and re-create it.

How do I display XML data in SQL in HTML

I am working on redesigning an old application that stored content in sql as xml data. I know that if I have a file I can use this to display the data in HTML:
xmlhttp.open("GET","xmlfile.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
Now, lets say I have a table in SQL. In a column I have the contents of "xmlfile.xml". How can I go about displaying that data?
Are you looking for specific field in the xml, SQL Server can query fields of a xml tag inside a sql server column row.
http://msdn.microsoft.com/en-us/library/ms345122(v=sql.90).aspx

How to store images in DB from DataGridView - C#?

I have DataGridView control in my form and the last column type is image.
How I can let the user select image in DataGridView and save it in the DB?
Thanks in advance.
Don't store the image itself in the DB bt in the filesystem and in tue DB store the images name only. This is a better option than stuffing data into DB blobs.
You can store arbitrary files in a SQL Server database by using a field with type BLOB ("Binary Large OBject"). Create a column of this type in your table, then you can load file data into it by referencing a file on the hard drive, probably using a stored procedure
There are quite a few articles on using BLOBS; here is a good-looking tutorial: Part one, part two.
Here is some sample code from that article that is specifically adding a BLOB value from a file:
INSERT INTO BLOBTest
(BLOBName, BLOBData)
SELECT 'First test file',
BulkColumn FROM OPENROWSET(
Bulk 'C:\temp\nextup.jpg', SINGLE_BLOB) AS BLOB
Whether or not you should do this is an entirely different question.

Categories

Resources