<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_Tutorial%2FADO.net_Database%2FCLR_Database_Objects</id>
		<title>ASP.NET Tutorial/ADO.net Database/CLR Database Objects - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_Tutorial%2FADO.net_Database%2FCLR_Database_Objects"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=ASP.NET_Tutorial/ADO.net_Database/CLR_Database_Objects&amp;action=history"/>
		<updated>2026-04-29T16:27:56Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=ASP.NET_Tutorial/ADO.net_Database/CLR_Database_Objects&amp;diff=2645&amp;oldid=prev</id>
		<title> в 15:30, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=ASP.NET_Tutorial/ADO.net_Database/CLR_Database_Objects&amp;diff=2645&amp;oldid=prev"/>
				<updated>2010-05-26T15:30:57Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:30, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=ASP.NET_Tutorial/ADO.net_Database/CLR_Database_Objects&amp;diff=2646&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=ASP.NET_Tutorial/ADO.net_Database/CLR_Database_Objects&amp;diff=2646&amp;oldid=prev"/>
				<updated>2010-05-26T11:56:39Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Building a Data Access Layer with a User-Defined Type==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
File: App_Code\DBDataLayer.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using System.Web.Configuration;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
public class DBDataLayer&lt;br /&gt;
{&lt;br /&gt;
    private static readonly string _connectionString;&lt;br /&gt;
    public List&amp;lt;DBProduct&amp;gt; GetAll()&lt;br /&gt;
    {&lt;br /&gt;
        List&amp;lt;DBProduct&amp;gt; results = new List&amp;lt;DBProduct&amp;gt;();&lt;br /&gt;
        SqlConnection con = new SqlConnection(_connectionString);&lt;br /&gt;
        SqlCommand cmd = new SqlCommand(&amp;quot;SELECT Product FROM DBProducts&amp;quot;, con);&lt;br /&gt;
        using (con)&lt;br /&gt;
        {&lt;br /&gt;
            con.Open();&lt;br /&gt;
            SqlDataReader reader = cmd.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
            {&lt;br /&gt;
                DBProduct newProduct = (DBProduct)reader[&amp;quot;Product&amp;quot;];&lt;br /&gt;
                results.Add(newProduct);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return results;&lt;br /&gt;
    }&lt;br /&gt;
    public void Insert(DBProduct productToAdd)&lt;br /&gt;
    {&lt;br /&gt;
        SqlConnection con = new SqlConnection(_connectionString);&lt;br /&gt;
        SqlCommand cmd = new SqlCommand(&amp;quot;INSERT DBProducts (Product) VALUES (@Product)&amp;quot;, con);&lt;br /&gt;
        cmd.Parameters.Add(&amp;quot;@Product&amp;quot;, SqlDbType.Udt);&lt;br /&gt;
        cmd.Parameters[&amp;quot;@Product&amp;quot;].UdtTypeName = &amp;quot;DBProduct&amp;quot;;&lt;br /&gt;
        cmd.Parameters[&amp;quot;@Product&amp;quot;].Value = productToAdd;&lt;br /&gt;
        using (con)&lt;br /&gt;
        {&lt;br /&gt;
            con.Open();&lt;br /&gt;
            cmd.ExecuteNonQuery();&lt;br /&gt;
        }&lt;br /&gt;
    }    &lt;br /&gt;
    static DBDataLayer()&lt;br /&gt;
    {&lt;br /&gt;
        _connectionString = WebConfigurationManager.ConnectionStrings[&amp;quot;Products&amp;quot;].ConnectionString;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
File: ShowDBDataLayer.aspx&lt;br /&gt;
&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; %&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot;&lt;br /&gt;
  &amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; &amp;gt;&lt;br /&gt;
&amp;lt;head id=&amp;quot;Head1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;Show DBDataLayer&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;div&amp;gt;&lt;br /&gt;
    &amp;lt;asp:GridView&lt;br /&gt;
        id=&amp;quot;grdProducts&amp;quot;&lt;br /&gt;
        DataSourceID=&amp;quot;srcProducts&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;br /&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset&amp;gt;&lt;br /&gt;
    &amp;lt;legend&amp;gt;Add Product&amp;lt;/legend&amp;gt;&lt;br /&gt;
    &amp;lt;asp:DetailsView&lt;br /&gt;
        id=&amp;quot;dtlProduct&amp;quot;&lt;br /&gt;
        DataSourceID=&amp;quot;srcProducts&amp;quot;&lt;br /&gt;
        DefaultMode=&amp;quot;Insert&amp;quot;&lt;br /&gt;
        AutoGenerateInsertButton=&amp;quot;true&amp;quot;&lt;br /&gt;
        AutoGenerateRows=&amp;quot;false&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Fields&amp;gt;&lt;br /&gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;Title&amp;quot; HeaderText=&amp;quot;Title&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;Director&amp;quot; HeaderText=&amp;quot;Director&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;asp:BoundField DataField=&amp;quot;Totals&amp;quot;&lt;br /&gt;
           HeaderText=&amp;quot;Box Office Totals&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;/Fields&amp;gt;&lt;br /&gt;
    &amp;lt;/asp:DetailsView&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
    &amp;lt;asp:ObjectDataSource&lt;br /&gt;
        id=&amp;quot;srcProducts&amp;quot;&lt;br /&gt;
        TypeName=&amp;quot;DBDataLayer&amp;quot;&lt;br /&gt;
        DataObjectTypeName=&amp;quot;DBProduct&amp;quot;&lt;br /&gt;
        SelectMethod=&amp;quot;GetAll&amp;quot;&lt;br /&gt;
        InsertMethod=&amp;quot;Insert&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
== Building Database Objects with the .NET Framework==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
Enabling CLR Integration&lt;br /&gt;
You must enable CLR integration by executing the following SQL Server command:&lt;br /&gt;
sp_configure &amp;quot;clr enabled&amp;quot;, 1&lt;br /&gt;
RECONFIGURE&lt;br /&gt;
Creating User-Defined Types with the .NET Framework&lt;br /&gt;
You can create a new user-defined type.&lt;br /&gt;
Then use it in exactly the same way as the built-in SQL types such as the Int, NVarChar, or Decimal types. &lt;br /&gt;
For example, you can create a new type and use the type to define a column in a database table.&lt;br /&gt;
To create a user-defined type with the .NET Framework&lt;br /&gt;
1. Create an assembly that contains the new type.&lt;br /&gt;
2. Register the assembly with SQL Server.&lt;br /&gt;
3. Create a type based on the assembly.&lt;br /&gt;
&lt;br /&gt;
The class must be decorated with a SqlUserDefinedType attribute.&lt;br /&gt;
The class must be able to equal NULL.&lt;br /&gt;
The class must be serializable to/from a byte array.&lt;br /&gt;
The class must be serializable to/from a string.&lt;br /&gt;
SqlUserDefinedType supports the following properties:&lt;br /&gt;
Format        specifies how a user-defined type is serialized in SQL Server. &lt;br /&gt;
              Possible values are Native and UserDefined.&lt;br /&gt;
IsByteOrdered marks the user-defined type as ordered in the same way as its byte representation.&lt;br /&gt;
IsFixedLength specifies that all instances of this type have the same length.&lt;br /&gt;
MaxByteSize   specifies the maximum size of the user-defined type in bytes.&lt;br /&gt;
Name          specifies a name for the user-defined type.&lt;br /&gt;
File: DBProduct.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Text;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
using System.Data.SqlTypes;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using System.IO;&lt;br /&gt;
[SqlUserDefinedType(Format.UserDefined, MaxByteSize = 512, IsByteOrdered = true)]&lt;br /&gt;
public class DBProduct : INullable, IBinarySerialize&lt;br /&gt;
{&lt;br /&gt;
    private bool _isNull;&lt;br /&gt;
    private string _title;&lt;br /&gt;
    private string _director;&lt;br /&gt;
    private decimal totals;&lt;br /&gt;
    public bool IsNull&lt;br /&gt;
    {&lt;br /&gt;
        get { return _isNull; }&lt;br /&gt;
    }&lt;br /&gt;
    public static DBProduct Null&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            DBProduct product = new DBProduct();&lt;br /&gt;
            product._isNull = true;&lt;br /&gt;
            return product;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public string Title&lt;br /&gt;
    {&lt;br /&gt;
        get { return _title; }&lt;br /&gt;
        set { _title = value; }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public string Director&lt;br /&gt;
    {&lt;br /&gt;
        get { return _director; }&lt;br /&gt;
        set { _director = value; }&lt;br /&gt;
    }&lt;br /&gt;
    [SqlFacet(Precision = 38, Scale = 2)]&lt;br /&gt;
    public decimal Totals&lt;br /&gt;
    {&lt;br /&gt;
        get { return totals; }&lt;br /&gt;
        set { totals = value; }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [SqlMethod(OnNullCall = false)]&lt;br /&gt;
    public static DBProduct Parse(SqlString s)&lt;br /&gt;
    {&lt;br /&gt;
        if (s.IsNull)&lt;br /&gt;
            return Null;&lt;br /&gt;
        DBProduct product = new DBProduct();&lt;br /&gt;
        string[] parts = s.Value.Split(new char[] { &amp;quot;,&amp;quot; });&lt;br /&gt;
        product.Title = parts[0];&lt;br /&gt;
        product.Director = parts[1];&lt;br /&gt;
        product.Totals = decimal.Parse(parts[2]);&lt;br /&gt;
        return product;&lt;br /&gt;
    }&lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        if (this.IsNull)&lt;br /&gt;
            return &amp;quot;NULL&amp;quot;;&lt;br /&gt;
        StringBuilder builder = new StringBuilder();&lt;br /&gt;
        builder.Append(_title);&lt;br /&gt;
        builder.Append(&amp;quot;,&amp;quot;);&lt;br /&gt;
        builder.Append(_director);&lt;br /&gt;
        builder.Append(&amp;quot;,&amp;quot;);&lt;br /&gt;
        builder.Append(totals.ToString());&lt;br /&gt;
        return builder.ToString();&lt;br /&gt;
    }&lt;br /&gt;
    public void Write(BinaryWriter w)&lt;br /&gt;
    {&lt;br /&gt;
        w.Write(_title);&lt;br /&gt;
        w.Write(_director);&lt;br /&gt;
        w.Write(totals);&lt;br /&gt;
    }&lt;br /&gt;
    public void Read(BinaryReader r)&lt;br /&gt;
    {&lt;br /&gt;
        _title = r.ReadString();&lt;br /&gt;
        _director = r.ReadString();&lt;br /&gt;
        totals = r.ReadDecimal();&lt;br /&gt;
    }&lt;br /&gt;
    public DBProduct()&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
            &lt;br /&gt;
You need to compile the DBProduct class into a separate assembly (.dll file). &lt;br /&gt;
&lt;br /&gt;
csc /t:library DBProduct.cs&lt;br /&gt;
&lt;br /&gt;
Registering the User-Defined Type Assembly with SQL Server&lt;br /&gt;
Register the DBProduct assembly by executing the following command:&lt;br /&gt;
CREATE ASSEMBLY DBProduct&lt;br /&gt;
FROM &amp;quot;C:\DBProduct.dll&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Check SQL Server by executing the following query:&lt;br /&gt;
SELECT * FROM sys.assemblies&lt;br /&gt;
&lt;br /&gt;
You can drop any assembly by executing the DROP Assembly command. &lt;br /&gt;
DROP Assembly DBProduct&lt;br /&gt;
&lt;br /&gt;
Creating the User-Defined Type&lt;br /&gt;
CREATE TYPE dbo.DBProduct EXTERNAL NAME DBProduct.DBProduct&lt;br /&gt;
&lt;br /&gt;
If you need to delete the type, you can execute the following command:&lt;br /&gt;
DROP TYPE DBProduct&lt;br /&gt;
&lt;br /&gt;
Create a new database table with the following command:&lt;br /&gt;
CREATE TABLE DBProducts(Id INT IDENTITY, Product DBProduct)&lt;br /&gt;
You can insert a new record into this table with the following command:&lt;br /&gt;
INSERT DBProducts (Product)&lt;br /&gt;
VALUES (&amp;quot;Star Wars,George Lucas,12.34&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Finally, you can perform queries against the table with queries like the following:&lt;br /&gt;
SELECT Id, Product FROM DBProducts WHERE Product.Totals &amp;gt; 13.23&lt;br /&gt;
SELECT MAX(Product.Totals) FROM DBProducts&lt;br /&gt;
SELECT Product FROM DBProducts WHERE Product.Director LIKE &amp;quot;g%&amp;quot;&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
== Creating Stored Procedures with the .NET Framework==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
You must complete the following steps:&lt;br /&gt;
1. Create an assembly that contains the stored procedure method.&lt;br /&gt;
2. Register the assembly with SQL Server.&lt;br /&gt;
3. Create a stored procedure based on the assembly.&lt;br /&gt;
Creating the Stored Procedure Assembly&lt;br /&gt;
File: RandomRows.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using Microsoft.SqlServer.Server;&lt;br /&gt;
public class RandomRows&lt;br /&gt;
{&lt;br /&gt;
    [SqlProcedure]&lt;br /&gt;
    public static void GetRandomRow()&lt;br /&gt;
    {&lt;br /&gt;
        SqlDataAdapter dad = new SqlDataAdapter(&amp;quot;SELECT Id,Title FROM Products&amp;quot;, &amp;quot;context connection=true&amp;quot;);&lt;br /&gt;
        DataTable dtblProducts = new DataTable();&lt;br /&gt;
        dad.Fill(dtblProducts);&lt;br /&gt;
        Random rnd = new Random();&lt;br /&gt;
        DataRow ranRow = dtblProducts.Rows[rnd.Next(dtblProducts.Rows.Count)];&lt;br /&gt;
        SqlDataRecord result = new SqlDataRecord(new SqlMetaData(&amp;quot;Id&amp;quot;, SqlDbType.Int), new SqlMetaData(&amp;quot;Title&amp;quot;, SqlDbType.NVarChar, 100));&lt;br /&gt;
        result.SetSqlInt32(0, (int)ranRow[&amp;quot;Id&amp;quot;]);&lt;br /&gt;
        result.SetSqlString(1, (string)ranRow[&amp;quot;Title&amp;quot;]);&lt;br /&gt;
        SqlContext.Pipe.Send(result);&lt;br /&gt;
    }&lt;br /&gt;
    [SqlProcedure]&lt;br /&gt;
    public static void GetRandomRows(int rowsToReturn)&lt;br /&gt;
    {&lt;br /&gt;
        SqlDataAdapter dad = new SqlDataAdapter(&amp;quot;SELECT Id,Title FROM Products&amp;quot;, &amp;quot;context connection=true&amp;quot;);&lt;br /&gt;
        DataTable dtblProducts = new DataTable();&lt;br /&gt;
        dad.Fill(dtblProducts);&lt;br /&gt;
        SqlDataRecord result = new SqlDataRecord(new SqlMetaData(&amp;quot;Id&amp;quot;, SqlDbType.Int), new SqlMetaData(&amp;quot;Title&amp;quot;, SqlDbType.NVarChar, 100));&lt;br /&gt;
        SqlContext.Pipe.SendResultsStart(result);&lt;br /&gt;
        Random rnd = new Random();&lt;br /&gt;
        for (int i = 0; i &amp;lt; rowsToReturn; i++)&lt;br /&gt;
        {&lt;br /&gt;
            DataRow ranRow = dtblProducts.Rows[rnd.Next(dtblProducts.Rows.Count)];&lt;br /&gt;
            result.SetSqlInt32(0, (int)ranRow[&amp;quot;Id&amp;quot;]);&lt;br /&gt;
            result.SetSqlString(1, (string)ranRow[&amp;quot;Title&amp;quot;]);&lt;br /&gt;
            SqlContext.Pipe.SendResultsRow(result);&lt;br /&gt;
        }&lt;br /&gt;
        SqlContext.Pipe.SendResultsEnd();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
csc /t:library RandomRows.cs&lt;br /&gt;
Registering the Stored Procedure Assembly with SQL Server&lt;br /&gt;
CREATE ASSEMBLY RandomRows&lt;br /&gt;
FROM &amp;quot;C:\RandomRows.dll&amp;quot;&lt;br /&gt;
To remove the assembly:&lt;br /&gt;
DROP Assembly RandomRows&lt;br /&gt;
Creating the Stored Procedures&lt;br /&gt;
CREATE PROCEDURE GetRandomRow AS&lt;br /&gt;
EXTERNAL NAME RandomRows.RandomRows.GetRandomRow&lt;br /&gt;
CREATE PROCEDURE GetRandomRows(@rowsToReturn Int) AS&lt;br /&gt;
EXTERNAL NAME RandomRows.RandomRows.GetRandomRows&lt;br /&gt;
&lt;br /&gt;
Executing the following command displays three random products from the Products database:&lt;br /&gt;
GetRandomRows 3&lt;br /&gt;
&lt;br /&gt;
If you need to delete these stored procedures, you can execute the following two commands:&lt;br /&gt;
DROP PROCEDURE GetRandomRow&lt;br /&gt;
DROP PROCEDURE GetRandomRows&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
== Executing a .NET Stored Procedure from an ASP.NET Page==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
File: App_Code\RandomDataLayer.cs&lt;br /&gt;
using System;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
using System.Web.Configuration;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
public class RandomDataLayer&lt;br /&gt;
{&lt;br /&gt;
    private static readonly string _connectionString;&lt;br /&gt;
    public List&amp;lt;String&amp;gt; GetRandomProducts()&lt;br /&gt;
    {        List&amp;lt;String&amp;gt; results = new List&amp;lt;String&amp;gt;();&lt;br /&gt;
        SqlConnection con = new SqlConnection(_connectionString);&lt;br /&gt;
        SqlCommand cmd = new SqlCommand(&amp;quot;GetRandomRows&amp;quot;, con);&lt;br /&gt;
        cmd.rumandType = CommandType.StoredProcedure;&lt;br /&gt;
        cmd.Parameters.AddWithValue(&amp;quot;@rowsToReturn&amp;quot;, 5);&lt;br /&gt;
        using (con)&lt;br /&gt;
        {&lt;br /&gt;
            con.Open();&lt;br /&gt;
            SqlDataReader reader = cmd.ExecuteReader();&lt;br /&gt;
            while (reader.Read())&lt;br /&gt;
                results.Add((string)reader[&amp;quot;Title&amp;quot;]);&lt;br /&gt;
        }&lt;br /&gt;
        return results;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static string GetRandomProduct()&lt;br /&gt;
    {&lt;br /&gt;
        string result = String.Empty;&lt;br /&gt;
        SqlConnection con = new SqlConnection(_connectionString);&lt;br /&gt;
        SqlCommand cmd = new SqlCommand(&amp;quot;GetRandomRow&amp;quot;, con);&lt;br /&gt;
        cmd.rumandType = CommandType.StoredProcedure;&lt;br /&gt;
        using (con)&lt;br /&gt;
        {&lt;br /&gt;
            con.Open();&lt;br /&gt;
            SqlDataReader reader = cmd.ExecuteReader();&lt;br /&gt;
            if (reader.Read())&lt;br /&gt;
                result = (string)reader[&amp;quot;Title&amp;quot;];&lt;br /&gt;
        }&lt;br /&gt;
        return result;&lt;br /&gt;
    }&lt;br /&gt;
    static RandomDataLayer()&lt;br /&gt;
    {&lt;br /&gt;
        _connectionString = WebConfigurationManager.ConnectionStrings[&amp;quot;Products&amp;quot;].ConnectionString;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
            &lt;br /&gt;
File: ShowRandomDataLayer.aspx&lt;br /&gt;
&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; %&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot;&lt;br /&gt;
&amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
    void Page_Load()&lt;br /&gt;
    {&lt;br /&gt;
        lblRandomProduct.Text = RandomDataLayer.GetRandomProduct();&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; &amp;gt;&lt;br /&gt;
&amp;lt;head id=&amp;quot;Head1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;Show RandomDataLayer&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;div&amp;gt;&lt;br /&gt;
    Random Product:&lt;br /&gt;
    &amp;lt;asp:Label&lt;br /&gt;
        id=&amp;quot;lblRandomProduct&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;hr /&amp;gt;&lt;br /&gt;
    &amp;lt;asp:GridView&lt;br /&gt;
        id=&amp;quot;grdProducts&amp;quot;&lt;br /&gt;
        DataSourceID=&amp;quot;srcProducts&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;asp:ObjectDataSource&lt;br /&gt;
        id=&amp;quot;srcProducts&amp;quot;&lt;br /&gt;
        TypeName=&amp;quot;RandomDataLayer&amp;quot;&lt;br /&gt;
        SelectMethod=&amp;quot;GetRandomProducts&amp;quot;&lt;br /&gt;
        Runat=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>