<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-1626721032063245108</atom:id><lastBuildDate>Mon, 12 May 2025 12:49:35 +0000</lastBuildDate><category>TSQL</category><category>sql server</category><category>sql</category><category>Asp.Net</category><category>SQL CLR Project</category><category>sql server 2005</category><category>sql server 2008</category><category>DataSet</category><category>OLE automation stored procedures</category><category>WCF service</category><category>web service</category><category>xp_cmdshell</category><category>Ajax Enabled WCF Service</category><category>Android</category><category>ArrayList</category><category>Asp.net service</category><category>CLR Integration</category><category>Call service in javascript</category><category>Calling a WCF Service</category><category>Catalog Views</category><category>DataReader</category><category>FillRow Method</category><category>JQuery</category><category>OBJECT_DEFINITION</category><category>OLE Automation</category><category>Ole Automation Procedures Server Configuration</category><category>Retrun a datatable from sql clr procedure</category><category>SharePoint Server 2010</category><category>SqlContext</category><category>SqlContext.Pipe</category><category>System Views</category><category>Table Value Function</category><category>Views</category><category>WCF Data Service</category><category>What is Sharepoint server</category><category>Write a file</category><category>Write a file using Sql CLR Project</category><category>Write a text file</category><category>adding numbers from string</category><category>data service</category><category>datatable</category><category>encryption</category><category>get system view</category><category>index view</category><category>partion view</category><category>procedure definition</category><category>read a file</category><category>schemabinding</category><category>send dataset</category><category>send datatable</category><category>sp_helptext</category><category>sql view</category><category>string function</category><category>sys.columns</category><category>sys.databases</category><category>sys.object</category><category>sys.procedures</category><category>sys.system_sql_modules</category><category>sys.tables</category><category>sys.triggers</category><category>sys.views</category><category>system proceduers</category><category>view</category><category>view the definition of stored procedure</category><category>view the definition of system views</category><category>windows communication foundation</category><category>write file</category><title>Waqar Farooq Janjua</title><description></description><link>http://wfjanjua.blogspot.com/</link><managingEditor>noreply@blogger.com (Waqar Farooq Janjua)</managingEditor><generator>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-2639105874714071398</guid><pubDate>Tue, 05 Mar 2013 06:27:00 +0000</pubDate><atom:updated>2013-03-08T20:24:16.091+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">encryption</category><category domain="http://www.blogger.com/atom/ns#">index view</category><category domain="http://www.blogger.com/atom/ns#">partion view</category><category domain="http://www.blogger.com/atom/ns#">schemabinding</category><category domain="http://www.blogger.com/atom/ns#">sql view</category><category domain="http://www.blogger.com/atom/ns#">view</category><title>SQL Views</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;VIEW:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;View can be defined as a virtual table or a stored query. The data accessible through a view is not stored in the database as a distint object.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Views are useful due to two main reasons:&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Security&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Convenience&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;From security point of view, it restrict users to specific rows or columns. Also user is not aware of data source of the View. For example, we can get data from multiple tables using JOIN in a view but the user is not aware about tables from which data is coming.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Another most important benefit of View is that we don&#39;t have to write large querys again and again to fetch data from multiple tables, we only write query once and then stored it in a View.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;View Syntax:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH &amp;lt;view_attribute&amp;gt; [ ,...n ] ]
AS select_statement
[ WITH CHECK OPTION ] [ ; ]
&amp;lt;view_attribute&amp;gt; ::=
{
&amp;nbsp; &amp;nbsp; [ ENCRYPTION ]
&amp;nbsp; &amp;nbsp; [ SCHEMABINDING ]
&amp;nbsp; &amp;nbsp; [ VIEW_METADATA ] &amp;nbsp; &amp;nbsp; }
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;INSERT,UPDATE and DELETE Operations on View:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can apply these operatoins on Views, they have affect on the data present inside the table.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Example:
&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;-- We are using AdventureWorks2012 database and will create a View using Person.Address table.

CREATE VIEW vw_PersonAddress
AS
SELECT Person.Address.AddressLine1, Person.Address.City, Person.Address.PostalCode,Person.Address.StateProvinceID FROM Person.Address
-- We created a view and getting Address,City,PostalCode and StateProvinceID 
-- Now we will add a new row using vw_PersonAddress
--INSERTION
INSERT INTO vw_PersonAddress(AddressLine1,City,StateProvinceID,PostalCode) VALUES(&#39;Inside SQL&#39;,&#39;Table&#39;,14,&#39;333&#39;)
SELECT * FROM vw_PersonAddress WHERE City = &#39;Table&#39;
-- Output:
-- AddressLine1 City PostalCode StateProvinceID
-- Inside SQL Table 333 14

--UPDATION

UPDATE vw_PersonAddress
SET AddressLine1 = &#39;Inside SQL updated&#39;,
 PostalCode = &#39;999&#39;
WHERE City = &#39;Table&#39;

--Output:
SELECT * FROM vw_PersonAddress
WHERE City = &#39;Table&#39;

--AddressLine1 City PostalCode StateProvinceID
--Inside SQL updated Table 999 14

-- DELETION:
DELETE FROM vw_PersonAddress WHERE City = &#39;Table&#39;

-- It will delete the row
&lt;/pre&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;Conditions:
&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;TOP is not used anywhere in the select_statement of the view together with the WITH CHECK OPTION clause.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;With Check Option:

&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;It has three options.&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;ENCRYPTION&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;SCHEMABINDING&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;VIEW_METADATA&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;Encryption:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;It encrypts the entries in sys.syscomments that contain the text of the CREATE VIEW statement. Using WITH ENCRYPTION prevents the view from being published as part of SQL Server replication.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can better understand With Encryption check with below example.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Example:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;-- We will alter the above view WITH ENCRYPTION option
-- Before altering the view first look at the definition of view using Object_definition
SELECT OBJECT_DEFINITION(OBJECT_ID(&#39;vw_PersonAddress&#39;))
--OUTPUT:
--CREATE VIEW [dbo].[vw_PersonAddress]
--AS
--SELECT Person.Address.AddressLine1, Person.Address.City, Person.Address.PostalC--ode,Person.Address.StateProvinceID FROM Person.Address
ALTER VIEW [dbo].[vw_PersonAddress]
WITH ENCRYPTION
AS
SELECT Person.Address.AddressLine1, Person.Address.City, Person.Address.PostalCode,Person.Address.StateProvinceID FROM Person.Address
--After Alteration:
SELECT OBJECT_DEFINITION(OBJECT_ID(&#39;vw_PersonAddress&#39;))
--OUTPUT:
--NULL
-- Because we altered it using with Encryption check, we can&#39;t view it&#39;s definition
&lt;/pre&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;SCHEMABINDING:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;It binds the View to the schema of the underlying table or tables, means if we create a view using this option the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must be first modified or dropped to remove dependencies on the table that is to be modified. Also those tables that are referenced in a view containing Schemabinding option can be removed until view is removed or schemabinding option is removed from that view.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Example:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;--First I will modify the view to remove ENCRYPTION option

ALTER VIEW [dbo].[vw_PersonAddress]
AS
SELECT Person.Address.AddressLine1, Person.Address.City, Person.Address.PostalCode,Person.Address.StateProvinceID FROM Person.Address

--Now I will alter the view with SCHEMABINDING option

ALTER VIEW [dbo].[vw_PersonAddress]
WITH SCHEMABINDING
AS
SELECT Person.Address.AddressLine1, Person.Address.City, Person.Address.PostalCode,Person.Address.StateProvinceID FROM Person.Address

-- Now I will try to delete Person.Address table

DELETE FROM Person.ADDRESS

--Output

--Msg 547, Level 16, State 0, Line 1
--The DELETE statement conflicted with the REFERENCE constraint &quot;FK_BusinessEntityAddress_Address_AddressID&quot;. The conflict occurred in database &quot;AdventureWorks2012&quot;, table &quot;Person.BusinessEntityAddress&quot;, column &#39;AddressID&#39;.
--The statement has been terminated.

-- The AddressID is referenced in multiple tables, so I will create a new table and view to demonstrate it

CREATE TABLE [dbo].[testView]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](10) NULL
)
INSERT INTO dbo.testView(NAME) values (&#39;apple&#39;)
INSERT INTO dbo.testView(NAME) VALUES (&#39;banana&#39;)
INSERT INTO dbo.testView(NAME) VALUES (&#39;orange&#39;)

CREATE VIEW vw_testView
WITH SCHEMABINDING
AS
SELECT ID,Name FROM dbo.testView
-- Now I will try to drop testView table
DROP TABLE testView
--Output
--Msg 3729, Level 16, State 1, Line 1
--Cannot DROP TABLE &#39;testView&#39; because it is being referenced by object &#39;vw_testView&#39;.
&lt;/pre&gt;
&lt;pre class=&quot;brush.sql&quot;&gt;&lt;/pre&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;Partitioned Views:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In partioned view we can fetch data from multiple tables in either the same instance of sql server or in a group of autonomous instances of sql server using UNION ALL.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Genral Syntax:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;-- Server1
create database Partitioning;

USE Partitioning
Go

Create table Students
(
 ID int Identity,
 Name varchar(30)
);

INSERT INTO Students(Name) VALUES(&#39;A&#39;);
INSERT INTO Students(Name) VALUES(&#39;B&#39;);
INSERT INTO Students(Name) VALUES(&#39;C&#39;);

-- Server2
create database Partitioning;

USE Partitioning
Go

Create table Students
(
 ID int Identity,
 Name varchar(30)
);

INSERT INTO Students(Name) VALUES(&#39;E&#39;);
INSERT INTO Students(Name) VALUES(&#39;F&#39;);
INSERT INTO Students(Name) VALUES(&#39;J&#39;);

-- Now I will create a partition view to fetch data from these two tables
CREATE view vw_Part_Students
AS
Select * from [Server1].Partitioning.dbo.Students
UNION  ALL
Select * from [Server2].Partitioning.dbo.Students

select * from vw_Part_Students
order by Name

--Output:
--ID     Name
--1       A
--2       B
--3       C
--1       E
--2       F
--3       J
&lt;/pre&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;Conditions for Partioned View:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;All columns in the member tables should be selected in the column list of the view definition.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The columns in the same ordinal position of each select list should be of the same type, including collations. It is not sufficient for the columns to be implicitly convertible types, as is generally the case for UNION.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b&gt;Indexed Views:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can created indexes on views. The first index created on a view must be a unique clustered index. After unique clustered index we can create additional nonclustered indexes.&lt;/span&gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;CREATE UNIQUE CLUSTERED INDEX myidx ON vw_testView(ID)
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Thanks for reading if you have any query please ask.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Source:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms187956.aspx&quot; target=&quot;_blank&quot;&gt;MSDN&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://wfjanjua.blogspot.com/2013/03/sql-views.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-429472402069847955</guid><pubDate>Thu, 23 Aug 2012 08:26:00 +0000</pubDate><atom:updated>2012-08-23T13:31:10.052+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">get system view</category><category domain="http://www.blogger.com/atom/ns#">OBJECT_DEFINITION</category><category domain="http://www.blogger.com/atom/ns#">procedure definition</category><category domain="http://www.blogger.com/atom/ns#">sp_helptext</category><category domain="http://www.blogger.com/atom/ns#">sys.system_sql_modules</category><category domain="http://www.blogger.com/atom/ns#">system proceduers</category><category domain="http://www.blogger.com/atom/ns#">view the definition of stored procedure</category><category domain="http://www.blogger.com/atom/ns#">view the definition of system views</category><title>Get definition of system or user created objects( Stored Procedures, functions, views ) in SQL Server</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In SQL Server 2005 or 2008 we can easily get the definition of user created functions, stored procedures as well as system views, procedures etc.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;There are three ways to get the&amp;nbsp;definition of system views,procedures as well as user created objects.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Using&amp;nbsp; OBJECT_DEFINITION function&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Using&amp;nbsp;sys.system_sql_modules view ( Only returns system views,procedures etc)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Using&amp;nbsp;sp_helptext procedure&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We will explore each one by one.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;1) Using OBJECT_DEFINITION&amp;nbsp;:&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Using&amp;nbsp;OBJECT_DEFINITION function we can get the source text of definition of the specified object.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Syntax:&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;OBJECT_DEFINITION function takes one parameter object_id. It is the ID of the object and it&#39;s datatype is int and assumed to represent an object in the current database context. It&#39;s return type is nvarchar(max).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Example of System Stored Procedure:&lt;/b&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot; style=&quot;text-align: left;&quot;&gt;select OBJECT_DEFINITION(object_id(&#39;sp_databases&#39;)) 

Output:
  create procedure sys.sp_databases  as      set nocount on        select          DATABASE_NAME   = db_name(s_mf.database_id),          DATABASE_SIZE   = convert(int,                                      case -- more than 2TB(maxint) worth of pages (by 8K each) can not fit an int...                                      when convert(bigint, sum(s_mf.size)) &amp;gt;= 268435456                                      then null                                      else sum(s_mf.size)*8 -- Convert from 8192 byte pages to Kb                                      end),          REMARKS         = convert(varchar(254),null)      from          sys.master_files s_mf      where          s_mf.state = 0 and -- ONLINE          has_dbaccess(db_name(s_mf.database_id)) = 1 -- Only look at databases to which we have access      group by s_mf.database_id      order by 1  
&lt;/pre&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Example of&amp;nbsp;&lt;/b&gt;&lt;b&gt;System Views:&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can get the definition of sys.tables system view as&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot; style=&quot;text-align: left;&quot;&gt;select OBJECT_DEFINITION(object_id(&#39;sys.tables&#39;))

Output:
CREATE VIEW sys.tables AS &amp;nbsp; SELECT o.name, o.object_id, o.principal_id, o.schema_id, o.parent_object_id, &amp;nbsp; &amp;nbsp;o.type, o.type_desc, o.create_date, o.modify_date, &amp;nbsp; &amp;nbsp;o.is_ms_shipped, o.is_published, o.is_schema_published, &amp;nbsp; &amp;nbsp;lob.lobds AS lob_data_space_id, &amp;nbsp; &amp;nbsp;rfs.indepid AS filestream_data_space_id, &amp;nbsp; &amp;nbsp;o.property AS max_column_id_used, &amp;nbsp; &amp;nbsp;o.lock_on_bulk_load, o.uses_ansi_nulls, o.is_replicated, o.has_replication_filter, &amp;nbsp; &amp;nbsp;o.is_merge_published, o.is_sync_tran_subscribed, o.has_unchecked_assembly_data, &amp;nbsp; &amp;nbsp;lob.intprop AS text_in_row_limit, &amp;nbsp; &amp;nbsp;o.large_value_types_out_of_row, &amp;nbsp; &amp;nbsp;o.is_tracked_by_cdc, &amp;nbsp; &amp;nbsp;o.lock_escalation_option AS lock_escalation, &amp;nbsp; &amp;nbsp;ts.name AS lock_escalation_desc &amp;nbsp; FROM sys.objects$ o &amp;nbsp; LEFT JOIN sys.sysidxstats lob ON lob.id = o.object_id AND lob.indid &amp;lt;= 1 &amp;nbsp; LEFT JOIN sys.syssingleobjrefs rfs ON rfs.depid = o.object_id AND rfs.class = 42 AND rfs.depsubid = 0 -- SRC_OBJTOFSDS &amp;nbsp; LEFT JOIN sys.syspalvalues ts ON ts.class = &#39;LEOP&#39; AND ts.value = o.lock_escalation_option &amp;nbsp; WHERE o.type = &#39;U&#39; 
&lt;/pre&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In similar manner we can get the definition of sys.columns, sys.views, sys.databases etc.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now I will show you some examples using Adventure Works database.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Example of Stored Procedure:&lt;/b&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot; style=&quot;text-align: left;&quot;&gt;select OBJECT_DEFINITION(object_id(&#39;uspGetEmployeeManagers&#39;))

Output:
CREATE PROCEDURE [dbo].[uspGetEmployeeManagers] &amp;nbsp;@EmployeeID [int] &amp;nbsp;AS &amp;nbsp;BEGIN &amp;nbsp;SET NOCOUNT ON; &amp;nbsp;WITH [EMP_cte]([EmployeeID], [ManagerID], [FirstName], [LastName], [Title], [RecursionLevel]) -- CTE name and columns &amp;nbsp;AS ( &amp;nbsp;SELECT e.[EmployeeID], e.[ManagerID], c.[FirstName], c.[LastName], e.[Title], 0 -- Get the initial Employee &amp;nbsp;FROM [HumanResources].[Employee] e &amp;nbsp; INNER JOIN [Person].[Contact] c &amp;nbsp; ON e.[ContactID] = c.[ContactID] &amp;nbsp;WHERE e.[EmployeeID] = @EmployeeID &amp;nbsp;UNION ALL &amp;nbsp;SELECT e.[EmployeeID], e.[ManagerID], c.[FirstName], c.[LastName], e.[Title], [RecursionLevel] + 1 -- Join recursive member to anchor &amp;nbsp;FROM [HumanResources].[Employee] e &amp;nbsp; INNER JOIN [EMP_cte] &amp;nbsp;ON e.[EmployeeID] = [EMP_cte].[ManagerID] &amp;nbsp;INNER JOIN [Person].[Contact] c &amp;nbsp; ON e.[ContactID] = c.[ContactID] &amp;nbsp;) &amp;nbsp;SELECT [EMP_cte].[RecursionLevel], [EMP_cte].[EmployeeID], [EMP_cte].[FirstName], [EMP_cte].[LastName], &amp;nbsp; [EMP_cte].[ManagerID], c.[FirstName] AS &#39;ManagerFirstName&#39;, c.[LastName] AS &#39;ManagerLastName&#39; &amp;nbsp;-- Outer select from the CTE &amp;nbsp;FROM [EMP_cte] &amp;nbsp; INNER JOIN [HumanResources].[Employee] e &amp;nbsp; ON [EMP_cte].[ManagerID] = e.[EmployeeID] &amp;nbsp;INNER JOIN [Person].[Contact] c &amp;nbsp; ON e.[ContactID] = c.[ContactID] &amp;nbsp;ORDER BY [RecursionLevel], [ManagerID], [EmployeeID] &amp;nbsp;OPTION (MAXRECURSION 25) &amp;nbsp; END; 
&lt;/pre&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Example of View:&lt;/b&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot; style=&quot;text-align: left;&quot;&gt;select OBJECT_DEFINITION(object_id(&#39;HumanResources.vEmployeeDepartment&#39;))

Output:
CREATE VIEW [HumanResources].[vEmployeeDepartment] &amp;nbsp; AS &amp;nbsp; SELECT &amp;nbsp; e.[EmployeeID] &amp;nbsp; ,c.[Title] &amp;nbsp; ,c.[FirstName] &amp;nbsp; ,c.[MiddleName] &amp;nbsp; ,c.[LastName] &amp;nbsp; ,c.[Suffix] &amp;nbsp; ,e.[Title] AS [JobTitle] &amp;nbsp; ,d.[Name] AS [Department] &amp;nbsp; ,d.[GroupName] &amp;nbsp; ,edh.[StartDate] &amp;nbsp; FROM [HumanResources].[Employee] e &amp;nbsp;INNER JOIN [Person].[Contact] c &amp;nbsp; ON c.[ContactID] = e.[ContactID] &amp;nbsp;INNER JOIN [HumanResources].[EmployeeDepartmentHistory] edh &amp;nbsp; ON e.[EmployeeID] = edh.[EmployeeID] &amp;nbsp; INNER JOIN [HumanResources].[Department] d &amp;nbsp; ON edh.[DepartmentID] = d.[DepartmentID] &amp;nbsp; WHERE GETDATE() BETWEEN edh.[StartDate] AND ISNULL(edh.[EndDate], GETDATE()); 
&lt;/pre&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Example of Table-valued function:&lt;/b&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot; style=&quot;text-align: left;&quot;&gt;select OBJECT_DEFINITION(object_id(&#39;ufnGetContactInformation&#39;))

Output:
CREATE FUNCTION [dbo].[ufnGetContactInformation](@ContactID int) &amp;nbsp;RETURNS @retContactInformation TABLE &amp;nbsp; ( &amp;nbsp;[ContactID] int PRIMARY KEY NOT NULL, &amp;nbsp; [FirstName] [nvarchar](50) NULL, &amp;nbsp; [LastName] [nvarchar](50) NULL, &amp;nbsp; [JobTitle] [nvarchar](50) NULL, &amp;nbsp; [ContactType] [nvarchar](50) NULL &amp;nbsp;) &amp;nbsp;AS &amp;nbsp; BEGIN &amp;nbsp;DECLARE &amp;nbsp; @FirstName [nvarchar](50), &amp;nbsp; @LastName [nvarchar](50), &amp;nbsp; @JobTitle [nvarchar](50), &amp;nbsp; @ContactType [nvarchar](50); &amp;nbsp;SELECT &amp;nbsp; @ContactID = ContactID, &amp;nbsp; @FirstName = FirstName, &amp;nbsp; @LastName = LastName &amp;nbsp;FROM [Person].[Contact] &amp;nbsp; WHERE [ContactID] = @ContactID; &amp;nbsp;SET @JobTitle = &amp;nbsp; CASE &amp;nbsp; WHEN EXISTS(SELECT * FROM [HumanResources].[Employee] e &amp;nbsp; WHERE e.[ContactID] = @ContactID) &amp;nbsp; THEN (SELECT [Title] &amp;nbsp; FROM [HumanResources].[Employee] &amp;nbsp; WHERE [ContactID] = @ContactID) &amp;nbsp;WHEN EXISTS(SELECT * FROM [Purchasing].[VendorContact] vc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON vc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE vc.[ContactID] = @ContactID) &amp;nbsp; THEN (SELECT ct.[Name] &amp;nbsp; FROM [Purchasing].[VendorContact] vc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON vc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE vc.[ContactID] = @ContactID) &amp;nbsp;WHEN EXISTS(SELECT * FROM [Sales].[StoreContact] sc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON sc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE sc.[ContactID] = @ContactID) &amp;nbsp; THEN (SELECT ct.[Name] &amp;nbsp; FROM [Sales].[StoreContact] sc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON sc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE [ContactID] = @ContactID) &amp;nbsp;ELSE NULL &amp;nbsp; END; &amp;nbsp;SET @ContactType = &amp;nbsp; CASE &amp;nbsp; WHEN EXISTS(SELECT * FROM [HumanResources].[Employee] e &amp;nbsp; WHERE e.[ContactID] = @ContactID) &amp;nbsp; THEN &#39;Employee&#39; &amp;nbsp;WHEN EXISTS(SELECT * FROM [Purchasing].[VendorContact] vc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON vc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE vc.[ContactID] = @ContactID) &amp;nbsp; THEN &#39;Vendor Contact&#39; &amp;nbsp;WHEN EXISTS(SELECT * FROM [Sales].[StoreContact] sc &amp;nbsp; INNER JOIN [Person].[ContactType] ct &amp;nbsp; ON sc.[ContactTypeID] = ct.[ContactTypeID] &amp;nbsp; WHERE sc.[ContactID] = @ContactID) &amp;nbsp; THEN &#39;Store Contact&#39; &amp;nbsp;WHEN EXISTS(SELECT * FROM [Sales].[Individual] i &amp;nbsp; WHERE i.[ContactID] = @ContactID) &amp;nbsp; THEN &#39;Consumer&#39; &amp;nbsp;END; &amp;nbsp;IF @ContactID IS NOT NULL &amp;nbsp; BEGIN &amp;nbsp;INSERT @retContactInformation &amp;nbsp;SELECT @ContactID, @FirstName, @LastName, @JobTitle, @ContactType; &amp;nbsp;END; &amp;nbsp;RETURN; &amp;nbsp;END; 
&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;2) Using sys.system_sql_modules view :&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can get the definition of all the system views, procedures by running the below query&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;select * from sys.system_sql_modules

-- This query will return 1880 rows having 9 Columns. I am showing only one row with two important columns.
Output:

object_id
-1073624922
definition
create procedure sys.sp_MSalreadyhavegeneration      (@genguid uniqueidentifier,        @subscribernick binary(6),       @compatlevel int = 10) -- backward compatibility level, default=Sphinx    as      declare @nicknames varbinary(1000)      declare @offset int      declare @retcode int      declare @dbname sysname            -- Security Checking       -- PAL users have access      exec @retcode = sys.sp_MSrepl_PAL_rolecheck      if (@retcode &amp;lt;&amp;gt; 0) or (@@error &amp;lt;&amp;gt; 0)          return 1                if (@genguid is null)      begin          RAISERROR(14043, 16, -1, &#39;@genguid&#39;, &#39;sp_MSalreadyhavegeneration&#39;)          return (1)      end        select @nicknames = nicknames from dbo.MSmerge_genhistory where guidsrc = @genguid      if @nicknames is null      begin          set @dbname= db_name()          -- what is the appropriate error to return?          RAISERROR(21333, 16, -1, @dbname)          return (1)      end        if @compatlevel &amp;lt; 90          set @subscribernick= substring(@subscribernick,1,4) + 0x0000        declare @replnicklength int      set @replnicklength= col_length(&#39;sysmergesubscriptions&#39;, &#39;replnickname&#39;)      set @offset= 1      while @offset &amp;lt; DATALENGTH(@nicknames)      begin          -- If the subscriber nickname is already in the list just return          if substring(@nicknames, @offset, @replnicklength) = @subscribernick              return (0)          set @offset = @offset + @replnicklength      end            -- Subscriber nickname is not in the array.  Add it so that we won&#39;t send this gen      -- back down in the return message.  (Would waste bandwidth...)        -- Append guard byte          set @offset = DATALENGTH(@nicknames)      if substring(@nicknames, @offset, 1) &amp;lt;&amp;gt; 0xFF          begin                  set @nicknames = @nicknames + 0xFF          end        if @nicknames = 0x0          set @nicknames = @subscribernick + 0xFF      else          set @nicknames = @subscribernick + @nicknames        update dbo.MSmerge_genhistory set nicknames = @nicknames where guidsrc = @genguid            return (0)  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We can also get the definition of only one view or procedure as&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;select definition from sys.system_sql_modules where [object_id] = ( select OBJECT_ID(&#39;sys.columns&#39;) )

Output:
CREATE VIEW sys.columns AS   SELECT id AS object_id,    name, colid AS column_id,    xtype AS system_type_id,    utype AS user_type_id,    length AS max_length,    prec AS precision, scale,    convert(sysname, CollationPropertyFromId(collationid, &#39;name&#39;)) AS collation_name,    sysconv(bit, 1 - (status &amp;amp; 1)) AS is_nullable,  -- CPM_NOTNULL    sysconv(bit, status &amp;amp; 2) AS is_ansi_padded,  -- CPM_NOTRIM    sysconv(bit, status &amp;amp; 8) AS is_rowguidcol,   -- CPM_ROWGUIDCOL    sysconv(bit, status &amp;amp; 4) AS is_identity,   -- CPM_IDENTCOL    sysconv(bit, status &amp;amp; 16) AS is_computed,   -- CPM_COMPUTED    sysconv(bit, status &amp;amp; 32) AS is_filestream,   -- CPM_FILESTREAM    sysconv(bit, status &amp;amp; 0x020000) AS is_replicated,   -- CPM_REPLICAT    sysconv(bit, status &amp;amp; 0x040000) AS is_non_sql_subscribed, -- CPM_NONSQSSUB    sysconv(bit, status &amp;amp; 0x080000) AS is_merge_published, -- CPM_MERGEREPL    sysconv(bit, status &amp;amp; 0x100000) AS is_dts_replicated, -- CPM_REPLDTS    sysconv(bit, status &amp;amp; 2048) AS is_xml_document,  -- CPM_XML_DOC     xmlns AS xml_collection_id,    dflt AS default_object_id,    chk AS rule_object_id,    sysconv(bit, status &amp;amp; 0x1000000) AS is_sparse, -- CPM_SPARSE    sysconv(bit, status &amp;amp; 0x2000000) AS is_column_set -- CPM_SPARSECOLUMNSET   FROM sys.syscolpars   WHERE number = 0 AND has_access(&#39;CO&#39;, id) = 1  

&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;3) Using sp_helptext procedure :&lt;/b&gt;&lt;br /&gt;
It d&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;isplays the definition of a user-defined rule, default, unencrypted Transact-SQL stored procedure, user-defined Transact-SQL function, trigger, computed column, CHECK constraint, view, or system object such as a system stored procedure. I&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;t also returns the definition in multiple rows.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Syntax:&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;It accepts two parameters; first one is object_id and the second one is computed column.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Examples: &lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Displaying the definition of a View: &lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;USE AdventureWorks;
GO
EXEC sp_helptext &#39;HumanResources.vEmployee&#39;;

Output:
CREATE VIEW [HumanResources].[vEmployee]   
AS   
SELECT   
e.[EmployeeID]  
,c.[Title]  
,c.[FirstName]  
,c.[MiddleName]  
,c.[LastName]  
,c.[Suffix]  
,e.[Title] AS [JobTitle]   
,c.[Phone]  
,c.[EmailAddress]  
,c.[EmailPromotion]  
,a.[AddressLine1]  
,a.[AddressLine2]  
,a.[City]  
,sp.[Name] AS [StateProvinceName]   
,a.[PostalCode]  
,cr.[Name] AS [CountryRegionName]   
,c.[AdditionalContactInfo]  
FROM [HumanResources].[Employee] e  
INNER JOIN [Person].[Contact] c   
ON c.[ContactID] = e.[ContactID]  
INNER JOIN [HumanResources].[EmployeeAddress] ea   
ON e.[EmployeeID] = ea.[EmployeeID]   
INNER JOIN [Person].[Address] a   
ON ea.[AddressID] = a.[AddressID]  
INNER JOIN [Person].[StateProvince] sp   
ON sp.[StateProvinceID] = a.[StateProvinceID]  
INNER JOIN [Person].[CountryRegion] cr   
ON cr.[CountryRegionCode] = sp.[CountryRegionCode];  

&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Displaying the definition of a computed column :&lt;/b&gt;&lt;br /&gt;
The following example displays the definition of the computed column TotalDue on the SalesOrderHeader table in the AdventureWorks database.&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;USE AdventureWorks;
GO
sp_helptext @objname = N&#39;AdventureWorks.Sales.SalesOrderHeader&#39;, @columnname = TotalDue ;
GO

Output:
(isnull(([SubTotal]+[TaxAmt])+[Freight],(0)))
&lt;/pre&gt;Similarly, we can get the definition of system databases.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading, If you have any query feel free to ask.&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/08/get-definition-of-system-or-user.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-4469625925749366449</guid><pubDate>Thu, 12 Jul 2012 20:11:00 +0000</pubDate><atom:updated>2012-08-24T11:28:06.167+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">adding numbers from string</category><category domain="http://www.blogger.com/atom/ns#">sql server</category><category domain="http://www.blogger.com/atom/ns#">string function</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><title>Add Numbers from string(varchar) in tsql</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Once I was asked by my colleague to write a function which will add the numbers from a string, so I write a function for it. I used the TSQL substring, &amp;nbsp;Len, ASCII and char functions to&amp;nbsp;achieve&amp;nbsp;this task.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code Snippet:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;CREATE FUNCTION SelectNumbersFromString(@str varchar(50)) RETURNS varchar(MAX) AS BEGIN DECLARE @cchk char(5); DECLARE @len int ; DECLARE @aschr int;
SET @len =
  (SELECT len(@str)); DECLARE @count int
SET @count = 1 DECLARE @ans varchar(MAX)
SET @ans = &#39;&#39; While @count &amp;lt;= @len BEGIN
SET @cchk =
  (SELECT Substring(@str,@count,1));
SET @aschr =
  (SELECT ASCII(@cchk)); IF @aschr IN (49,
                                       50,
                                       51,
                                       52,
                                       53,
                                       54,
                                       55,
                                       56,
                                       57,
                                       58) BEGIN
SET @ans = @ans + CHAR(@aschr) END
SET @count = @count + 1; END RETURN @ans; END
&lt;/pre&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/07/add-numbers-from-stringvarchar-in-tsql.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-8380643907754224385</guid><pubDate>Thu, 14 Jun 2012 09:36:00 +0000</pubDate><atom:updated>2012-08-24T13:17:55.899+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Asp.Net</category><category domain="http://www.blogger.com/atom/ns#">Asp.net service</category><category domain="http://www.blogger.com/atom/ns#">data service</category><category domain="http://www.blogger.com/atom/ns#">WCF Data Service</category><category domain="http://www.blogger.com/atom/ns#">WCF service</category><category domain="http://www.blogger.com/atom/ns#">web service</category><category domain="http://www.blogger.com/atom/ns#">windows communication foundation</category><title>Creating and Consuming WCF Data Service</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;WCF Data Services provides the ability to create Windows Communication Foundation (WCF) services that use the Atom Publishing protocol (AtomPub) and Open Data protocol (OData) to expose and consume data over the web. OData is an open protocol for sharing data that builds on Atom Publishing protocol&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;(AtomPub).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In this post I will explain you how to use a WCF Data Service. Create a new web application and add a WCF Data Service template from the Add New Item&amp;nbsp;Dialogue&amp;nbsp;box. We named it gamesDataService, this service will give us the detail of games. The gamesDataService contains the following code:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:csharp&quot;&gt;namespace wcfDataService  
 {  
   public class gamesDataService : DataService&amp;lt; /* TODO: put your data source class name here */ &amp;gt;  
   {  
     // This method is called only once to initialize service-wide policies.  
     public static void InitializeService(DataServiceConfiguration config)  
     {  
       // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.  
       // Examples:  
       // config.SetEntitySetAccessRule(&quot;MyEntityset&quot;, EntitySetRights.AllRead);  
       // config.SetServiceOperationAccessRule(&quot;MyServiceOperation&quot;, ServiceOperationRights.All);  
       config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;  
     }  
   }  
 }  &lt;/pre&gt;&lt;/div&gt;&lt;code style=&quot;white-space: normal; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt; &lt;/span&gt;&lt;/code&gt; &lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The code-behind class inherits from the generic DataService class. A comment appears where the parameter type is expected, gamesDataService&amp;nbsp;in this case. The code contains a method InitializeService which accepts one parameter of type DataServiceConfiguration. This method is used to set up service configuration.&amp;nbsp;The DataServiceConfiguration class has a SetEntitySetAccessRule method that accepts two parameters. The first parameter represents the name of the entity set on which you want to set permission. This parameter also accepts the asterisk (*) wildcard to set permissions on all entity sets. The second parameter is an EntitySetRights enumeration value that represents the permissions you want to assign to the entity set.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Following are the&amp;nbsp;EntitySetRights values:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;All -&amp;gt; Provides authorization to create, read, update, and delete data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;AllRead -&amp;gt; Provides authorization to read data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;AllWrite -&amp;gt; Provides authorization to write data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;None -&amp;gt; Denies all rights to access data.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;ReadMultiple -&amp;gt; Provides authorization to perform multiple item queries to read from the entity set.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;ReadSingle -&amp;gt; Provides authorization to perform single item queries to read&amp;nbsp;from the entity set.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;WriteAppend -&amp;gt; Provides authorization to write new items to the entity set.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;WriteDelete -&amp;gt; Provides authorization to delete items from the entity set.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;WriteMerge -&amp;gt; Provides authorization to perform merge-based updates, in which the payload must be an entity and needs only to contains the properties being modified.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;WriteReplace Provides authorization to perform replace-based updates, in which the payload must be an entity and should contains all the properties of the entity.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The config object sets permissions on the operations by providing&amp;nbsp;a ServiceOperationRights enumeration value to the SetServiceOperationAccessRule&amp;nbsp;method. The&amp;nbsp;config.DataServiceBehavior.MaxProtocolVersion is used to gets or sets the maximum protocol version that is supported by the response sent by the data service.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now we will add a class called Games to our project. In the Games class we create four properties which are shown below:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:csharp&quot;&gt;namespace wcfDataService  
 {  
   [DataServiceKey(&quot;gameID&quot;)]  
   public class Games  
   {  
     public long gameID { get; set; }  
     public string name { get; set; }  
     public string size { get; set; }  
     public string price { get; set; }  
   }  
 }  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;After creating this class we create properties in our gamesDataService class to expose data.&amp;nbsp;This property gets data from a hard-coded list of games but could certainly&amp;nbsp;get its data from a back-end SQL Server or other source. The property’s data type must be of generic IQueryable. The following code shows completed gamesDataService class with Game property&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;namespace wcfDataService  
 {  
   public class gamesDataService : DataService&amp;lt;gamesDataService&amp;gt;  
   {  
     public IQueryable&amp;lt;Games&amp;gt; Game  
     {  
       get  
       {  
         return (new List&amp;lt;Games&amp;gt;  
         {  
           new Games{ gameID=001, name=&quot;Street Fighter&quot;, price=&quot;10$&quot;, size=&quot;10MB&quot; },  
           new Games{ gameID=002,name=&quot;Need For Speed 2&quot;, price=&quot;25$&quot;, size=&quot;250MB&quot; },  
           new Games{ gameID=003, name=&quot;Delta Force 1&quot;, price=&quot;30$&quot;, size=&quot;300MB&quot; }  
         }).AsQueryable();  
       }  
     }  
     // This method is called only once to initialize service-wide policies.  
     public static void InitializeService(DataServiceConfiguration config)  
     {        
       config.SetEntitySetAccessRule(&quot;*&quot;, EntitySetRights.AllRead);        
       config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;  
     }  
   }  
 }  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now view the gamesDataService in browser it will show the following output:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzq_q0n_nJfs-wFzNJ3J4HtXBpI62io9K562Qcls8M2Sl_qhcIB2pIh_7UpizTmQyTNvgb2caBBD9F-ytHUJRMlTK_9Q5oTfW6UO6n73JUNZCPS43f8oJrvSlhDTn_kdEpqf32xlbCPrE/s1600/wcfDataService1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzq_q0n_nJfs-wFzNJ3J4HtXBpI62io9K562Qcls8M2Sl_qhcIB2pIh_7UpizTmQyTNvgb2caBBD9F-ytHUJRMlTK_9Q5oTfW6UO6n73JUNZCPS43f8oJrvSlhDTn_kdEpqf32xlbCPrE/s1600/wcfDataService1.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The browser shows an XML representation of the service. In the defined namespaces, the&amp;nbsp;atom namespace is defined. This is the namespace that is used for RSS feeds. The Game collection&amp;nbsp;is defined as a relative URI location, so adding Game to the end of the URI will display&amp;nbsp;an XML feed containing the list of games.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBj0ySw5LsOdT2eX0LvRq2VR9RV3NosXIYbAxGGdxdZIR6Z1f2MCF6cO8b2SGa1O_P3M1luEM8R_wNYCmqDuWayC3Id4lLo9oQcqu135zfIAnXctzEUpPp4pwtJZVgTehBmhh_WOmEU2M/s1600/wcfDataService2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBj0ySw5LsOdT2eX0LvRq2VR9RV3NosXIYbAxGGdxdZIR6Z1f2MCF6cO8b2SGa1O_P3M1luEM8R_wNYCmqDuWayC3Id4lLo9oQcqu135zfIAnXctzEUpPp4pwtJZVgTehBmhh_WOmEU2M/s1600/wcfDataService2.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In order to retrieve schema information add /$metadata at the end of the URI.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN4CoDUuRBROcjnDfUxns1DNuaqQvWvMjnWzI-_EmNvjjjWPhkmDcv5si-ah2xCoec3sFtkV2BnegC2ADh4LordjkNxI8msyBnXsFh_vPWA_aflPgf8jCPAecaWK0ItTUxje5mXS6gycU/s1600/wcfDataService3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN4CoDUuRBROcjnDfUxns1DNuaqQvWvMjnWzI-_EmNvjjjWPhkmDcv5si-ah2xCoec3sFtkV2BnegC2ADh4LordjkNxI8msyBnXsFh_vPWA_aflPgf8jCPAecaWK0ItTUxje5mXS6gycU/s1600/wcfDataService3.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now we will create a web form and we will get the data from the gamesDataService and will display it in a gridview. Add a new webform wfGames to the project, then add a gridview named gvGames to it. The aspx code is:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;  
 &amp;lt;head runat=&quot;server&quot;&amp;gt;  
   &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;  
 &amp;lt;/head&amp;gt;  
 &amp;lt;body&amp;gt;  
   &amp;lt;form id=&quot;form1&quot; runat=&quot;server&quot;&amp;gt;  
   &amp;lt;div&amp;gt;  
     &amp;lt;asp:GridView ID=&quot;gvGames&quot; runat=&quot;server&quot; &amp;gt;&amp;lt;/asp:GridView&amp;gt;  
   &amp;lt;/div&amp;gt;  
   &amp;lt;/form&amp;gt;  
 &amp;lt;/body&amp;gt;  
 &amp;lt;/html&amp;gt;  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Before we write code for data binding first add a reference to the WCF service. Right click on the solution and select Add Service Reference, a dailogue window is opened click the Discover button, it will show the gamesDataService, then click the OK button.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcC6yo6X7X5bYIO4N21Q427Js76XTF_Pw-kEXx6urTPRpL5d5IvCACyZuJ0Lg_qc0hKGgF-MTb6jnLiblFkr1l6eVAlNp5xXoYDYMDjA_fxpQMk06TeXcAmHStDydvy084Kgfc1V_LIdE/s1600/wcfDataService4.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcC6yo6X7X5bYIO4N21Q427Js76XTF_Pw-kEXx6urTPRpL5d5IvCACyZuJ0Lg_qc0hKGgF-MTb6jnLiblFkr1l6eVAlNp5xXoYDYMDjA_fxpQMk06TeXcAmHStDydvy084Kgfc1V_LIdE/s1600/wcfDataService4.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now in the Page Load event add the following code to retrieve data and display it in a grid view:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;namespace wcfDataService  
 {  
   public partial class wfGames : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       gamesDataService gds = new gamesDataService();  
       var query = (from f in gds.Game  
              select f).ToList();  
       gvGames.DataSource = query;  
       gvGames.DataBind();  
     }  
   }  
 }  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;We create an object of&amp;nbsp;&lt;span style=&quot;background-color: #f0f0f0; font-size: 12px; line-height: 20px;&quot;&gt;gamesDataService class and then write a simple LINQ query to get all the games and then bind the list with the grid view. The following output is shown in the browser:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHQRA5DBID4-PH6MA8imDOsEt1-J_PFIL-U5Mmu_uh12QVYofKGgLCIl5YrWUltlWq7DaECi5TvKk99su1ioWhwhlWg1MHz6RmHZKoJKRcpIReZmOfAfO_iF_YT1ZFw1aghJSNr_IiRy8/s1600/wcfDataService5.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHQRA5DBID4-PH6MA8imDOsEt1-J_PFIL-U5Mmu_uh12QVYofKGgLCIl5YrWUltlWq7DaECi5TvKk99su1ioWhwhlWg1MHz6RmHZKoJKRcpIReZmOfAfO_iF_YT1ZFw1aghJSNr_IiRy8/s1600/wcfDataService5.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;background-color: #f0f0f0; line-height: 20px;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Thanks for reading this post, if you like it then share it with others, you can also the download complete source code from below sky drive link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f0f0f0; line-height: 20px;&quot;&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;&lt;a href=&quot;http://sdrv.ms/MCsiq7&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Download WCFDataService Project&lt;/b&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f0f0f0; line-height: 20px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f0f0f0; line-height: 20px;&quot;&gt;References:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f0f0f0; line-height: 20px;&quot;&gt;Accessing Data with Microsoft.NET Framework 4 Ebook&lt;/span&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/06/create-and-consume-wcf-data-service.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzq_q0n_nJfs-wFzNJ3J4HtXBpI62io9K562Qcls8M2Sl_qhcIB2pIh_7UpizTmQyTNvgb2caBBD9F-ytHUJRMlTK_9Q5oTfW6UO6n73JUNZCPS43f8oJrvSlhDTn_kdEpqf32xlbCPrE/s72-c/wcfDataService1.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-4927137420580292312</guid><pubDate>Fri, 01 Jun 2012 08:04:00 +0000</pubDate><atom:updated>2012-08-24T11:40:29.350+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Ajax Enabled WCF Service</category><category domain="http://www.blogger.com/atom/ns#">Asp.Net</category><category domain="http://www.blogger.com/atom/ns#">Call service in javascript</category><category domain="http://www.blogger.com/atom/ns#">Calling a WCF Service</category><category domain="http://www.blogger.com/atom/ns#">WCF service</category><category domain="http://www.blogger.com/atom/ns#">web service</category><title>Calling a WCF Service from Client Side( JavaScript ) ( Ajax Enabled WCF Service )</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In this post I will show you how to call the WCF service from the&amp;nbsp;client side. First of all create a new Asp.net empty website. From the add new item dialog box add a new web form. In this web form add two text boxes and a label control. In text boxes we will take the numbers from the user and in label we will show the sum of the numbers, also add a button control. The aspx source code looks like:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;&amp;lt;asp:TextBox ID=&quot;txtNum1&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; /&amp;gt;  
 &amp;lt;br /&amp;gt;  
 &amp;lt;asp:TextBox ID=&quot;txtNum2&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; /&amp;gt;  
 &amp;lt;br /&amp;gt;  
 &amp;lt;input name=&quot;ButtonCalculate&quot; type=&quot;button&quot; value=&quot;Sum&quot;  
 onclick=&quot;ButtonCalculate_onclick()&quot; /&amp;gt;  
 &amp;lt;br /&amp;gt;  
 &amp;lt;asp:Label ID=&quot;lblResult&quot; runat=&quot;server&quot; ClientIDMode=&quot;Static&quot; &amp;gt;&amp;lt;/asp:Label&amp;gt;  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now after &amp;nbsp;this from the add new item&amp;nbsp;dialogue&amp;nbsp;box add a new Ajax enabled WCF service to your solution. It will add two files one with .svc and one with .cs extension. Now open the .cs file and write a&amp;nbsp;function&amp;nbsp;for calculating the sum of two numbers. The code is below:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;[ServiceContract(Namespace = &quot;&quot;)]  
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
 public class ServiceAddNumbers  
 {       
      [OperationContract]  
      public int AddNumbes(int a,int b)  
      {           
           return a+b;  
      }        
 }  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In the above code namespace attribute is not necessary. We created a function AddNumbers which takes two integers and return its sum.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now we will look at how to call this service from the client side. First add a script manager to the page. In source of Script Manager add a services tag and inside it add a&amp;nbsp;reference&amp;nbsp;to the wcf service. The code is below:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;&amp;lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&amp;gt;  
 &amp;lt;Services&amp;gt;  
     &amp;lt;asp:ServiceReference Path=&quot;~/ServiceAddNumbers.svc&quot; /&amp;gt;  
 &amp;lt;/Services&amp;gt;  
 &amp;lt;/asp:ScriptManager&amp;gt;  
&lt;/pre&gt;After this we will write a&amp;nbsp;JavaScript&amp;nbsp;function to call the service. The function code is below :&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;&amp;lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&amp;gt;  
     function ButtonCalculate_onclick() {  
       var svc = new ServiceAddNumbers;  
       svc.AddNumbes(txtNum1.value, txtNum2.value, onSuccess, onFail, null);  
       function onSuccess(result) {         
         lblResult.innerText = result;  
       }  
       function onFail(result) {  
         alert(result);  
       }            
 }  
   &amp;lt;/script&amp;gt;  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The svc.AddNumbers function took five parameters first and second are the numbers,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;onSuccess&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;function is called on successfully calling the service and onFail if an error occured and last parameter is usercontext set it to null.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now the code is completed. Just run it add two numbers and it will show the sum of two numbers in the label control.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;Download the complete Visual Studio Solution from below link:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://sdrv.ms/JDcPKK&quot; target=&quot;_blank&quot;&gt;Calling WCF Service from Client Side&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Thanks.&lt;/span&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/06/calling-wcf-service-from-client-side.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-2538587815164196879</guid><pubDate>Fri, 25 May 2012 07:15:00 +0000</pubDate><atom:updated>2012-08-28T11:08:37.907+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataSet</category><category domain="http://www.blogger.com/atom/ns#">datatable</category><category domain="http://www.blogger.com/atom/ns#">Retrun a datatable from sql clr procedure</category><category domain="http://www.blogger.com/atom/ns#">send dataset</category><category domain="http://www.blogger.com/atom/ns#">send datatable</category><category domain="http://www.blogger.com/atom/ns#">sql</category><category domain="http://www.blogger.com/atom/ns#">SQL CLR Project</category><category domain="http://www.blogger.com/atom/ns#">sql server</category><category domain="http://www.blogger.com/atom/ns#">sql server 2005</category><category domain="http://www.blogger.com/atom/ns#">sql server 2008</category><category domain="http://www.blogger.com/atom/ns#">SqlContext</category><category domain="http://www.blogger.com/atom/ns#">SqlContext.Pipe</category><title>Send DataSet/DataTable From Sql Clr Stored Procedure</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In this post I will show you how to send a datatable or dataset from a sql clr procedure. Firstly create a new sql clr project. If you have no idea how to create a sql clr project then see the following post.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;http://wfjanjua.blogspot.com/2012/04/creating-sql-clr-stored-procedure-using.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Creating a sql clr project&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;
After creating sql clr project go to Add New Item and add a new stored procedure template. This will create the following code:&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:csharp&quot;&gt;public partial class StoredProcedures  
 {  
   [Microsoft.SqlServer.Server.SqlProcedure]  
   public static void SendData()  
   {      
      
   }  
 };  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Now I will write two functions SendDataSet and SendDataTable. In SendDataSet We will check whether DataSet is null or not, if dataset is not null then we will call the SendDataTable function. In&amp;nbsp;SendDataTable function we will first&amp;nbsp;determine&amp;nbsp;the datatypes of datacolumns, for this we have a function&amp;nbsp;ExtractDataTableColumnMetaData. This function will return the metadata of the given datatable. Now we will assign the metadata to the SqlDataRecord. After assigning it we will use the SqlContext.Pipe methods to Send the row. Below is the complete code.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;public partial class StoredProcedures  
 {  
   [Microsoft.SqlServer.Server.SqlProcedure]  
   public static void SendData()  
   {  
     string connectionString = &quot;Data Source=.;Initial Catalog=Northwind;Integrated Security=SSPI;&quot;;  
     string cmd = &quot;select * from categories; select * from customers&quot;;  
     using (SqlConnection conn = new SqlConnection(connectionString))  
     {  
       SqlCommand sqlCmd = new SqlCommand(cmd, conn);  
       SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);  
   
       DataSet ds = new DataSet(&quot;Test&quot;);  
       adapter.Fill(ds);  
       SendDataSet(ds);  
     }  
   }  
   
   public static void SendDataSet(DataSet ds)  
   {  
     if (ds == null)  
     {  
       throw new ArgumentException(&quot;SendDataSet requires a non-null data set.&quot;);  
     }  
     else  
     {  
       foreach (DataTable dt in ds.Tables)  
       {  
         SendDataTable(dt);  
       }  
     }  
   }  
   
   
   public static void SendDataTable(DataTable dt)  
   {  
     bool[] coerceToString; // Do we need to coerce this column to string?  
     SqlMetaData[] metaData = ExtractDataTableColumnMetaData(dt, out coerceToString);  
   
     SqlDataRecord record = new SqlDataRecord(metaData);  
     SqlPipe pipe = SqlContext.Pipe;  
     pipe.SendResultsStart(record);  
     try  
     {  
       foreach (DataRow row in dt.Rows)  
       {  
         for (int index = 0; index &amp;lt; record.FieldCount; index++)  
         {  
           object value = row[index];  
           if (null != value &amp;amp;&amp;amp; coerceToString[index])  
             value = value.ToString();  
           record.SetValue(index, value);  
         }  
          
         pipe.SendResultsRow(record);  
       }  
     }  
     finally  
     {  
       pipe.SendResultsEnd();  
     }  
   }  
   
   private static SqlMetaData[] ExtractDataTableColumnMetaData(DataTable dt, out bool[] coerceToString)  
   {  
     SqlMetaData[] metaDataResult = new SqlMetaData[dt.Columns.Count];  
     coerceToString = new bool[dt.Columns.Count];  
     for (int index = 0; index &amp;lt; dt.Columns.Count; index++)  
     {  
       DataColumn column = dt.Columns[index];  
       metaDataResult[index] = SqlMetaDataFromColumn(column, out coerceToString[index]);  
     }  
   
     return metaDataResult;  
   }  
   
   private static Exception InvalidDataTypeCode(TypeCode code)  
   {  
     return new ArgumentException(&quot;Invalid type: &quot; + code);  
   }  
   
   private static Exception UnknownDataType(Type clrType)  
   {  
     return new ArgumentException(&quot;Unknown type: &quot; + clrType);  
   }  
   
   private static SqlMetaData SqlMetaDataFromColumn(DataColumn column, out bool coerceToString)  
   {  
     coerceToString = false;  
     SqlMetaData smd = null;  
     Type clrType = column.DataType;  
     string name = column.ColumnName;  
     switch (Type.GetTypeCode(clrType))  
     {  
       case TypeCode.Boolean: smd = new SqlMetaData(name, SqlDbType.Bit); break;  
       case TypeCode.Byte: smd = new SqlMetaData(name, SqlDbType.TinyInt); break;  
       case TypeCode.Char: smd = new SqlMetaData(name, SqlDbType.NVarChar, 1); break;  
       case TypeCode.DateTime: smd = new SqlMetaData(name, SqlDbType.DateTime); break;  
       case TypeCode.DBNull: throw InvalidDataTypeCode(TypeCode.DBNull);  
       case TypeCode.Decimal: smd = new SqlMetaData(name, SqlDbType.Decimal, 18, 0); break;  
       case TypeCode.Double: smd = new SqlMetaData(name, SqlDbType.Float); break;  
       case TypeCode.Empty: throw InvalidDataTypeCode(TypeCode.Empty);  
       case TypeCode.Int16: smd = new SqlMetaData(name, SqlDbType.SmallInt); break;  
       case TypeCode.Int32: smd = new SqlMetaData(name, SqlDbType.Int); break;  
       case TypeCode.Int64: smd = new SqlMetaData(name, SqlDbType.BigInt); break;  
       case TypeCode.SByte: throw InvalidDataTypeCode(TypeCode.SByte);  
       case TypeCode.Single: smd = new SqlMetaData(name, SqlDbType.Real); break;  
       case TypeCode.String: smd = new SqlMetaData(name, SqlDbType.NVarChar, column.MaxLength);  
         break;  
       case TypeCode.UInt16: throw InvalidDataTypeCode(TypeCode.UInt16);  
       case TypeCode.UInt32: throw InvalidDataTypeCode(TypeCode.UInt32);  
       case TypeCode.UInt64: throw InvalidDataTypeCode(TypeCode.UInt64);  
       case TypeCode.Object:  
         smd = SqlMetaDataFromObjectColumn(name, column, clrType);  
         if (smd == null)  
         {  
           // Unknown type, try to treat it as string;  
           smd = new SqlMetaData(name, SqlDbType.NVarChar, column.MaxLength);  
           coerceToString = true;  
         }  
         break;  
   
       default: throw UnknownDataType(clrType);  
     }  
   
     return smd;  
   }  
   
   private static SqlMetaData SqlMetaDataFromObjectColumn(string name, DataColumn column, Type clrType)  
   {  
     SqlMetaData smd = null;  
     if (clrType == typeof(System.Byte[]) || clrType == typeof(SqlBinary) || clrType == typeof(SqlBytes) ||  
   clrType == typeof(System.Char[]) || clrType == typeof(SqlString) || clrType == typeof(SqlChars))  
       smd = new SqlMetaData(name, SqlDbType.VarBinary, column.MaxLength);  
     else if (clrType == typeof(System.Guid))  
       smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier);  
     else if (clrType == typeof(System.Object))  
       smd = new SqlMetaData(name, SqlDbType.Variant);  
     else if (clrType == typeof(SqlBoolean))  
       smd = new SqlMetaData(name, SqlDbType.Bit);  
     else if (clrType == typeof(SqlByte))  
       smd = new SqlMetaData(name, SqlDbType.TinyInt);  
     else if (clrType == typeof(SqlDateTime))  
       smd = new SqlMetaData(name, SqlDbType.DateTime);  
     else if (clrType == typeof(SqlDouble))  
       smd = new SqlMetaData(name, SqlDbType.Float);  
     else if (clrType == typeof(SqlGuid))  
       smd = new SqlMetaData(name, SqlDbType.UniqueIdentifier);  
     else if (clrType == typeof(SqlInt16))  
       smd = new SqlMetaData(name, SqlDbType.SmallInt);  
     else if (clrType == typeof(SqlInt32))  
       smd = new SqlMetaData(name, SqlDbType.Int);  
     else if (clrType == typeof(SqlInt64))  
       smd = new SqlMetaData(name, SqlDbType.BigInt);  
     else if (clrType == typeof(SqlMoney))  
       smd = new SqlMetaData(name, SqlDbType.Money);  
     else if (clrType == typeof(SqlDecimal))  
       smd = new SqlMetaData(name, SqlDbType.Decimal, SqlDecimal.MaxPrecision, 0);  
     else if (clrType == typeof(SqlSingle))  
       smd = new SqlMetaData(name, SqlDbType.Real);  
     else if (clrType == typeof(SqlXml))  
       smd = new SqlMetaData(name, SqlDbType.Xml);  
     else  
       smd = null;  
   
     return smd;  
   }  
   
 };  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;After writing the whole code, Deploy the solution.Before deploying Must remember to change the Permission Level to external or unsafe, and also remember to give trustworthy right to the database by running the following command on the sql server management studio.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;alter database DataBaseName set trustworthy on  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;and one more thing CLR integration must be enabled, if it is not then run this command&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;sp_configure &#39;clr enabled&#39;, 1     
 GO     
 RECONFIGURE     
 GO  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Download the whole solution from skydrive, just change the database reference. Thanks.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;a href=&quot;http://sdrv.ms/L0t6FQ&quot; target=&quot;_blank&quot;&gt;SendDataSet&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;References:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The below msdn post helps me alot&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ff878201.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ff878201.aspx&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/05/send-datasetdatatable-from-sql-clr.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-1657511697380560596</guid><pubDate>Wed, 09 May 2012 06:37:00 +0000</pubDate><atom:updated>2012-08-24T12:36:07.059+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ArrayList</category><category domain="http://www.blogger.com/atom/ns#">FillRow Method</category><category domain="http://www.blogger.com/atom/ns#">sql</category><category domain="http://www.blogger.com/atom/ns#">SQL CLR Project</category><category domain="http://www.blogger.com/atom/ns#">sql server 2008</category><category domain="http://www.blogger.com/atom/ns#">Table Value Function</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><title>SQL CLR Table Valued Function</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;SQL CLR project allows the developers to write code in .Net. In this post I will create a SQL CLR function that will return a table as a result.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;For those people who did not knew how to create a Sql Clr project first take a look at the following post&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://wfjanjua.blogspot.com/2012/04/creating-sql-clr-stored-procedure-using.html&quot; target=&quot;_blank&quot;&gt;Creating Sql Clr Stored Procedure&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;After creating a SQL CLR project. From the Project Menu or Add New Item dialog box add a new class file. In the class create a function having return type IEnumerable and add the SqlFunction attribute to header of your function as shown below:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;public partial class Functions  
 {  
   [Microsoft.SqlServer.Server.SqlFunction]  
   public static IEnumerable fn_getData()  
   {  
      ArrayList rowsArray = new ArrayList();  
      return rowsArray;  
   }  
 }  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now add the FillRowMethodName and&amp;nbsp;TableDefinition parameters to the SqlFunction attribute. Set&amp;nbsp;FillRowMethodName&amp;nbsp;= &quot;FillRow&quot;. The FillRow method is implicity called by the SQL CLR framework each time the MoveNext() on the&amp;nbsp;returned&amp;nbsp;IEnumerable ( type that implements IEnumerable interface ) is called. In&amp;nbsp;TableDefinition&amp;nbsp;specify the columns of the table. See the below code&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName=&quot;FillRow&quot;,TableDefinition=&quot;Name nvarchar(30), Age int, City nvarchar(30)&quot;)]  
   public static IEnumerable fn_getData()  
   {  
     ArrayList rowsArray = new ArrayList();  
     return rowsArray;  
   }  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now we will create a function SetData in which we will fill the arraylist with some data.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;&amp;nbsp;public static void SetData(ArrayList rowsArray)
&amp;nbsp; &amp;nbsp; &amp;nbsp;{
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; object[] column = new object[3];
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; column[0] = &quot;John&quot;;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; column[1] = 18;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; column[2] = &quot;Newyork&quot;;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rowsArray.Add(column);
&amp;nbsp; &amp;nbsp; &amp;nbsp;}
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;After this we will finally write the FillRow method. This method will write the row by row data of ArrayList to the table. It has an object parameter which will give the row of an arrayList. The signature of FillRow method is&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;private static void FillRow(Object obj, out [col1_type] [column Name], ... )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In FillRow method we will store the obj in an object Array and cast the columns to their respective types as shown below:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;public static void FillRow(object obj, out string Name, out int Age, out string City)  
    {  
     object[] row = (object[])obj;  
     Name = (string)row[0];  
     Age = (int)row[1];  
     City = (string)row[2];  
   }  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;It gets called once each time the framework calls .MoveNext() on the underlying ArrayList method (which implements the inherited IEnumerable interface)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The complete code is below:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;public partial class Functions  
 {  
   [Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName=&quot;FillRow&quot;,TableDefinition=&quot;Name nvarchar(30), Age int, City nvarchar(30)&quot;)]  
   public static IEnumerable fn_getData()  
   {  
     ArrayList rowsArray = new ArrayList();  
     SetData(rowsArray);  
     return rowsArray;  
   }  
    public static void FillRow(object obj, out string Name, out int Age, out string City)  
    {  
     object[] row = (object[])obj;  
     Name = (string)row[0];  
     Age = (int)row[1];  
     City = (string)row[2];  
   }  
    public static void SetData(ArrayList rowsArray)  
    {  
     object[] column = new object[3];  
     column[0] = &quot;John&quot;;  
     column[1] = 18;  
     column[2] = &quot;Newyork&quot;;  
     rowsArray.Add(column);  
     object[] column1 = new object[3];  
     column1[0] = &quot;Scott&quot;;  
     column1[1] = 25;  
     column1[2] = &quot;Washington&quot;;  
     rowsArray.Add(column1);  
   }  
 }  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now deploy the project and move to Sql Server. Open the database in which you have deployed the function. I have used NorthWind database. I ran this query and got the desired output&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_JWwEbsAEuaT8HthSlJOzS_dkfmoBW2ty7F98DEW5A9c8ZWBMw8vI7TcdHud5Je9t9DtNZTSpdQEhwE-ob-uV3ovVpaNLhy5HGcuGqm8W3-MIuTq7nxV1j4STDsbc_zotAlEbYfknSyg/s1600/tvfoutput.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_JWwEbsAEuaT8HthSlJOzS_dkfmoBW2ty7F98DEW5A9c8ZWBMw8vI7TcdHud5Je9t9DtNZTSpdQEhwE-ob-uV3ovVpaNLhy5HGcuGqm8W3-MIuTq7nxV1j4STDsbc_zotAlEbYfknSyg/s1600/tvfoutput.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
References:&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms131103(v=sql.90).aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms131103(v=sql.90).aspx&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;http://blogs.msdn.com/b/stuartpa/archive/2005/07/21/441468.aspx&quot;&gt;http://blogs.msdn.com/b/stuartpa/archive/2005/07/21/441468.aspx&lt;/a&gt; &lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/05/sql-clr-table-valued-function-returns.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_JWwEbsAEuaT8HthSlJOzS_dkfmoBW2ty7F98DEW5A9c8ZWBMw8vI7TcdHud5Je9t9DtNZTSpdQEhwE-ob-uV3ovVpaNLhy5HGcuGqm8W3-MIuTq7nxV1j4STDsbc_zotAlEbYfknSyg/s72-c/tvfoutput.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-2365993073384554064</guid><pubDate>Sun, 29 Apr 2012 09:47:00 +0000</pubDate><atom:updated>2012-08-24T13:06:19.192+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CLR Integration</category><category domain="http://www.blogger.com/atom/ns#">SQL CLR Project</category><category domain="http://www.blogger.com/atom/ns#">sql server 2005</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><category domain="http://www.blogger.com/atom/ns#">Write a file</category><category domain="http://www.blogger.com/atom/ns#">Write a file using Sql CLR Project</category><category domain="http://www.blogger.com/atom/ns#">Write a text file</category><title>Creating a SQL CLR Stored Procedure using VS 2010 and Sql Server 2008</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;SQL CLR give us the ability to write code in .Net Framework. In Visual Studio 2005,8 and 10 there is a database project namely SQL CLR Database project as shown in the below image.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGgFIUlzyzSGxsFCHPb72LmJN1X-WS47MCKL-VHqFvOozQr4H5Ab35O58876vJSTB46bjDNQ-wxv3IgHUvPBJ87KjiMJjv35tEl3LTcsdaHuXq34TVcQgEwFMXere2LblLMU6lwZJIj8/s1600/1.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGgFIUlzyzSGxsFCHPb72LmJN1X-WS47MCKL-VHqFvOozQr4H5Ab35O58876vJSTB46bjDNQ-wxv3IgHUvPBJ87KjiMJjv35tEl3LTcsdaHuXq34TVcQgEwFMXere2LblLMU6lwZJIj8/s1600/1.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Before creating the project please change the .net framework version to .net Framework 3.5 from the drop down menu.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;After choosing the template you have to choose a database in which you will deploy the solution. The following window will open when you press OK button.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtlHnzHn9kfYrnhAeLumGNqrmhsf6NqhdhX0U4LpeUVbNNObOicWSMXNaCQFi-DPX4oRO3Xr0mdoGxYhHHeQ1swZ9tlS8cz_WxY5d9vJOHaIziIGoFbZxk5qYVszmEH_vHKDf2kfbmK5I/s1600/2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtlHnzHn9kfYrnhAeLumGNqrmhsf6NqhdhX0U4LpeUVbNNObOicWSMXNaCQFi-DPX4oRO3Xr0mdoGxYhHHeQ1swZ9tlS8cz_WxY5d9vJOHaIziIGoFbZxk5qYVszmEH_vHKDf2kfbmK5I/s1600/2.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We will use Northwind database. Press the OK button. The following window will open&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinzP8busmuGMVqtix_-nh_kpZtcGDKE6XRyU2ttqh5KhhG2Se25mHrnS4hx4VQZpHvrpyyYSSOPqUUkh_Jy7OnqgtBf-w7x-RYkI19Jphjo8NT6SzuG1-OeKKOq0m41g064srgpbzOSps/s1600/3.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinzP8busmuGMVqtix_-nh_kpZtcGDKE6XRyU2ttqh5KhhG2Se25mHrnS4hx4VQZpHvrpyyYSSOPqUUkh_Jy7OnqgtBf-w7x-RYkI19Jphjo8NT6SzuG1-OeKKOq0m41g064srgpbzOSps/s1600/3.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;To enable the debugging press &amp;nbsp;the Yes button otherwise No. Now the project is created. The project consists of following folders and files.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS8VKiiMRbsuSexoKn8dKOamgZMogJhS7iUfvUoCiDsNIhummdbFILdqCqPEXys7yVSwr4b7yPZf0xV56KTEyGDK_sTN6t7vs12rIzaAoNLWBTqua9EXc-FjCLh4qbu_R4GeyT5C7oeOU/s1600/15.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS8VKiiMRbsuSexoKn8dKOamgZMogJhS7iUfvUoCiDsNIhummdbFILdqCqPEXys7yVSwr4b7yPZf0xV56KTEyGDK_sTN6t7vs12rIzaAoNLWBTqua9EXc-FjCLh4qbu_R4GeyT5C7oeOU/s1600/15.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now from the project menu select&amp;nbsp;stored procedure to add a code file.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghgnzfY4d4N_MwnJufPlbdQmZuzD0FjBy4lPktef2tgKuWqwAPV7lEElOsS2xvcZLj2BG6Vmk-HKrDEaRk2O-0fF3DeLVBu3IxcHz8z3nCjjuX6TMxSH5KJ2qIMCBvDDnDpnz1ZOEj2Pc/s1600/16.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghgnzfY4d4N_MwnJufPlbdQmZuzD0FjBy4lPktef2tgKuWqwAPV7lEElOsS2xvcZLj2BG6Vmk-HKrDEaRk2O-0fF3DeLVBu3IxcHz8z3nCjjuX6TMxSH5KJ2qIMCBvDDnDpnz1ZOEj2Pc/s1600/16.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Add the&amp;nbsp;stored procedure&amp;nbsp;and give a name to it. We gave a name sp_WriteFile to it. We use the File Class method WriteAllText to write a file to the Sytem. The complete code consists of only one line as shown in the below image.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVZGeMtD6xbAl2tgC54ECPlqlAqRH1YP0Seklo3TkdxdFS2icWehCuGRbAJzlkUOntiZQEBFwxvzDAlJGqdswwq-toPW6y6836fMiFw2ynmrgCILzdFAzcW1T2kwiqo5hsb9-sPVX8CGs/s1600/6.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVZGeMtD6xbAl2tgC54ECPlqlAqRH1YP0Seklo3TkdxdFS2icWehCuGRbAJzlkUOntiZQEBFwxvzDAlJGqdswwq-toPW6y6836fMiFw2ynmrgCILzdFAzcW1T2kwiqo5hsb9-sPVX8CGs/s1600/6.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The sp has two parameters FileName and FileContent. Now go to project properties select Database tab and set the Permission Level to external or Unsafe. By default permission level is set to Safe.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNuAlSAhfXknmgL2oFc2ePBviSLlbgjTnTMakr59uWi4Pwwq4UvFgc_AGXlt1bjhIIG7ktZiY-eHzPW4U_Uo0piSGiiU95-YTXfNM_4WYrS973xFst0pGOJGMhinCFDKWmdD6oKWn9k-E/s1600/13.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNuAlSAhfXknmgL2oFc2ePBviSLlbgjTnTMakr59uWi4Pwwq4UvFgc_AGXlt1bjhIIG7ktZiY-eHzPW4U_Uo0piSGiiU95-YTXfNM_4WYrS973xFst0pGOJGMhinCFDKWmdD6oKWn9k-E/s1600/13.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We changed the permission level because we have to access the File System. In Safe permission level we can &#39;t&amp;nbsp;access&amp;nbsp;the file system.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Read more about the permission levels at&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms345101(v=sql.100).aspx&quot; target=&quot;_blank&quot;&gt;MSDN&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now deploy the solution.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh35HjaJtNMJ3EZg5C9AbOH0qmNlUf9jSCX3ceZ0ntlRDCl-6aouDBhh3kIzI0IrWEN0MfXFsyX6_FmZdq2R5jhYC8euT-18VpbbwlS5Yw9gO1wpO1q_Pca2SjXRlvAEwzpZn4CxTNZPSU/s1600/12.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh35HjaJtNMJ3EZg5C9AbOH0qmNlUf9jSCX3ceZ0ntlRDCl-6aouDBhh3kIzI0IrWEN0MfXFsyX6_FmZdq2R5jhYC8euT-18VpbbwlS5Yw9gO1wpO1q_Pca2SjXRlvAEwzpZn4CxTNZPSU/s1600/12.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;When you deploy the project the following error may appears.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;//C:\Code\CLRProject\CLRProject\bin\Debug\CLRProject.sql(106-106): Deploy error SQL01268: .Net SqlClient Data Provider: Msg 10327, Level 14, State 1, Line 1 CREATE ASSEMBLY for assembly &#39;CLRProject&#39; failed because assembly &#39;CLRProject&#39; is not authorized for PERMISSION_SET = EXTERNAL_ACCESS. The assembly is authorized when either of the following is true: the database owner (DBO) has EXTERNAL ACCESS ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with EXTERNAL ACCESS ASSEMBLY permission.  
// An error occurred while the batch was being executed.  
// Build FAILED.  
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;As the message explains, SQLCLR code with the EXTERNAL_ACCESS permission set must either be signed or the database must be given the TRUSTWORTHY attribute. Let&#39;s make the&amp;nbsp;database TRUSTWORTHY with this ALTER DATABASE statement:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;ALTER DATABASE Northwind SET TRUSTWORTHY ON  
&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Another problem is of database ownership as mentioned in the message &quot;database owner (DBO) has EXTERNAL ACCESS ASSEMBLY permission&quot; so change the owner of the database to sa. Run the following command&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;EXEC sp_changedbowner sa  
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;CLR Integration must be enabled in order to use SQL CLR objects. By Default CLR Intergration is disabled so we first have to enable it.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Run the following script to enable Sql CLR integation.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;sp_configure &#39;show advanced options&#39;, 1;  
 GO  
 RECONFIGURE;  
 GO  
 sp_configure &#39;clr enabled&#39;, 1;  
 GO  
 RECONFIGURE;  
 GO  
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now again deploy the solution. After deployment open the Sql Server Management Studio. Select Northwind database and open the stored procedures folder and the assemblies folder. See the below image.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtRnH7VNvFCot5sbWcUMoNAn3sm8jDjaEQHKyefw-C3N34rM9AYrYFj6GNHqUTgQIEUAwy_Q_agkl9fNyIRA5hYxoNV8hUry4Sls2Jhvuv3n2MeE4QwfemOZ3lnm-T7iNuX2fIsxS3c_8/s1600/14.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtRnH7VNvFCot5sbWcUMoNAn3sm8jDjaEQHKyefw-C3N34rM9AYrYFj6GNHqUTgQIEUAwy_Q_agkl9fNyIRA5hYxoNV8hUry4Sls2Jhvuv3n2MeE4QwfemOZ3lnm-T7iNuX2fIsxS3c_8/s1600/14.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;As shown in the image sp_WriteFile is added to stored procedures and an&amp;nbsp;assembly of CLRProject&amp;nbsp;is added to the Assemblies folder.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;References:&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms131048(v=sql.100).aspx&quot; target=&quot;_blank&quot;&gt;Enabling CLR Integration&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms345101(v=sql.100).aspx&quot; target=&quot;_blank&quot;&gt;CLR Integration Code Access Security&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;http://www.mssqltips.com/sqlservertip/1662/writing-to-an-operating-system-file-using-the-sql-server-sqlclr/&quot;&gt;http://www.mssqltips.com/sqlservertip/1662/writing-to-an-operating-system-file-using-the-sql-server-sqlclr/&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/04/creating-sql-clr-stored-procedure-using.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmGgFIUlzyzSGxsFCHPb72LmJN1X-WS47MCKL-VHqFvOozQr4H5Ab35O58876vJSTB46bjDNQ-wxv3IgHUvPBJ87KjiMJjv35tEl3LTcsdaHuXq34TVcQgEwFMXere2LblLMU6lwZJIj8/s72-c/1.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-1623279937947063155</guid><pubDate>Tue, 27 Mar 2012 10:56:00 +0000</pubDate><atom:updated>2012-08-24T13:11:26.955+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">OLE Automation</category><category domain="http://www.blogger.com/atom/ns#">Ole Automation Procedures Server Configuration</category><category domain="http://www.blogger.com/atom/ns#">OLE automation stored procedures</category><category domain="http://www.blogger.com/atom/ns#">sql</category><category domain="http://www.blogger.com/atom/ns#">sql server</category><category domain="http://www.blogger.com/atom/ns#">sql server 2005</category><category domain="http://www.blogger.com/atom/ns#">sql server 2008</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><category domain="http://www.blogger.com/atom/ns#">xp_cmdshell</category><title>Steps to enable CLR Integration, OLE Automation and xp_cmdshell procedures in sql server 2005 and 2008</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;For SQL Server 2005:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Follow these steps:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;1) From start menu open Sql Server 2005 surface are configuration&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2R2a02_Cx4XB5leIVj3_ZI0pvejrDzK1yr4Tskrt9DOjn0RQ36bis4XmFRycjKSO9WJI5QZurrXm6p5GW_Lj9qy9i759wKdfWvOPlzHsdz3Pz9CiNYR7eUVUJX9lorp1baQPf32rCxvo/s1600/sql1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2R2a02_Cx4XB5leIVj3_ZI0pvejrDzK1yr4Tskrt9DOjn0RQ36bis4XmFRycjKSO9WJI5QZurrXm6p5GW_Lj9qy9i759wKdfWvOPlzHsdz3Pz9CiNYR7eUVUJX9lorp1baQPf32rCxvo/s1600/sql1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;2) Select Surface Area Configuration For Features&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg16EI7W60zJ0V-MK46qA_uoJzks9gRJDVJAJeviXwbvG12cASEod3jt3rdP_246Ypm6ux8VvLqPTQhXr19ZmIryWVNMRKa0pvcfQAu2d0O5tkmCNiWKp-VDYauZ8eScUNFAu4aS0t9Fqs/s1600/sql2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg16EI7W60zJ0V-MK46qA_uoJzks9gRJDVJAJeviXwbvG12cASEod3jt3rdP_246Ypm6ux8VvLqPTQhXr19ZmIryWVNMRKa0pvcfQAu2d0O5tkmCNiWKp-VDYauZ8eScUNFAu4aS0t9Fqs/s1600/sql2.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Select OLE Automation from Database Engine node and then checked the Enable OLE Automation checked box to enable the feature. Follow the same step for CLR Integration and xp_cmdshell. Then click the apply button and close the window. The&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;CLR Integration,&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&amp;nbsp;OLE Automation features and xp_cmdshell features are now enabled.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;For SQL Server 2008:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;1) Open the SQL Server 2008 and follow the below steps&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilkEPFxfublMDS8Hg5Hk-7zerfwlTvINh5AoGXfpH48c_tvjJqyNfIHPZ8rdlXkAWEnBgti-X4otZNGVeWfpUc395gu5xl-IaZO6tBK-QwU_Eo3GrLoZ5-TT7YEyCKQldgHanqIFoZWHI/s1600/sql3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilkEPFxfublMDS8Hg5Hk-7zerfwlTvINh5AoGXfpH48c_tvjJqyNfIHPZ8rdlXkAWEnBgti-X4otZNGVeWfpUc395gu5xl-IaZO6tBK-QwU_Eo3GrLoZ5-TT7YEyCKQldgHanqIFoZWHI/s1600/sql3.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfYDUPCTMLIETGWQ2JUV6elgHlbqXH58KutY4ZGq45-0JxvwO53o_v1I-8teJrb9zqaIR3p3c2k5CliPs31Tu9MlqTLN1_QVxRrT17puTyWxOfgOmU553noH3BrVmy3uFhLRVM40ks2IY/s1600/sql4.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfYDUPCTMLIETGWQ2JUV6elgHlbqXH58KutY4ZGq45-0JxvwO53o_v1I-8teJrb9zqaIR3p3c2k5CliPs31Tu9MlqTLN1_QVxRrT17puTyWxOfgOmU553noH3BrVmy3uFhLRVM40ks2IY/s1600/sql4.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmqJKn9QdnO-fLIjUGUjxC8PQCqeqHBURNZkMmqeDXqfmKpDCxn2dnAkFjYNw_dNDyYq5BSOVb51FUjLdVOmFp25T5PMXyVCnXM_4FhZMNExUIqMZm_oMvhhyphenhyphenppDsIhTmERSLzmwqerfg/s1600/sql5.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmqJKn9QdnO-fLIjUGUjxC8PQCqeqHBURNZkMmqeDXqfmKpDCxn2dnAkFjYNw_dNDyYq5BSOVb51FUjLdVOmFp25T5PMXyVCnXM_4FhZMNExUIqMZm_oMvhhyphenhyphenppDsIhTmERSLzmwqerfg/s1600/sql5.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;After performing the above steps click he OK button.&amp;nbsp;&amp;nbsp;The OLE Automation features and xp_cmdshell features are now enabled.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;To enable CLR Integration select the server configuration from the drop down menu.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoPE4BVjsbVODZreOFtUit_zo9RXNz7mvnkSAcoq54Ac69eFZMln87EhnVd0TNxpdGhP-GlKVeQqlq0-P5JwvI7I5G2WBwV0PmqCTtmTT8WtfF0XnfHFLJKVYmpT_wq_6_e-MwDc4KUmI/s1600/10.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoPE4BVjsbVODZreOFtUit_zo9RXNz7mvnkSAcoq54Ac69eFZMln87EhnVd0TNxpdGhP-GlKVeQqlq0-P5JwvI7I5G2WBwV0PmqCTtmTT8WtfF0XnfHFLJKVYmpT_wq_6_e-MwDc4KUmI/s1600/10.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Change the ClrIntegrationEnabled value from false to true to enable it. Now CLR Integration is enabled.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;or run the following script to enable CLR Integration&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;sp_configure &#39;show advanced options&#39;, 1;   
  GO   
  RECONFIGURE;   
  GO   
  sp_configure &#39;clr enabled&#39;, 1;   
  GO   
  RECONFIGURE;   
  GO   
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Reference:&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms161956.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms161956.aspx&lt;/a&gt; &lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/03/steps-to-enable-ole-automation-and.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2R2a02_Cx4XB5leIVj3_ZI0pvejrDzK1yr4Tskrt9DOjn0RQ36bis4XmFRycjKSO9WJI5QZurrXm6p5GW_Lj9qy9i759wKdfWvOPlzHsdz3Pz9CiNYR7eUVUJX9lorp1baQPf32rCxvo/s72-c/sql1.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-7371371252855032262</guid><pubDate>Tue, 27 Mar 2012 10:15:00 +0000</pubDate><atom:updated>2012-08-28T11:13:11.712+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">OLE automation stored procedures</category><category domain="http://www.blogger.com/atom/ns#">read a file</category><category domain="http://www.blogger.com/atom/ns#">sql</category><category domain="http://www.blogger.com/atom/ns#">sql server</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><category domain="http://www.blogger.com/atom/ns#">write file</category><category domain="http://www.blogger.com/atom/ns#">xp_cmdshell</category><title>Reading and Writing Files using T-SQL ( Sql Server )</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;Reading a File:&lt;/span&gt;&lt;/h2&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;OPENROWSET is a T-SQL function that allows reading data from many sources including using the SQL Server’s BULK import capability. One of the useful features of the BULK provider is its ability to read individual files from the file system into SQL Server, such as loading a data from a text file or a Word document into a SQL Server table.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;The BULK option was added to T-SQL in SQL Server 2005 and it persists in SQL Server 2008.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;When used with the BULK provider keyword you can name a data file to read as one of three types of objects:&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;SINGLE_BLOB, which reads a file as varbinary(max)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;SINGLE_CLOB, which reads a file as varchar(max)&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;SINGLE_NCLOB, which reads a file as nvarchar(max)&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;OPENROWSET returns a single column, named BulkColumn, as its result. Here’s an example that reads a text file:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Query:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;SELECT BulkColumn 
FROM OPENROWSET (BULK &#39;C:\Test\test.txt&#39;, SINGLE_CLOB) MyFile&lt;/pre&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;Output:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiM4L4Rku7825h3dZEnj4ufZB614Lms4wNoc_TjB69pKGu2owEwiMrLBgZTmIlUnM1tQOMUS1UqmL7yOGE_-zYO7tft8PKIoZSc4GUB6BGTi3VapsOKZkNwyZ6_PRV7dBT3PL-Q7ta-gRY/s1600/rw1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiM4L4Rku7825h3dZEnj4ufZB614Lms4wNoc_TjB69pKGu2owEwiMrLBgZTmIlUnM1tQOMUS1UqmL7yOGE_-zYO7tft8PKIoZSc4GUB6BGTi3VapsOKZkNwyZ6_PRV7dBT3PL-Q7ta-gRY/s1600/rw1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;color: blue; font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Writing a File:&lt;/span&gt;&lt;/h2&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;1) Write a File using&amp;nbsp; &lt;/span&gt;&lt;span id=&quot;internal-source-marker_0.08437939989380538&quot; style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;font-family: Cambria; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;xp_cmdshell:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: 15px; white-space: pre-wrap;&quot;&gt;Before using xp_cmdshell, we first have to enable xp_cmdshell feature, due to security reasons it is disabled by default.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: 15px; white-space: pre-wrap;&quot;&gt;See the following post&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://wfjanjua.blogspot.com/2012/03/steps-to-enable-ole-automation-and.html&quot; target=&quot;_blank&quot;&gt;Steps to enable OLE Automation and xp_cmdshell procedures in sql server 2005 and 2008&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OR&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot;&gt;EXEC sp_configure &#39;show advanced options&#39;, 1

GO

--- To update the currently configured value for advanced options.

RECONFIGURE

GO

-- To enable the feature.

EXEC sp_configure &#39;xp_cmdshell&#39;, 1

GO

--- To update the currently configured value for this feature.

RECONFIGURE

GO  
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Query:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;Declare @text varchar(max)
Declare @data cursor
Declare @a varchar(20)
Declare @b varchar(30)
DECLARE @cmd sysname, @var sysname

SET @data = Cursor for
Select FirstName,LastName from dbo.Employees // Northwind database


Open @data
WHILE (1 &amp;lt;&amp;gt; 0)
BEGIN

    FETCH NEXT FROM @data INTO @a , @b

    SET @text =&#39;First Name :&#39;+@a+&#39; and Last Name :&#39;+@b

    IF ( @@FETCH_STATUS &amp;lt;&amp;gt;  0 )
    BEGIN
        BREAK;
    END

SET @cmd = &#39;echo &#39; + @text + &#39; &amp;gt;&amp;gt; c:\var_out.txt&#39;
exec master..xp_cmdshell @cmd

END
&lt;/pre&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In the above code, we declare a cursor &amp;nbsp;@data in which we stored first name and last name of employees and then open the @data cursor and place an infinite while loop. The IF condition is placed inside the while loop to check @@Fetch_Status. The loop will continue works until the @@Fetch_Status does not return 0.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In the end the records are written to the var_out.txt file. There are multiple records in the employees table, all of them are appended in the same file.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;2) Using OLE Automation Stored Procedures:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;CREATE PROCEDURE sp_OAToFile(@FileName varchar(255), @Text1 text) AS
DECLARE @FS int, @OLEResult int, @FileID int

EXECUTE @OLEResult = sp_OACreate &#39;Scripting.FileSystemObject&#39;, @FS OUT
IF @OLEResult &amp;lt;&amp;gt; 0 PRINT &#39;Scripting.FileSystemObject&#39;

--Open a file
execute @OLEResult = sp_OAMethod @FS, &#39;OpenTextFile&#39;, @FileID OUT, @FileName, 8, 1
IF @OLEResult &amp;lt;&amp;gt; 0 PRINT &#39;OpenTextFile&#39;

--Write Text1
execute @OLEResult = sp_OAMethod @FileID, &#39;WriteLine&#39;, Null, @Text1
IF @OLEResult &amp;lt;&amp;gt; 0 PRINT &#39;WriteLine&#39;

EXECUTE @OLEResult = sp_OADestroy @FileID
EXECUTE @OLEResult = sp_OADestroy @FS

GO

SET QUOTED_IDENTIFIER OFF
GO 
SET ANSI_NULLS ON
GO
&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;In the above code, we created a stored procedure which takes two parameters file name and text. File name is the name of the file which will be created and text is the content which is written to the file.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;sp_OACreate creates an instance of OLE object.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;sp_OAMethod calls the method of OLE object.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;and last one sp_OADestroy procedure destroys the OLE object.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;References:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;OLE Automation Stored Procedures.&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms190501(v=sql.110).aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms190501(v=sql.110).aspx&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Read file reference&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.mssqltips.com/sqlservertip/1643/using-openrowset-to-read-large-files-into-sql-server/&quot;&gt;http://www.mssqltips.com/sqlservertip/1643/using-openrowset-to-read-large-files-into-sql-server/&lt;/a&gt; &lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/03/reading-and-writing-files-using-t-sql.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiM4L4Rku7825h3dZEnj4ufZB614Lms4wNoc_TjB69pKGu2owEwiMrLBgZTmIlUnM1tQOMUS1UqmL7yOGE_-zYO7tft8PKIoZSc4GUB6BGTi3VapsOKZkNwyZ6_PRV7dBT3PL-Q7ta-gRY/s72-c/rw1.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-1913627637590860473</guid><pubDate>Fri, 17 Feb 2012 13:39:00 +0000</pubDate><atom:updated>2012-08-24T13:25:01.787+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Catalog Views</category><category domain="http://www.blogger.com/atom/ns#">sql server</category><category domain="http://www.blogger.com/atom/ns#">sys.columns</category><category domain="http://www.blogger.com/atom/ns#">sys.databases</category><category domain="http://www.blogger.com/atom/ns#">sys.object</category><category domain="http://www.blogger.com/atom/ns#">sys.procedures</category><category domain="http://www.blogger.com/atom/ns#">sys.tables</category><category domain="http://www.blogger.com/atom/ns#">sys.triggers</category><category domain="http://www.blogger.com/atom/ns#">sys.views</category><category domain="http://www.blogger.com/atom/ns#">System Views</category><category domain="http://www.blogger.com/atom/ns#">TSQL</category><category domain="http://www.blogger.com/atom/ns#">Views</category><title>Important MS SQL server system Views</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Microsoft SQL server has a lot of built in system views which provides information about almost all of the objects in the database. Let&#39;s talk about the most important system views:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;1)sys.databases&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This view will return all the databases in the instance of a Sql Server.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFAjZbtt8Bk4aI6I37FrQz1ySDHZmsMXtenA8GllPggMwe7JxSvNbnBdOVpB1-zHQA8_AakZEj0_jetHo_rQr-_ygN7yJbpK6zPWL82zOxU1TyzmiucZjuMcl159Ir4WUW_VIHKikSMxE/s1600/db2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;281&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFAjZbtt8Bk4aI6I37FrQz1ySDHZmsMXtenA8GllPggMwe7JxSvNbnBdOVpB1-zHQA8_AakZEj0_jetHo_rQr-_ygN7yJbpK6zPWL82zOxU1TyzmiucZjuMcl159Ir4WUW_VIHKikSMxE/s640/db2.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Columns:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;name ( datatype sysname ) of database is unique with in the instance of a SQL Server&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;database_id ( datatype int ) of database is unique with in the instance of a SQL Server&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; font-family: Georgia, Times New Roman, serif; font-size: 13px; line-height: 18px;&quot;&gt;Database state ( type tinyint ) :&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;0 = ONLINE&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;1 = RESTORING&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;2 = RECOVERING&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;3 = RECOVERY_PENDING&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;4 = SUSPECT&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;5 = EMERGENCY&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;6 = OFFLINE&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;2) sys.objects&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This view provides information about all of the objects in the selected database.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFAjZbtt8Bk4aI6I37FrQz1ySDHZmsMXtenA8GllPggMwe7JxSvNbnBdOVpB1-zHQA8_AakZEj0_jetHo_rQr-_ygN7yJbpK6zPWL82zOxU1TyzmiucZjuMcl159Ir4WUW_VIHKikSMxE/s1600/db2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;281&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFAjZbtt8Bk4aI6I37FrQz1ySDHZmsMXtenA8GllPggMwe7JxSvNbnBdOVpB1-zHQA8_AakZEj0_jetHo_rQr-_ygN7yJbpK6zPWL82zOxU1TyzmiucZjuMcl159Ir4WUW_VIHKikSMxE/s640/db2.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The name ( datatype sysname ) is the name of the object.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The object_id ( datatype int ) is unique with in the database.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The type column ( datatype char(2) ) specifies whether it is a function, procedure or a table etc.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Important types are given below.&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;AF = Aggregate function (CLR)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;C = CHECK constraint&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;D = DEFAULT (constraint or stand-alone)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;F = FOREIGN KEY constraint&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;FN = SQL scalar function&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;FS = Assembly (CLR) scalar-function&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;FT = Assembly (CLR) table-valued function&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;IF = SQL inline table-valued function&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;IT = Internal table&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;P = SQL Stored Procedure&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;PC = Assembly (CLR) stored-procedure&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;PG = Plan guide&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;PK = PRIMARY KEY constraint&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;R = Rule (old-style, stand-alone)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;RF = Replication-filter-procedure&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;S = System base table&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;SN = Synonym&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;SQ = Service queue&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;TA = Assembly (CLR) DML trigger&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;TF = SQL table-valued-function&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;TR = SQL DML trigger&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;TT = Table type&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;U = Table (user-defined)&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;UQ = UNIQUE constraint&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;V = View&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; margin-top: 13px; position: relative;&quot;&gt;&lt;div style=&quot;font-size: 13px; line-height: 18px;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;X = Extended stored procedure&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px; line-height: 18px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-size: 13px; line-height: 18px;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Example:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;pre class=&quot;brush:sql&quot;&gt;select * from sys.objects     
where type=&#39;V&#39;
            OR
select * from sys.views
--Both queries will bring the same result.
&lt;/pre&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;3) sys.procedures&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This procedure will return all the procedures from the selected database&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;3) sys.tables&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This view will returns all the tables from the selected database&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;4) sys.views&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This view will return all the views from the selected database.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;b id=&quot;internal-source-marker_0.15709401154890656&quot; style=&quot;font-size: medium; line-height: normal; text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;background-color: transparent; font-size: 15px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;5) sys.triggers;&lt;/span&gt;&lt;/b&gt; &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;background-color: white; font-size: 13px; line-height: 18px; margin-top: 13px; position: relative;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This view will return all the triggers from the selected database.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;6) sys.columns&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;This will retrun all the columns of the objects ( Table,Views etc ) in the selected database&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Source:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;MSDN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Read more at&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial;&quot;&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms178534.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms178534.aspx&lt;/a&gt; &lt;/div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial;&quot;&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms189783.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms189783.aspx&lt;/a&gt; &lt;/div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/02/important-ms-sql-server-system-views.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFAjZbtt8Bk4aI6I37FrQz1ySDHZmsMXtenA8GllPggMwe7JxSvNbnBdOVpB1-zHQA8_AakZEj0_jetHo_rQr-_ygN7yJbpK6zPWL82zOxU1TyzmiucZjuMcl159Ir4WUW_VIHKikSMxE/s72-c/db2.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-1035376738812037669</guid><pubDate>Fri, 13 Jan 2012 06:56:00 +0000</pubDate><atom:updated>2012-01-17T10:50:11.134+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataReader</category><category domain="http://www.blogger.com/atom/ns#">DataSet</category><title>When to use DataReader, DataSet [ DataReader v/s DataSet ]</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;link href=&quot;http://www.manoli.net/csharpformat/csharp.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;&lt;/link&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;color: blue; font-size: large;&quot;&gt;&lt;b&gt;When to use DataReader and DataSet&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;ADO.NET provides two objects for retrieving relational data and storing it in memory: the&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;and the&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataReader&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;. The&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;provides an in-memory relational representation of data, a complete set of data that includes the tables that contain, order, and constrain the data, as well as the relationships between the tables.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;The&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataReader&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;provides a fast, forward-only, read-only stream of data from a database.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;When using a&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;, you will often make use of a&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataAdapter&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;(and possibly a&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;CommandBuilder&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;) to interact with your data source. Also, when using a&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;, you may employ a&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataView&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;to apply sorting and filtering to the data in the&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;. The&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;can also be inherited to create a strongly typed&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataSet&lt;/b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;in order to expose tables, rows, and columns as strongly typed object properties.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;b style=&quot;color: blue; font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 1.077em;&quot;&gt;DataSet vs. DataReader&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;To determine whether to use the&amp;nbsp;&lt;b&gt;DataSet&lt;/b&gt;&amp;nbsp;or the&amp;nbsp;&lt;b&gt;DataReader&lt;/b&gt;&amp;nbsp;when you design your application, consider the level of functionality that is needed in the application.&lt;/div&gt;&lt;div style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&lt;b&gt;Use the&amp;nbsp;DataSet&amp;nbsp;in order to do the following with your application:&lt;/b&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Navigate between multiple discrete tables of results.&lt;/span&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Manipulate data from multiple sources (for example, a mixture of data from more than one database, from an XML file, and from a spreadsheet).&lt;/span&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Exchange data between tiers or using an XML Web service. Unlike the&amp;nbsp;&lt;b&gt;DataReader&lt;/b&gt;, the&amp;nbsp;&lt;b&gt;DataSet&lt;/b&gt;&amp;nbsp;can be passed to a remote client. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Reuse the same set of rows to achieve a performance gain by caching them (such as for sorting, searching, or filtering the data). &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Perform a large amount of processing per row. Extended processing on each row returned using a&amp;nbsp;&lt;b&gt;DataReader&lt;/b&gt;&amp;nbsp;ties up the connection serving the&amp;nbsp;&lt;b&gt;DataReader&lt;/b&gt;&amp;nbsp;longer than necessary, impacting performance. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Manipulate data using XML operations such as Extensible Stylesheet Language Transformations (XSLT transformations) or XPath queries. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;Use the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;DataReader&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 13px;&quot;&gt;&amp;nbsp;in your application if you:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Do not need to cache the data.&lt;/span&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Are processing a set of results too large to fit into memory. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: 13px;&quot;&gt;Need to quickly access data once, in a forward-only and read-only manner. &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;span style=&quot;color: blue; font-family: &#39;Segoe UI&#39;, Verdana, Arial;&quot;&gt;&lt;span style=&quot;font-size: 17px;&quot;&gt;&lt;b&gt;How DataReader Works in Connected Mode&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto;&quot;&gt;The DataReader has a defined set of operations that revolve around its connected, forward-only, read-only nature (the read-only DataReader is also known as the firehose cursor of ADO.NET). A DataReader is a stream of data that is returned from a database query. When the query is executed, the first row is returned to the DataReader via the stream. The stream then remains connected to the database, poised to retrieve the next record. The DataReader reads one row at a time from the database and can only move forward, one record at a time. As the DataReader reads the rows from the database, the values of the columns in each row can be read and evaluated, but they cannot be edited.&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto;&quot;&gt;&amp;nbsp;The following code demonstrates how to retrieve a list of products from the Northwind database using a SqlDataReader object:&lt;/span&gt; &lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: x-small;&quot;&gt;&lt;span style=&quot;line-height: 16px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; sSQL = &lt;span class=&quot;str&quot;&gt;&quot;SELECT * FROM Products&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; sConnString =&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;str&quot;&gt;&quot;Server=(local);Database=Northwind;Integrated Security=SSPI;&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; (SqlConnection oCn = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; SqlConnection(sConnString))&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    SqlCommand oSelCmd = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; SqlCommand(sSQL, oCn);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    oSelCmd.CommandType = CommandType.Text;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    oCn.Open();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    SqlDataReader oDr = oSelCmd.ExecuteReader();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    DataGrid1.DataSource = oDr;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    DataGrid1.DataBind();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;ArticleNormalPara&quot; style=&quot;background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 7px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;Both a SqlConnection and a SqlCommand object are created. The SqlConnection is opened and the SqlCommand object executes the SQL query, returning the first row to the SqlDataReader. At this point the connection to the database is still open and associated with the SqlDataReader. This code shows how a SqlDataReader can be bound to a bindable object such as an ASP.NET DataGrid.&lt;/div&gt;&lt;div class=&quot;ArticleNormalPara&quot; style=&quot;background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 7px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto;&quot;&gt;Alternatively, a DataReader could be used to retrieve the rows and then loop through them manually, one by one. It can support several resultsets as well. For example, a list of products and categories could be retrieved from a database. The following code retrieves a SqlDataReader and loops through its rows, writing the first column&#39;s value for each row to the console:&lt;/div&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;SqlDataReader oDr = oCmd.ExecuteReader();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;while(oDr.Read()) {&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    Console.WriteLine(oDr[0]);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;
&lt;pre&gt;&lt;b style=&quot;color: blue; font-family: &#39;Segoe UI&#39;, Verdana, Arial; font-size: 17px; white-space: normal;&quot;&gt;How Dataset Works in Disconnected Mode&lt;/b&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;background-color: transparent; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto; white-space: normal;&quot;&gt;The DataSet is the main data storage tool in the ADO.NET disconnected architecture. Unlike the DataReader, the DataSet is not connected directly to a database through a Connection object when you populate it. Instead, to fill a DataSet from a database you first create a DataAdapter object (such as a SqlDataAdapter) for the provider and associate it with a SqlConnection object. Then the SqlDataAdapter can broker the data retrieval for the DataSet by issuing a SqlCommand against the database through the SqlConnection, retrieving the data, and filling the DataSet.&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div class=&quot;ArticleNormalPara&quot; style=&quot;border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 7px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; white-space: normal;&quot;&gt;You can think of the SqlDataAdapter as a bridge between the connected and disconnected objects. One of its purposes is to serve as the route for a rowset to get from the database to the DataSet. For example, when the SqlDataAdapter&#39;s Fill method is executed it opens its associated SqlConnection object (if not already open) and issues its associated SqlCommand object against the SqlConnection. Behind the scenes, a SqlDataReader is created implicitly and the rowset is retrieved one row at a time in succession and sent to the DataSet. Once all of the data is in the DataSet, the implicit SqlDataReader is destroyed and the SqlConnection is closed.&lt;/div&gt;&lt;div class=&quot;ArticleNormalPara&quot; style=&quot;border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: initial; outline-width: 0px; padding-bottom: 7px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; white-space: normal;&quot;&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;The following code shows how a DataSet can be filled from the Products table of the Northwind database. Notice that there is no explicit SqlDataReader object in this code sample:&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; sSQL = &lt;span class=&quot;str&quot;&gt;&quot;SELECT * FROM Products&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; sConnString = &lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;str&quot;&gt;&quot;Server=(local);Database=Northwind;Integrated Security=SSPI;&quot;&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;SqlDataAdapter oDa = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; SqlDataAdapter();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;DataSet oDs = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; DataSet();&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt;(SqlConnection oCn = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; SqlConnection(sConnString))&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    SqlCommand oSelCmd = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; SqlCommand(sSQL, oCn);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    oSelCmd.CommandType = CommandType.Text;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    oDa.SelectCommand = oSelCmd;&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    oDa.Fill(oDs, &lt;span class=&quot;str&quot;&gt;&quot;Products&quot;&lt;/span&gt;);&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto; white-space: normal;&quot;&gt;&amp;nbsp;Unlike the DataReader, the DataSet is not read-only. A DataSet can be modified, and rows can be added or deleted. Changes to a DataSet can be sent to the database via a managed provider&#39;s objects. Another key difference between the DataSet and the DataReader is that the DataSet is fully navigable. Its rows can be traversed forward or backward. The DataReader can be traversed forward only. In addition, a DataSet is highly flexible in that its DataTable objects can be filtered vertically or horizontally and they can be sorted or even searched. The DataSet is independent of any one data provider as it relies on a DataAdapter specific to each provider to broker the data between the DataSet and the database.&lt;/span&gt;
&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Segoe UI&#39;, &#39;Lucida Grande&#39;, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 16px; text-align: -webkit-auto; white-space: normal;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;Source: MSDN&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms971481.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/ms971481.aspx&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/cc188717.aspx&quot;&gt;http://msdn.microsoft.com/en-us/magazine/cc188717.aspx&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2012/01/when-to-use-datareader-dataset-adapter.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-4398601501895696300</guid><pubDate>Mon, 26 Sep 2011 19:53:00 +0000</pubDate><atom:updated>2012-08-26T22:08:55.148+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Asp.Net</category><title>Read, Edit and Create Microsoft Word documents in asp.net  using docX</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Microsoft word is mostly used in web applications. It is very easy to edit MS word documents on the server using docx.dll.&lt;/span&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Docx is a .NET Library which allows word 2007 manipulation. You can download it from codeplex.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;a href=&quot;http://docx.codeplex.com/&quot;&gt;http://docx.codeplex.com/&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;Create a web form containing a fileupload, Textbox and two buttons, one for upload and second for editing and saving the document. The code is below:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Source Code:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;                 &lt;td&gt;Upload CV&lt;/td&gt;                  &lt;td&gt;&lt;asp:fileupload id=&quot;FileUpload1&quot; runat=&quot;server&quot;&gt;                                    &lt;asp:label id=&quot;Label8&quot; runat=&quot;server&quot; text=&quot;* .doc file&quot;&gt;&lt;/asp:label&gt;                 &lt;/asp:fileupload&gt;                     &lt;/td&gt;                    &lt;/tr&gt;
&lt;tr&gt;                      &lt;td&gt;&lt;asp:button id=&quot;btnUpload&quot; onclick=&quot;btnUpload_Click&quot; runat=&quot;server&quot; text=&quot;Upload&quot; width=&quot;75px&quot;&gt;           &lt;/asp:button&gt;&lt;/td&gt;                     &lt;td style=&quot;text-align: left;&quot;&gt;&lt;asp:label id=&quot;lblupload&quot; runat=&quot;server&quot; text=&quot;&quot;&gt;&lt;/asp:label&gt; &lt;/td&gt;                                       &lt;/tr&gt;
&lt;tr&gt;                     &lt;td&gt;&lt;asp:textbox enabletheming=&quot;false&quot; height=&quot;270&quot; id=&quot;txtWord&quot; runat=&quot;server&quot; textmode=&quot;MultiLine&quot; width=&quot;580&quot;&gt;&lt;/asp:textbox&gt;    &lt;/td&gt;           &lt;/tr&gt;
&lt;tr&gt;                        &lt;td&gt;&lt;asp:button id=&quot;btnSave&quot; onclick=&quot;btnSave_Click&quot; runat=&quot;server&quot; text=&quot;Save&quot; width=&quot;80&quot;&gt;            &lt;/asp:button&gt;&lt;/td&gt;            &lt;/tr&gt;
&lt;/tbody&gt; &lt;/table&gt;&lt;/pre&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In the btnUpload event handler we write the code for saving the word file on&amp;nbsp;the server and display it in the textbox with the help of docX.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Code behind:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;protected void btnUpload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        if ( System.IO.Path.GetExtension(FileUpload1.FileName)==&quot;.docx&quot; )
        {
            FileUpload1.SaveAs(Server.MapPath(&quot;~\\CVs\\&quot;) + 
            FileUpload1.FileName);
            this.lblupload.Text = &quot;CV successfully uploaded&quot;;                
            DocX doc = DocX.Load(Server.MapPath(&quot;~\\CVs\\&quot;) + FileUpload1.FileName);      
            \\ Created a Docx variable doc and using Docx.Load(&quot;word file&quot;) to load the word 2007 file                         
            txtWord.Text = doc.Text; 
            \\Set the text property of doc to textbox text property. Text property of doc returns all the text from the word document.               
       }
       else
       {
            Response.Write(&quot;&lt;script&gt;alert(&#39;Please select a word 2007 file&#39;)&lt;/script&gt;&quot;);         
       }
    }
    else
    {
           Response.Write(&quot;&lt;script&gt;alert(&#39;Please select a word file&#39;)&lt;/script&gt;&quot;);         
    }
} 
&lt;/pre&gt;On the btnSave eventhandler we have deleted the previous word file and&amp;nbsp;created a new word document and apply simple formatting and then put the&amp;nbsp;content from textbox in the new word document and finally save it.&lt;br /&gt;
&lt;b&gt;Code for saving edited data:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:csharp&quot;&gt;protected void btnSave_Click(object sender, EventArgs e)
{
    try
    {
         FileInfo file = new FileInfo(MapPath(&quot;~\\CVs\\&quot;) + 
         ViewState[&quot;CV&quot;].ToString());                          
         if (file.Exists)
         {
             File.Delete(MapPath(&quot;~\\CVs\\&quot;) + ViewState[&quot;CV&quot;].ToString());
             \\ Delete existing word file
         }
             DocX doc = DocX.Create(Server.MapPath(&quot;~\\CVs\\&quot;) +
             ViewState[&quot;CV&quot;].ToString());
             \\ Create a new word document            
             Formatting f = new Formatting();
             f.FontFamily = new FontFamily(&quot;Tahoma&quot;);
             \\Defines font family
             doc.InsertParagraph(txtWord.Text,false,f);     
             doc.Save();            
   }
   catch(Exception)
   {
            
   }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;With DocX it&#39;s very easy to play with word files but it supports only .docx format.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Any question or suggestion is appreciable.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Regards,&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Georgia,&amp;quot;Times New Roman&amp;quot;,serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Waqar Farooq Janjua&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2011/09/read-ms-word-document-edit-it-and.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>4</thr:total><georss:featurename>Karachi, Pakistan</georss:featurename><georss:point>24.893379 67.02806099999998</georss:point><georss:box>24.634214 66.624313499999985 25.152544 67.431808499999974</georss:box></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-2177370245946511185</guid><pubDate>Sun, 18 Sep 2011 04:51:00 +0000</pubDate><atom:updated>2011-09-18T09:53:07.436+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Android</category><title>Solution of Android Error: invalid command-line parameter</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Android Error:&lt;/b&gt;&lt;br /&gt;
invalid command-line parameter: Files. &lt;br /&gt;
Hint: use &#39;@foo&#39; to launch a virtual device named &#39;foo&#39;. &lt;br /&gt;
please use -help for more information&lt;br /&gt;
&lt;br /&gt;
The problem is due to the spaces in the path of android sdk.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;For Windows XP: &lt;/b&gt;&lt;br /&gt;
If android SDK is located at C:\Program Files\Android\android-sdk&lt;br /&gt;
Then you should change the path to C:\PROGRA~1\Android\android-sdk&amp;nbsp; in the preferences of ECLIPSE IDE.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;For Windows 7(64-bit):&lt;/b&gt;&lt;br /&gt;
In window 7 there are two program files folders. Program Files and Program Files (x86). Android is installed at Program Files (x86) because android SDK is a 32-bit.&lt;br /&gt;
In window 7 SDK is located at C:\Program Files (x86)\Android\android-sdk&lt;br /&gt;
then you have to change the path to C:\PROGRA~2\Android\android-sdk&lt;br /&gt;
&lt;br /&gt;
Hope it helps.&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2011/09/solution-of-android-error-invalid.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>1</thr:total><georss:featurename>Karachi, Pakistan</georss:featurename><georss:point>24.893379 67.02806099999998</georss:point><georss:box>24.634214 66.624313499999985 25.152544 67.431808499999974</georss:box></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-6830867693080381064</guid><pubDate>Fri, 16 Sep 2011 19:17:00 +0000</pubDate><atom:updated>2012-02-23T13:14:12.465+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SharePoint Server 2010</category><category domain="http://www.blogger.com/atom/ns#">What is Sharepoint server</category><title>What is Sharepoint Server 2010</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;Introduction:&lt;/b&gt; &lt;br /&gt;
Sharepoint Server was first released in 2001 as Sharepoint portal server 2001, also in the same year Microsoft launched a content Managemet Server 2001 (CMS) . In 2003, Microsoft launched Sharepoint portal server 2003 as well as an updated version of Content Management Server. These products were ultimately combined to become Microsoft Office Sharepoint server 2007 ( MOSS ) in late 2006.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is SharePoint 2010?&lt;/b&gt; &lt;br /&gt;
&lt;i&gt;Sharepoint 2010 is a fully integrated platform that is made up of the best bits of many tried-and-tested products.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
SharePoint 2010 platform is the .NET Framework with a whole host of reusable application services such as content management, business intelligence, offline synchronization and workflow.&lt;br /&gt;
SharePoint is more than a document management tool, a content management tool, or an application that allow users to create collaborative websites. Behind all of this, Sharepoint is a new kind of operating system that runs accross an entire organisation and provides the tools and services that information workers needed on a day-to-day basis.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt; &lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2011/09/what-is-sharepoint-server-2010.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>1</thr:total><georss:featurename>Karachi, Pakistan</georss:featurename><georss:point>24.893379 67.02806099999998</georss:point><georss:box>24.634214 66.624313499999985 25.152544 67.431808499999974</georss:box></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1626721032063245108.post-2535701248419082266</guid><pubDate>Mon, 18 Apr 2011 06:03:00 +0000</pubDate><atom:updated>2011-09-18T07:45:34.286+05:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">JQuery</category><title>Jquery.</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Jquery is a JavaScript library which helps you in accessing and manipulating HTML elements. It is an open source library which works with every browser.It simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development&lt;/div&gt;</description><link>http://wfjanjua.blogspot.com/2011/04/jquery.html</link><author>noreply@blogger.com (Waqar Farooq Janjua)</author><thr:total>4</thr:total><georss:featurename>Karachi, Pakistan</georss:featurename><georss:point>24.893379 67.02806099999998</georss:point><georss:box>24.634214 66.624313499999985 25.152544 67.431808499999974</georss:box></item></channel></rss>