<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-597990716815320904</id><updated>2011-04-28T11:50:59.520+05:30</updated><category term='Using Transactions'/><category term='Changing DB in ADO.NET'/><category term='ADO.NET Counting Records'/><category term='Connecting Text File in ADO.NET'/><category term='ADO.NET Web Service'/><category term='Collection Pooling in ADO.NET'/><category term='Connecting to Pwd DB'/><category term='Conneting to Named Instance'/><category term='Connecting to Access DB'/><category term='Connection Pooling in ADO.NET'/><category term='Retrieving Hierarchical Data'/><category term='Building DataSet'/><category term='Connecting to Secured DB'/><category term='Connecting using IP Addr'/><category term='Connecting to Oracle DB'/><category term='ADO.NET Deleting Rows'/><category term='Connecting to SQL Server'/><category term='Connecting to MS Excel'/><category term='DB Independent code'/><category term='Connecting to ODBC'/><category term='Creating DataSet'/><category term='Connecting to Exc or Outlook'/><title type='text'>ADO.NET Slackers All concepts covered</title><subtitle type='html'>Best Programming Practices of ADO.NET from basics to advanced level has Connecting data, Retrieving data, Managing data, Analyzing data, Searching data, Modifying data, Adding data. Maintaining Database Integrity, Binding data to .NET Interfaces and Optimizing .NET data Access.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>satish</name><uri>http://www.blogger.com/profile/11104095808954112849</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>31</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-4391837747803324684</id><published>2008-10-16T13:43:00.001+05:30</published><updated>2008-10-16T13:43:30.425+05:30</updated><title type='text'>Testing for No Records in DB</title><content type='html'>&lt;p&gt;To determine whether any records were returned from a query that you just executed.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Use the &lt;tt&gt;DataRowCollection.Count&lt;/tt&gt; property, the &lt;tt&gt;DataReader.HasRows&lt;/tt&gt; property, or the &lt;tt&gt;DataReader.Read( )&lt;/tt&gt; method.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code creates and fills a &lt;tt&gt;DataTable&lt;/tt&gt; and uses the &lt;tt&gt;Count&lt;/tt&gt; property of the &lt;tt&gt;DataRowCollection&lt;/tt&gt; to determine if the query used to create the table returned any rows. Next, a &lt;tt&gt;DataReader&lt;/tt&gt; is created and both the &lt;tt&gt;HasRows&lt;/tt&gt; property and the &lt;tt&gt;Read( )&lt;/tt&gt; method are used to determine whether the query used to create the &lt;tt&gt;DataReader&lt;/tt&gt; returned any rows.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown here.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example: File: NoRecordTestForm.cs&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;// Table name constants&lt;br /&gt;private const String ORDERS_TABLE = "Orders";&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;&lt;br /&gt;// Fill the Orders DataTable.&lt;br /&gt;SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders",&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;DataTable orderTable = new DataTable(ORDERS_TABLE);&lt;br /&gt;da.Fill(orderTable);&lt;br /&gt;&lt;br /&gt;// Test Orders DataTable for records.&lt;br /&gt;bool tableHasRecords = orderTable.Rows.Count &amp;gt; 0;&lt;br /&gt;result.Append("DataTable " + ORDERS_TABLE + ": Has records = " +&lt;br /&gt;    tableHasRecords + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Create the Orders DataReader.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;SqlCommand cmd = new SqlCommand("SELECT * FROM ORDERS", conn);&lt;br /&gt;conn.Open( );&lt;br /&gt;SqlDataReader orderReader = cmd.ExecuteReader( );&lt;br /&gt;&lt;br /&gt;// Test Orders DataReader for records.&lt;br /&gt;result.Append("DataReader " + ORDERS_TABLE + ": Has records = " +&lt;br /&gt;    orderReader.HasRows + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Test Orders DataReader for records.&lt;br /&gt;bool readerHasRecords = orderReader.Read( );&lt;br /&gt;result.Append("DataReader " + ORDERS_TABLE + ": Has records = " +&lt;br /&gt;    readerHasRecords + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;orderReader.Close( );&lt;br /&gt;conn.Close( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p/&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"/&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;DataTable&lt;/tt&gt; contains a &lt;tt&gt;DataRowCollection&lt;/tt&gt; object that contains all &lt;tt&gt;DataRow&lt;/tt&gt; objects in the table. The &lt;tt&gt;DataRowCollection&lt;/tt&gt; has a &lt;tt&gt;Count&lt;/tt&gt; property that returns the number of rows in the table. The &lt;tt&gt;Count&lt;/tt&gt; property for an empty table has a value of 0.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;HasRows&lt;/tt&gt; property of the &lt;tt&gt;DataReader&lt;/tt&gt; returns a Boolean value indicating whether the &lt;tt&gt;DataReader&lt;/tt&gt; has any records.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Another way is to use the &lt;tt&gt;Read( )&lt;/tt&gt; method to advance the &lt;tt&gt;DataReader&lt;/tt&gt; to the next record. This returns a value of &lt;tt&gt;true&lt;/tt&gt; if a record is available and &lt;tt&gt;false&lt;/tt&gt; otherwise. The first call to the &lt;tt&gt;Read( )&lt;/tt&gt; method will indicate whether any records were returned by the &lt;tt&gt;DataReader&lt;/tt&gt;. This was the only way to determine whether the &lt;tt&gt;DataReader&lt;/tt&gt; contained any records prior to the introduction of the &lt;tt&gt;HasRows&lt;/tt&gt; property in .NET Framework 1.1.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-4391837747803324684?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/4391837747803324684/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/testing-for-no-records-in-db.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/4391837747803324684'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/4391837747803324684'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/testing-for-no-records-in-db.html' title='Testing for No Records in DB'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-1238700867361895170</id><published>2008-10-16T13:40:00.001+05:30</published><updated>2008-10-16T13:40:47.542+05:30</updated><title type='text'>Raising and Handling Stored Procedure Errors</title><content type='html'>&lt;p&gt;To catch and handle an error raised from a stored procedure.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Use a &lt;tt&gt;try . . . catch&lt;/tt&gt; block to catch serious errors. Use the &lt;tt&gt;SqlConnection.InfoMessage&lt;/tt&gt; event handler to catch informational and warning messages.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code, as shown below, uses a single stored procedure and two event handlers:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;em&gt;SP0210_Raiserror&lt;/em&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Accepts two input parameters specifying the severity and the state of an error and raises an error with the specified severity and state.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;Raise Error &lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;Button.Click&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Creates a connection and attaches a handler for warning and information messages from the SQL Server. A &lt;tt&gt;Command&lt;/tt&gt; is created for the &lt;em&gt;SP0210_Raiserror&lt;/em&gt; stored procedure and the input parameters are defined. The user-specified severity and state are assigned to the input parameters and the stored procedure command is executed within a &lt;tt&gt;try&lt;/tt&gt; statement.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;SqlConnection.InfoMessage&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Called when a warning or informational message is raised by the SQL Server.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example: Stored procedure: SP0210_Raiserror&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;CREATE PROCEDURE SP0210_Raiserror&lt;br /&gt;    @Severity int,&lt;br /&gt;    @State int = 1&lt;br /&gt;AS&lt;br /&gt;    if @Severity&amp;gt;=0 and @Severity &amp;lt;=18&lt;br /&gt;        RAISERROR ('Error of severity %d raised from SP 0210_Raiserror.', @Severity, &lt;br /&gt;             @State, @Severity)&lt;br /&gt;    &lt;br /&gt;    if @Severity&amp;gt;=19 and @Severity&amp;lt;=25&lt;br /&gt;        RAISERROR ('Fatal error of severity %d raised from SP 0210_Raiserror.',&lt;br /&gt;             @Severity, @State, @Severity) WITH LOG&lt;br /&gt;    &lt;br /&gt;    RETURN&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown below.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;strong&gt;Example: File: RaiserrorForm.cs&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void raiseErrorButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;    resultTextBox.Text =&lt;br /&gt;        "Severity: " + severityTextBox.Text + Environment.NewLine +&lt;br /&gt;        "State: " + stateTextBox.Text + Environment.NewLine +&lt;br /&gt;        Environment.NewLine;&lt;br /&gt;&lt;br /&gt;    // Create the connection.&lt;br /&gt;    SqlConnection conn = new SqlConnection(&lt;br /&gt;        ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;    // Attach handler for SqlInfoMessage events.&lt;br /&gt;    conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);&lt;br /&gt;    &lt;br /&gt;    // Define a stored procedure command and the parameters.&lt;br /&gt;    SqlCommand cmd = new SqlCommand("SP0210_Raiserror", conn);&lt;br /&gt;    cmd.CommandType = CommandType.StoredProcedure;&lt;br /&gt;    cmd.Parameters.Add("@Severity", SqlDbType.Int);&lt;br /&gt;    cmd.Parameters.Add("@State", SqlDbType.Int);&lt;br /&gt;    // Set the value for the stored procedure parameters.&lt;br /&gt;    cmd.Parameters["@Severity"].Value = severityTextBox.Text;&lt;br /&gt;    cmd.Parameters["@State"].Value = stateTextBox.Text;&lt;br /&gt;&lt;br /&gt;    // Open the connection.&lt;br /&gt;    conn.Open( );&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        // Try to execute the stored procedure.&lt;br /&gt;        cmd.ExecuteNonQuery( );&lt;br /&gt;    }&lt;br /&gt;    catch(System.Data.SqlClient.SqlException ex)&lt;br /&gt;    {&lt;br /&gt;        // Catch SqlException errors.&lt;br /&gt;        resultTextBox.Text += "ERROR: " + ex.Message;&lt;br /&gt;    }&lt;br /&gt;    catch(Exception ex)&lt;br /&gt;    {&lt;br /&gt;        // Catch other errors.&lt;br /&gt;        resultTextBox.Text += "OTHER ERROR: " + ex.Message;&lt;br /&gt;    }&lt;br /&gt;    finally&lt;br /&gt;    {&lt;br /&gt;        // Close the connection.&lt;br /&gt;        conn.Close( );&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)&lt;br /&gt;{&lt;br /&gt;    resultTextBox.Text += "MESSAGE: " + e.Message;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-1238700867361895170?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/1238700867361895170/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/raising-and-handling-stored-procedure.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1238700867361895170'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1238700867361895170'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/raising-and-handling-stored-procedure.html' title='Raising and Handling Stored Procedure Errors'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5053249489691429756</id><published>2008-10-16T13:37:00.000+05:30</published><updated>2008-10-16T13:38:03.956+05:30</updated><title type='text'>Returning an Output Parameter Using DataReader</title><content type='html'>&lt;p&gt;To access an output parameter returned by a stored procedure that you have used to create a &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Add a parameter to a &lt;tt&gt;Command&lt;/tt&gt;'s &lt;tt&gt;ParameterCollection&lt;/tt&gt; and specify the &lt;tt&gt;ParameterDirection&lt;/tt&gt; as either &lt;tt&gt;Output&lt;/tt&gt; or &lt;tt&gt;InputOutput&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code uses a single stored procedure:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;em&gt;SP0209_OutputValueWithDataReader&lt;/em&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Returns a result set containing all records from the Orders table in Northwind. The stored procedure takes one input and one output parameter and sets the value of the output parameter to the value of the input parameter.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code creates a &lt;tt&gt;DataReader&lt;/tt&gt; from a stored procedure command as shown in below example. The stored procedure returns a single output parameter, and then the stored procedure sets this value to the value of the input parameter specified by the user. The code displays the value of the output parameter at four different stages of working with the result set in the &lt;tt&gt;DataReader&lt;/tt&gt;:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Before the &lt;tt&gt;DataReader&lt;/tt&gt; is created&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Immediately after the &lt;tt&gt;DataReader&lt;/tt&gt; is created&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;After all rows in the &lt;tt&gt;DataReader&lt;/tt&gt; have been read&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;After the &lt;tt&gt;DataReader&lt;/tt&gt; is closed&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example: Stored procedure: SP0209_OutputValueWithDataReader&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;CREATE PROCEDURE SP0209_OutputValueWithDataReader&lt;br /&gt;    @ValueIn int,&lt;br /&gt;    @ValueOut int output&lt;br /&gt;AS&lt;br /&gt;    set nocount on&lt;br /&gt;&lt;br /&gt;    set @ValueOut = @ValueIn&lt;br /&gt;&lt;br /&gt;    select * from Orders&lt;br /&gt;        &lt;br /&gt;    RETURN&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown below&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example: File: SpOutputValueDataReaderForm.cs&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;&lt;br /&gt;// Create the connection.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;&lt;br /&gt;// Create the command.&lt;br /&gt;SqlCommand cmd = new SqlCommand("SP0209_OutputValueWithDataReader", conn);&lt;br /&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;br /&gt;&lt;br /&gt;// Define the input parameter for the command.&lt;br /&gt;cmd.Parameters.Add("@ValueIn", SqlDbType.Int);&lt;br /&gt;// Set the input parameter value.&lt;br /&gt;cmd.Parameters[0].Value = Convert.ToInt32(outputValueTextBox.Text);&lt;br /&gt;&lt;br /&gt;// Define the output parameter for the command.&lt;br /&gt;SqlParameter outParam = cmd.Parameters.Add("@ValueOut", SqlDbType.Int);&lt;br /&gt;outParam.Direction = ParameterDirection.Output;&lt;br /&gt;&lt;br /&gt;result.Append("Before execution, output value = " + outParam.Value +&lt;br /&gt;    Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Open the connection and create the DataReader.&lt;br /&gt;conn.Open( );&lt;br /&gt;SqlDataReader dr = cmd.ExecuteReader( );&lt;br /&gt;&lt;br /&gt;result.Append("After execution, output value = " + outParam.Value +&lt;br /&gt;    Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Iterate over the records for the DataReader.&lt;br /&gt;int rowCount = 0;&lt;br /&gt;while (dr.Read( ))&lt;br /&gt;{&lt;br /&gt;    rowCount++;&lt;br /&gt;&lt;br /&gt;    //  . . .  Code to process result set in DataReader&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;result.Append("After reading all " + rowCount + " rows, output value = " +&lt;br /&gt;    outParam.Value + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Close the DataReader.&lt;br /&gt;dr.Close( );&lt;br /&gt;result.Append("After DataReader.Close( ), output value = " +&lt;br /&gt;    outParam.Value + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Close the connection.&lt;br /&gt;conn.Close( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Output parameters allow a stored procedure to pass a data value or cursor variable back to the caller. To use an output parameter with a &lt;tt&gt;DataReader&lt;/tt&gt;, add the output parameter to the &lt;tt&gt;ParameterCollection&lt;/tt&gt; for the &lt;tt&gt;Command&lt;/tt&gt; object used to create the &lt;tt&gt;DataReader&lt;/tt&gt;. Specify the &lt;tt&gt;ParameterDirection&lt;/tt&gt; property of the &lt;tt&gt;Parameter&lt;/tt&gt; as &lt;tt&gt;Output&lt;/tt&gt; or &lt;tt&gt;InputOutput&lt;/tt&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5053249489691429756?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5053249489691429756/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/returning-output-parameter-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5053249489691429756'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5053249489691429756'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/returning-output-parameter-using.html' title='Returning an Output Parameter Using DataReader'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-7029106190149656675</id><published>2008-10-16T13:35:00.001+05:30</published><updated>2008-10-16T13:35:21.782+05:30</updated><title type='text'>Mapping .NET Data Provider Data Types to .NET Framework Data Types</title><content type='html'>&lt;p&gt;To convert between .NET provider data types and .NET Framework data types.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The ADO.NET &lt;tt&gt;DataSet&lt;/tt&gt; and contained objects are data source independent. The &lt;tt&gt;DataAdapter&lt;/tt&gt; is used to retrieve data into the &lt;tt&gt;DataSet&lt;/tt&gt; and to reconcile modifications made to the data to the data source at some later time. The implication is that data in the &lt;tt&gt;DataTable&lt;/tt&gt; objects contained in the &lt;tt&gt;DataSet&lt;/tt&gt; are .NET Framework data types rather than data types specific to the underlying data source or the .NET data provider used to connect to that data source.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;While the &lt;tt&gt;DataReader&lt;/tt&gt; object for a data source is specific to the .NET data provider used to retrieve the data, the values in the &lt;tt&gt;DataReader&lt;/tt&gt; are stored in variables with .NET Framework data types.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The .NET Framework data type is inferred from the .NET data provider used to fill the &lt;tt&gt;DataSet&lt;/tt&gt; or build the &lt;tt&gt;DataReader&lt;/tt&gt;. The &lt;tt&gt;DataReader&lt;/tt&gt; has typed accessor methods that improve performance by returning a value as a specific .NET Framework data type when the data type is known, thereby eliminating the need for additional type conversion.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Some &lt;tt&gt;DataReader&lt;/tt&gt; classes expose data source specific accessor methods as well. For example, the &lt;tt&gt;SqlDataReader&lt;/tt&gt; exposes accessor methods that return SQL Server data types as objects of &lt;tt&gt;System.Data.SqlType&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The following example shows how to cast a value from a &lt;tt&gt;DataReader&lt;/tt&gt; to a .NET Framework data type and how to use the .NET Framework typed accessor and the SQL Server-specific typed accessor:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Create the connection and the command.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;SqlCommand cmd = new SqlCommand(&lt;br /&gt;    "SELECT CategoryID, CategoryName FROM Categories", conn);&lt;br /&gt;&lt;br /&gt;// Open the connection and build the DataReader.&lt;br /&gt;conn.Open( );&lt;br /&gt;SqlDataReader dr = cmd.ExecuteReader( );&lt;br /&gt;&lt;br /&gt;// Get the CategoryID from the DataReader and cast to int.&lt;br /&gt;int categoryId = Convert.ToInt32(dr[0]);&lt;br /&gt;&lt;br /&gt;// Get the CategoryID using typed accessor.&lt;br /&gt;int taCategoryId = dr.GetInt32(0);&lt;br /&gt;&lt;br /&gt;// Get the CategoryID using the SQL Server-specific accessor.&lt;br /&gt;System.Data.SqlTypes.SqlInt32 sqlCategoryId = dr.GetSqlInt32(0);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;In all cases, a null value for a .NET Framework data type is represented by &lt;tt&gt;System.DBNull.Value&lt;/tt&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-7029106190149656675?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/7029106190149656675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/mapping-net-data-provider-data-types-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7029106190149656675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7029106190149656675'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/mapping-net-data-provider-data-types-to.html' title='Mapping .NET Data Provider Data Types to .NET Framework Data Types'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5498787527283517025</id><published>2008-10-10T00:51:00.002+05:30</published><updated>2008-10-10T01:03:42.936+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ADO.NET Counting Records'/><title type='text'>Counting Records in DataReader</title><content type='html'>To determine how many records there are in a &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;br /&gt;&lt;div class="docText"&gt;Use one of the following three techniques:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;Iterate over the rows in the &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;Issue a &lt;tt&gt;COUNT(*)&lt;/tt&gt; query as part of a batch query. Note that not all data sources support batch queries. If not, execute the statements separately one after the other for a similar result.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;Use the &lt;tt&gt;@@ROWCOUNT&lt;/tt&gt; function to return the number or rows in a &lt;tt&gt;DataReader&lt;/tt&gt; after the &lt;tt&gt;DataReader&lt;/tt&gt; has been closed. This technique is SQL Server specific.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;The sample code uses a single stored procedure:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;i&gt;SP0207_GetOrders&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Returns a result set containing all records in the Orders table in Northwind. Also, the stored procedure returns the &lt;tt&gt;@@ROWCOUNT&lt;/tt&gt; value for the query in an output parameter. The stored procedure is shown in the below example here.&lt;/div&gt;&lt;div class="docList"&gt;&lt;/div&gt;&lt;/dd&gt; &lt;dd&gt;Example: Stored procedure: SP0207_GetOrders &lt;/dd&gt; &lt;/dl&gt;&lt;pre&gt;ALTER PROCEDURE SP0207_GetOrders&lt;br /&gt;@RowCount int output&lt;br /&gt;AS&lt;br /&gt;set nocount on&lt;br /&gt;&lt;br /&gt;select * from Orders&lt;br /&gt;&lt;br /&gt;set @RowCount = @@ROWCOUNT&lt;br /&gt;&lt;br /&gt;RETURN&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The C# code is shown below:&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example. File: DataReaderRowCountForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;// Batch query to retrieve the COUNT of records and&lt;br /&gt;// all of the records in the Orders table as two result sets.&lt;br /&gt;String sqlText = "SELECT COUNT(*) FROM Orders; " +&lt;br /&gt;"SELECT * FROM Orders;";&lt;br /&gt;&lt;br /&gt;// Create the connection.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;SqlCommand cmd = new SqlCommand(sqlText, conn);&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;// Create a DataReader on the first result set.&lt;br /&gt;SqlDataReader dr = cmd.ExecuteReader( );&lt;br /&gt;// Get the count of records from the select count(*) statement.&lt;br /&gt;dr.Read( );&lt;br /&gt;resultTextBox.Text = "Orders table record count, using COUNT(*)= " +&lt;br /&gt;dr.GetInt32(0) + Environment.NewLine;&lt;br /&gt;&lt;br /&gt;// Move to the data result set.&lt;br /&gt;dr.NextResult( );&lt;br /&gt;int count = 0;&lt;br /&gt;// Iterate over the records in the DataReader.&lt;br /&gt;while(dr.Read( ))&lt;br /&gt;{&lt;br /&gt;count++;&lt;br /&gt;&lt;br /&gt;//  . . .  Do something interesting with the data here.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Close the DataReader and the connection.&lt;br /&gt;dr.Close( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text += "Orders table record count, " +&lt;br /&gt;"iterating over result set = " + count +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;&lt;br /&gt;// Create the stored procedure to use in the DataReader.&lt;br /&gt;cmd = new SqlCommand("SP0207_GetOrders", conn);&lt;br /&gt;cmd.CommandType = CommandType.StoredProcedure;&lt;br /&gt;// Create the output paramter to return @@ROWCOUNT.&lt;br /&gt;cmd.Parameters.Add("@RowCount", SqlDbType.Int).Direction =&lt;br /&gt;ParameterDirection.Output;&lt;br /&gt;&lt;br /&gt;// Create a DataReader for the result set returned by&lt;br /&gt;// the stored procedure.&lt;br /&gt;dr = cmd.ExecuteReader( );&lt;br /&gt;&lt;br /&gt;//  . . .  Process the data in the DataReader.&lt;br /&gt;&lt;br /&gt;// Close the DataReader.&lt;br /&gt;dr.Close( );&lt;br /&gt;// The output parameter containing the row count is now available.&lt;br /&gt;&lt;br /&gt;resultTextBox.Text += "Orders table record count, " +&lt;br /&gt;"returning @@ROWCOUNT from stored procedure = " + cmd.Parameters["@RowCount"].Value;&lt;br /&gt;&lt;br /&gt;conn.Close( );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;The &lt;tt&gt;DataReader&lt;/tt&gt; provides forward-only, read-only access to a stream of rows from a data source. It is optimized for performance by reading data directly from a connection to a data source. As a result, there is no way to determine the number of records in the result set for the &lt;tt&gt;DataReader&lt;/tt&gt; without iterating through all of the records. Additionally, because the &lt;tt&gt;DataReader&lt;/tt&gt; is forward-only, you cannot move backwards in &lt;tt&gt;DataReader&lt;/tt&gt; so when iterating, you must process data at the same time. This technique provides the only accurate count of records in the &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;A second technique demonstrated in the solution counts the records using the &lt;tt&gt;COUNT&lt;/tt&gt; aggregate function for the command text used to build the &lt;tt&gt;DataReader&lt;/tt&gt;. This technique can have discrepancies with the number of records actually in the &lt;tt&gt;DataReader&lt;/tt&gt; because of the timing lag between issuing the &lt;tt&gt;COUNT&lt;/tt&gt; function and creating the &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The solution also demonstrates using the SQL Server &lt;tt&gt;@@ROWCOUNT&lt;/tt&gt; variable, which returns the number of rows affected by the previous statement, to return the number of records in the result set used to create the &lt;tt&gt;DataReader&lt;/tt&gt;. The count is returned as an output parameter and is therefore not available until the &lt;tt&gt;DataReader&lt;/tt&gt; is closed. Although this does not improve the availability of the record count, centralizing the count in the stored procedure is less prone to coding errors than the counting approach.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;HasRows( )&lt;/tt&gt; method of the &lt;tt&gt;DataReader&lt;/tt&gt; was introduced in Version 1.1 of the .NET Framework. It returns a Boolean value indicating whether the &lt;tt&gt;DataReader&lt;/tt&gt; contains at least one row.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;There is also no way to determine the number of result sets in a &lt;tt&gt;DataReader&lt;/tt&gt; built using a batch query without iterating over the result sets using the &lt;tt&gt;NextResult( )&lt;/tt&gt; method.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5498787527283517025?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5498787527283517025/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/counting-records-in-datareader.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5498787527283517025'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5498787527283517025'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/counting-records-in-datareader.html' title='Counting Records in DataReader'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-2068943405592785133</id><published>2008-10-10T00:28:00.002+05:30</published><updated>2008-10-10T00:35:45.200+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ADO.NET Deleting Rows'/><title type='text'>Accessing Deleted Rows in DataTable</title><content type='html'>&lt;div class="docText"&gt;Use either a &lt;tt&gt;DataView&lt;/tt&gt; or the &lt;tt&gt;Select( )&lt;/tt&gt; method of the &lt;tt&gt;DataTable&lt;/tt&gt; to access deleted rows.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains three event handlers:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Form.Load&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Sets up the sample by creating a &lt;tt&gt;DataTable&lt;/tt&gt; containing Orders data from Northwind. A view containing the &lt;tt&gt;Current&lt;/tt&gt; rows is bound to a data grid on the form.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Current Rows RadioButton.CheckedChanged&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Sets the view of the Orders data to display only &lt;tt&gt;Current&lt;/tt&gt; rows. The text box displaying information about deleted rows is cleared.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Deleted Rows RadioButton.CheckedChanged&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Sets the view of the Orders data to display only &lt;tt&gt;Deleted&lt;/tt&gt; rows. The &lt;tt&gt;DataTable&lt;/tt&gt; for the &lt;tt&gt;DataView&lt;/tt&gt; is retrieved and the &lt;tt&gt;Select( )&lt;/tt&gt; method is used to get the &lt;tt&gt;Deleted&lt;/tt&gt; rows. The overloaded indexer in C#, or &lt;tt&gt;Item( )&lt;/tt&gt; property in VB.NET, is used to retrieve the OrderID from the &lt;tt&gt;Original&lt;/tt&gt; version of these deleted rows. This information is displayed in the text box on the form.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown in the following example.&lt;/div&gt;&lt;div class="docText"&gt;Example: File: AccessDeletedRowsForm.cs&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;private DataView dv;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void AccessDataSetDeletedRowsForm_Load(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Fill the Orders table.&lt;br /&gt;SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders",&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;DataTable dt = new DataTable("Orders");&lt;br /&gt;da.Fill(dt);&lt;br /&gt;&lt;br /&gt;// Define a view of just the Current rows.&lt;br /&gt;dv = new DataView(dt, null, null, DataViewRowState.CurrentRows);&lt;br /&gt;dataGrid.DataSource = dv;&lt;br /&gt;&lt;br /&gt;currentRadioButton.Checked = true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void currentRadioButton_CheckedChanged(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Filter to include only current rows.&lt;br /&gt;dv.RowStateFilter = DataViewRowState.CurrentRows;&lt;br /&gt;dataGrid.ReadOnly = false;&lt;br /&gt;&lt;br /&gt;deletedTextBox.Clear( );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void deletedRadioButton_CheckedChanged(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Filter the view to include only deleted rows.&lt;br /&gt;dv.RowStateFilter = DataViewRowState.Deleted;&lt;br /&gt;dataGrid.ReadOnly = true;&lt;br /&gt;&lt;br /&gt;// Get the DataTable from the DataView.&lt;br /&gt;DataTable dt = dv.Table;&lt;br /&gt;// Filter using the DataTable RowState.&lt;br /&gt;DataRow[] delRows = dt.Select(null, null, DataViewRowState.Deleted);&lt;br /&gt;&lt;br /&gt;StringBuilder sb = new StringBuilder("Deleted Records:" +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;// Iterate over the collection of deleted rows.&lt;br /&gt;foreach(DataRow row in delRows)&lt;br /&gt;sb.Append("Order ID: " + row["OrderID",&lt;br /&gt;DataRowVersion.Original] + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;deletedTextBox.Text = sb.ToString( );&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;ADO.NET manages the state of the rows while they are being modified. Rows are assigned a state from the &lt;tt&gt;DataRowState&lt;/tt&gt; enumeration described in below table 1.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table 1. DataRowState enumeration&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Value&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Added&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The row has been added to the collection of rows in the table but &lt;tt&gt;AcceptChanges( )&lt;/tt&gt; has not been called.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Deleted&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The row has been deleted from the collection of rows in the table but &lt;tt&gt;AcceptChanges( )&lt;/tt&gt; has not been called.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Detached&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The row does not belong to the collection of rows in a &lt;tt&gt;DataTable&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Modified&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The data in the row has been changed but &lt;tt&gt;AcceptChanges( )&lt;/tt&gt; has not been called.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Unchanged&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The data in the row has not been changed since it was loaded or since &lt;tt&gt;AcceptChanges( )&lt;/tt&gt; was last called.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;When &lt;tt&gt;AcceptChanges( )&lt;/tt&gt; is called on the &lt;tt&gt;DataSet&lt;/tt&gt;, &lt;tt&gt;DataTable&lt;/tt&gt;, or &lt;tt&gt;DataRow&lt;/tt&gt;, either explicitly or implicitly by calling the &lt;tt&gt;Update( )&lt;/tt&gt; method of the &lt;tt&gt;DataAdapter&lt;/tt&gt;, the following occurs:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;All rows with a row state of &lt;tt&gt;Deleted&lt;/tt&gt; are removed.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;All other rows are assigned a row state of &lt;tt&gt;Unchanged&lt;/tt&gt; and the &lt;tt&gt;Original&lt;/tt&gt; row version values are overwritten with the &lt;tt&gt;Current&lt;/tt&gt; version values.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;When &lt;tt&gt;RejectChanges( )&lt;/tt&gt; is called on the &lt;tt&gt;DataSet&lt;/tt&gt;, &lt;tt&gt;DataTable&lt;/tt&gt;, or &lt;tt&gt;DataRow&lt;/tt&gt;, the following occurs:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;All rows with a row state of &lt;tt&gt;Added&lt;/tt&gt; are removed.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;All other rows are assigned a row state of &lt;tt&gt;Unchanged&lt;/tt&gt; and the &lt;tt&gt;Current&lt;/tt&gt; row version values are overwritten with the &lt;tt&gt;Original&lt;/tt&gt; row version values.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;Each &lt;tt&gt;DataRow&lt;/tt&gt; has a &lt;tt&gt;RowState&lt;/tt&gt; property that returns the current state of the row.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;ADO.NET maintains several versions of the data in each row while it is being modified to allow the disconnected to be later reconciled with the data source. The following table2 describes the &lt;tt&gt;DataRowVersion&lt;/tt&gt; enumeration values.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table 2. DataRowVersion enumeration&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Value&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Current&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Current value. This version does not exist for rows with a state of &lt;tt&gt;Deleted&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Default&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Default value as determined by the &lt;tt&gt;DataRowState&lt;/tt&gt;:&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version for rows with &lt;tt&gt;Added&lt;/tt&gt;, &lt;tt&gt;Modified&lt;/tt&gt;, or &lt;tt&gt;Unchanged&lt;/tt&gt; state&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;The &lt;tt&gt;Original&lt;/tt&gt; version for rows with &lt;tt&gt;Deleted&lt;/tt&gt; state&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="docList"&gt;The &lt;tt&gt;Proposed&lt;/tt&gt; value for rows with &lt;tt&gt;Detached&lt;/tt&gt; state&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Original&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Original value. This version does not exist for rows with a state of &lt;tt&gt;Added&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Proposed&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Proposed value. This value exists during a row edit operation started either implicitly or explicitly with the &lt;tt&gt;BeginEdit( )&lt;/tt&gt; method and for &lt;tt&gt;Detached&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;HasVersion( )&lt;/tt&gt; method of the &lt;tt&gt;DataRow&lt;/tt&gt; object checks whether a particular row version exists.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;DataViewRowState&lt;/tt&gt; enumeration is used to retrieve a particular version of data or to determine whether a version exists. It is used for this purpose by both the &lt;tt&gt;Select( )&lt;/tt&gt; method of the &lt;tt&gt;DataTable&lt;/tt&gt; and by the &lt;tt&gt;RowStateFilter&lt;/tt&gt; property of the &lt;tt&gt;DataView&lt;/tt&gt;. You can retrieve more than one version by using a Boolean &lt;tt&gt;OR&lt;/tt&gt; of &lt;tt&gt;DataViewRowState&lt;/tt&gt; values. Table3 describes the &lt;tt&gt;DataViewRowState&lt;/tt&gt; enumeration values.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table 3. DataViewRowState enumeration&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Value&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Added&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version of all &lt;tt&gt;Added&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;CurrentRows&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version of all &lt;tt&gt;Unchanged&lt;/tt&gt;, &lt;tt&gt;Added&lt;/tt&gt;, and &lt;tt&gt;Modified&lt;/tt&gt; rows. This is the default value.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Deleted&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Original&lt;/tt&gt; version of all &lt;tt&gt;Deleted&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;ModifiedCurrent&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version of all &lt;tt&gt;Modified&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;ModifiedOriginal&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Original&lt;/tt&gt; version of all &lt;tt&gt;Modified&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;None&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;No rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;OriginalRows&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Original&lt;/tt&gt; version of &lt;tt&gt;Unchanged&lt;/tt&gt;, &lt;tt&gt;Modified&lt;/tt&gt;, and &lt;tt&gt;Deleted&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Unchanged&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version of all &lt;tt&gt;Unchanged&lt;/tt&gt; rows.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Current&lt;/tt&gt; version of each row is retrieved by default when accessing rows in a &lt;tt&gt;DataTable&lt;/tt&gt; or in a &lt;tt&gt;DataView&lt;/tt&gt;. The solution demonstrates an approach for getting &lt;tt&gt;Deleted&lt;/tt&gt; rows from both a &lt;tt&gt;DataTable&lt;/tt&gt; and a &lt;tt&gt;DataView&lt;/tt&gt;. &lt;tt&gt;Deleted&lt;/tt&gt; rows include only those marked for deletion using the &lt;tt&gt;Delete( )&lt;/tt&gt; method of the &lt;tt&gt;DataRow&lt;/tt&gt; or the &lt;tt&gt;DataView&lt;/tt&gt;, not the &lt;tt&gt;Remove( )&lt;/tt&gt; or &lt;tt&gt;RemoveAt( )&lt;/tt&gt; method of the &lt;tt&gt;DataRowCollection&lt;/tt&gt;, which instead immediately removes the specified &lt;tt&gt;DataRow&lt;/tt&gt; from the collection.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The solution demonstrates two techniques for retrieving the deleted rows:&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;To get the &lt;tt&gt;Deleted&lt;/tt&gt; rows from the &lt;tt&gt;DataTable&lt;/tt&gt;, use an overload of the &lt;tt&gt;Select( )&lt;/tt&gt; method of the &lt;tt&gt;DataTable&lt;/tt&gt; to return an array of deleted &lt;tt&gt;DataRow&lt;/tt&gt; objects. The overload accepts an argument having a &lt;tt&gt;DataViewRowState&lt;/tt&gt; enumeration value. To retrieve deleted rows, pass a value of &lt;tt&gt;Deleted&lt;/tt&gt; as the argument.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;To get the &lt;tt&gt;Deleted&lt;/tt&gt; rows from the &lt;tt&gt;DataView&lt;/tt&gt;, set the &lt;tt&gt;RowStateFilter&lt;/tt&gt; property of the &lt;tt&gt;DataView&lt;/tt&gt; to &lt;tt&gt;Deleted&lt;/tt&gt;. Deleted rows are also visible, along with other rows, when you set the &lt;tt&gt;RowStateFilter&lt;/tt&gt; property to &lt;tt&gt;ModifiedOriginal&lt;/tt&gt; and &lt;tt&gt;OriginalRows&lt;/tt&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-2068943405592785133?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/2068943405592785133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/accessing-deleted-rows-in-datatable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2068943405592785133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2068943405592785133'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/accessing-deleted-rows-in-datatable.html' title='Accessing Deleted Rows in DataTable'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8177985607894449328</id><published>2008-10-10T00:12:00.001+05:30</published><updated>2008-10-10T00:12:28.569+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='ADO.NET Web Service'/><title type='text'>Using Web Service as Data Source</title><content type='html'>&lt;p class="docText"&gt;Create a web service that returns a &lt;tt&gt;DataSet&lt;/tt&gt; to a client, and then invoke the web service from the client to retrieve the &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The XML web service code contains one method:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;LoadOrders( )&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Creates a &lt;tt&gt;DataSet&lt;/tt&gt; containing all Orders and Order Details data from Northwind. A &lt;tt&gt;DataRelation&lt;/tt&gt; is created relating the tables. The &lt;tt&gt;DataSet&lt;/tt&gt; is returned by the method.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;p class="docText"&gt;The client-side code instantiates the web service class and calls the &lt;tt&gt;LoadOrders( )&lt;/tt&gt; method to create a &lt;tt&gt;DataSet&lt;/tt&gt; containing the Orders and Order Details data from Northwind. The default view of the Orders table is bound to a data grid on the form.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code for the XML web service is shown in the following example:&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Example 2-4. File: NorthwindServiceCS.asmx.cs&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;using System.Web.Services;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;        &lt;br /&gt;public const String ORDERS_TABLE = "Orders";&lt;br /&gt;public const String ORDERDETAILS_TABLE = "OrderDetails";&lt;br /&gt;&lt;br /&gt;public const String ORDERID_FIELD = "OrderID";&lt;br /&gt;&lt;br /&gt;public const String ORDERS_ORDERDETAILS_RELATION =&lt;br /&gt;    "Order_OrderDetails_Relation";&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;[WebMethod]&lt;br /&gt;public DataSet LoadOrders( )&lt;br /&gt;{&lt;br /&gt;    DataSet ds = new DataSet( );&lt;br /&gt;    &lt;br /&gt;    SqlDataAdapter da;&lt;br /&gt;&lt;br /&gt;    // Fill the Order table and add it to the DataSet.&lt;br /&gt;    da = new SqlDataAdapter("SELECT * FROM Orders",&lt;br /&gt;        ConfigurationSettings.AppSettings["DataConnectString"]);&lt;br /&gt;    DataTable orderTable = new DataTable(ORDERS_TABLE);&lt;br /&gt;    da.FillSchema(orderTable, SchemaType.Source);&lt;br /&gt;    da.Fill(orderTable);&lt;br /&gt;    ds.Tables.Add(orderTable);&lt;br /&gt;&lt;br /&gt;    // Fill the OrderDetails table and add it to the DataSet.&lt;br /&gt;    da = new SqlDataAdapter("SELECT * FROM [Order Details]",&lt;br /&gt;        ConfigurationSettings.AppSettings["DataConnectString"]);&lt;br /&gt;    DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE);&lt;br /&gt;    da.FillSchema(orderDetailTable, SchemaType.Source);&lt;br /&gt;    da.Fill(orderDetailTable);&lt;br /&gt;    ds.Tables.Add(orderDetailTable);&lt;br /&gt;&lt;br /&gt;    // Create a relation between the tables.&lt;br /&gt;    ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION,&lt;br /&gt;        ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD],&lt;br /&gt;        ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD],&lt;br /&gt;        true);&lt;br /&gt;&lt;br /&gt;    return ds;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# web services client-side code is shown in the following example:&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Example: File: WebServiceDataSourceForm.cs&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;&lt;br /&gt;// Table name constants&lt;br /&gt;private const String ORDERS_TABLE= "Orders";&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;// Create the Web Service object.&lt;br /&gt;NorthwindServiceCS nws = new NorthwindServiceCS( );&lt;br /&gt;// Load the DataSet containing orders and order details.&lt;br /&gt;DataSet ds = nws.LoadOrders( );&lt;br /&gt;&lt;br /&gt;// Bind the default view of the orders table to the grid.&lt;br /&gt;dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p/&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;br /&gt;&lt;p class="docText"&gt;An XML web service is software that is accessible using Internet standards such as XML and HTTP. Because it is accessible through open-standard interfaces, web services make it easy to allow heterogeneous systems to work together.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;.NET makes it very easy to build XML web services. In .NET, web services are implemented as &lt;em&gt;.ASMX&lt;/em&gt; files beginning with a &lt;tt&gt;@WebService&lt;/tt&gt; directive. For example, the solution code contains the following directive:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;%@ WebService Language="c#" Codebehind="NorthwindServiceCS.asmx.cs"&lt;br /&gt;    Class="NorthwindServiceCS" %&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Methods in the web service class that are exposed over the Web are tagged with the &lt;tt&gt;WebMethod&lt;/tt&gt; attribute; untagged methods can be used only internally by the web service. To deploy the web service, copy it to a virtual directory that has script execute permissions on an IIS web server that has ASP.NET support.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;To use the web service class, use &lt;em&gt;wsdl.exe&lt;/em&gt; to create the client-side proxy class. For the solution, the command is:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;wsdl.exe http://localhost/NorthwindWebServiceCS/NorthwindServiceCS.asmx&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Then, as with a local class, the client is able to instantiate the web service class using the &lt;tt&gt;new&lt;/tt&gt; operator.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;For more information about creating and consuming XML web services, see the MSDN Library.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The solution shows that there is very little difference between implementing the &lt;tt&gt;LoadOrders( )&lt;/tt&gt;methods to retrieve a &lt;tt&gt;DataSet&lt;/tt&gt; containing the Orders and Order Details data from Northwind as a local class or as a web services class.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8177985607894449328?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8177985607894449328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/using-web-service-as-data-source.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8177985607894449328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8177985607894449328'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/using-web-service-as-data-source.html' title='Using Web Service as Data Source'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-6061621773298907102</id><published>2008-10-10T00:07:00.002+05:30</published><updated>2008-10-10T00:09:24.125+05:30</updated><title type='text'>Processing a Batch SQL Statement</title><content type='html'>&lt;div class="docText"&gt;Use the &lt;tt&gt;NextResult( )&lt;/tt&gt; method to iterate through and process SQL queries that return multiple result sets.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains three event handlers:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Go Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Defines a SQL batch query statement that selects all Orders and Order Details records from Northwind. Depending on the radio button checked by the user, either a &lt;tt&gt;DataAdapter&lt;/tt&gt; is used to fill a &lt;tt&gt;DataSet&lt;/tt&gt; with multiple tables or a &lt;tt&gt;Command&lt;/tt&gt; object is used to create a &lt;tt&gt;DataReader&lt;/tt&gt; containing multiple result sets. In either case the results are displayed in a data grid for the &lt;tt&gt;DataSet&lt;/tt&gt; and in a text box for the &lt;tt&gt;DataReader&lt;/tt&gt;.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;DataSet RadioButton.CheckedChanged&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Displays a data grid to show the results of a batch query when loaded into a &lt;tt&gt;DataSet&lt;/tt&gt;. Hides the text box for the &lt;tt&gt;DataReader&lt;/tt&gt; results.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;DataReader RadioButton.CheckedChanged&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Displays a text box to show the results of a batch query when loaded into a &lt;tt&gt;DataReader&lt;/tt&gt;. Hides the data grid for the &lt;tt&gt;DataSet&lt;/tt&gt; results.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following example shows it.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;b&gt;Example:&lt;/b&gt; File: BatchSqlStatementForm.cs&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;// Table name constants&lt;br /&gt;private const String ORDERS_TABLE        = "Orders";&lt;br /&gt;private const String ORDERDETAILS_TABLE  = "OrderDetails";&lt;br /&gt;&lt;br /&gt;// Field name constants&lt;br /&gt;private const String ORDERID_FIELD       = "OrderID";&lt;br /&gt;&lt;br /&gt;// Relation name constants&lt;br /&gt;private const String ORDERS_ORDERDETAILS_RELATION =&lt;br /&gt;"Orders_OrderDetails_Relation";&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void goButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Batch SQL query returning two result sets&lt;br /&gt;String sqlText = "select OrderID, CustomerID, EmployeeID, OrderDate," +&lt;br /&gt;"RequiredDate, ShippedDate, ShipVia, Freight, ShipName, " +&lt;br /&gt;"ShipAddress, ShipCity, ShipRegion, ShipPostalCode, " +&lt;br /&gt;"ShipCountry " +&lt;br /&gt;"FROM Orders;" +&lt;br /&gt;"SELECT OrderID, ProductID, UnitPrice, Quantity, Discount " +&lt;br /&gt;"FROM [Order Details];";&lt;br /&gt;&lt;br /&gt;if (dataSetRadioButton.Checked)&lt;br /&gt;{&lt;br /&gt;SqlDataAdapter da = new SqlDataAdapter(sqlText,&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;&lt;br /&gt;// Map the automatically generated table names Table and Table1.&lt;br /&gt;da.TableMappings.Add("Table", ORDERS_TABLE);&lt;br /&gt;da.TableMappings.Add("Table1", ORDERDETAILS_TABLE);&lt;br /&gt;&lt;br /&gt;// Fill the DataSet with the results of the batch query.&lt;br /&gt;DataSet ds = new DataSet( );&lt;br /&gt;da.Fill(ds);&lt;br /&gt;&lt;br /&gt;// Add a relation between the Order and Order Details tables.&lt;br /&gt;ds.Relations.Add(new DataRelation(ORDERS_ORDERDETAILS_RELATION,&lt;br /&gt;ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD],&lt;br /&gt;ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD],&lt;br /&gt;true));&lt;br /&gt;&lt;br /&gt;// Bind the default view of the Orders table to the grid.&lt;br /&gt;resultDataGrid.DataSource = ds.Tables[ORDERS_TABLE];&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;StringBuilder sb = new StringBuilder( );&lt;br /&gt;&lt;br /&gt;// Create a new connection and command to fill the DataReader.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;SqlCommand cmd = new SqlCommand(sqlText, conn);&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;// Execute the batch query.&lt;br /&gt;SqlDataReader dr = cmd.ExecuteReader( );&lt;br /&gt;&lt;br /&gt;// Process each result set in the DataReader.&lt;br /&gt;int nResultSet = 0;&lt;br /&gt;do&lt;br /&gt;{&lt;br /&gt;sb.Append("RESULT SET: " + (++nResultSet) +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Iterate over the rows in the DataReader.&lt;br /&gt;while(dr.Read( ))&lt;br /&gt;{&lt;br /&gt;// Output each field in the DataReader row.&lt;br /&gt;for(int i = 0; i &amp;lt; dr.FieldCount; i++)&lt;br /&gt;sb.Append(dr[i] + "; ");&lt;br /&gt;&lt;br /&gt;sb.Append(Environment.NewLine);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sb.Append(Environment.NewLine);&lt;br /&gt;} while(dr.NextResult( ));&lt;br /&gt;dr.Close( );&lt;br /&gt;conn.Close( );&lt;br /&gt;&lt;br /&gt;// Display the results.&lt;br /&gt;resultTextBox.Text = sb.ToString( );&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void dataSetRadioButton_CheckedChanged(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Display the data grid for DataSet results.&lt;br /&gt;resultDataGrid.Visible = true;&lt;br /&gt;resultTextBox.Visible = false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void dataReaderRadioButton_CheckedChanged(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Display the text box for DataReader results.&lt;br /&gt;resultDataGrid.Visible = false;&lt;br /&gt;resultTextBox.Visible = true;        &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;A batch command is defined as a collection of SQL statements separated by semicolons. The batch command can fill a &lt;tt&gt;DataSet&lt;/tt&gt; or build a &lt;tt&gt;DataReader&lt;/tt&gt;. Working with the results is different for each of these scenarios as described in the following sections.&lt;br /&gt;&lt;br /&gt;The batch statement can also be contained in a stored procedure. Everything is the same as for the example where the SQL batch command is defined in the code once the &lt;tt&gt;Command&lt;/tt&gt; is executed.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;DataSet&lt;/h5&gt;&lt;pre&gt;The &lt;tt&gt;Fill( )&lt;/tt&gt; method of the &lt;tt&gt;DataAdapter&lt;/tt&gt; adds multiple result sets from a batch query to a &lt;tt&gt;DataSet&lt;/tt&gt;. One table is created in the &lt;tt&gt;DataSet&lt;/tt&gt; for each result set. By default, these tables will be named &lt;tt&gt;&lt;i&gt;Table&lt;/i&gt;&lt;/tt&gt;, &lt;tt&gt;&lt;i&gt;Table1&lt;/i&gt;&lt;/tt&gt;, &lt;tt&gt;&lt;i&gt;Table2&lt;/i&gt;&lt;/tt&gt;, and so on. You can make these names more meaningful by specifying table mappings in the &lt;tt&gt;TableMappings&lt;/tt&gt; collection of the &lt;tt&gt;DataAdapter&lt;/tt&gt;. Data relationships between the tables added with a batch query must be created programmatically. As with non-batch queries, you can define the relations and foreign key constraints for the tables prior to filling them with the results of the batch query.&lt;br /&gt;&lt;br /&gt;When using the &lt;tt&gt;Fill( )&lt;/tt&gt; method of the &lt;tt&gt;DataAdapter&lt;/tt&gt; with a batch fill operation, if one of the result sets contains an error, all subsequent processing is skipped and result sets are not added to the &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;When using the &lt;tt&gt;FillSchema( )&lt;/tt&gt; method of the &lt;tt&gt;DataAdapter&lt;/tt&gt; with a batch query and the OLE DB data provider, the schema is returned for only the first query. To retrieve the schema for all result sets, use the &lt;tt&gt;Fill( )&lt;/tt&gt; method with the &lt;tt&gt;MissingSchemaAction&lt;/tt&gt; argument set to &lt;tt&gt;AddWithKey&lt;/tt&gt;.&lt;br /&gt;&lt;/pre&gt;&lt;h5 class="docSection3Title"&gt;DataReader&lt;/h5&gt;&lt;pre&gt;As with a single statement command, a batch command is used to build a &lt;tt&gt;DataReader&lt;/tt&gt; by calling the &lt;tt&gt;ExecuteReader( )&lt;/tt&gt; method of the &lt;tt&gt;Command&lt;/tt&gt; object. The &lt;tt&gt;NextResult( )&lt;/tt&gt; method of the &lt;tt&gt;DataReader&lt;/tt&gt; is used to advance to the next result set where the method returns &lt;tt&gt;true&lt;/tt&gt; if there is another result set. Iterating over the &lt;tt&gt;DataReader&lt;/tt&gt; is demonstrated in the sample code using the following technique:&lt;br /&gt;&lt;/pre&gt;&lt;pre&gt;do {&lt;br /&gt;&lt;br /&gt; . .  process the result set        &lt;br /&gt;&lt;br /&gt;} while(dr.NextResult( ));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;Initially, the &lt;tt&gt;DataReader&lt;/tt&gt; is positioned on the first result set. Once &lt;tt&gt;NextResult( )&lt;/tt&gt; is called there is no way to return to the previous result set.&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-6061621773298907102?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/6061621773298907102/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/processing-batch-sql-statement.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/6061621773298907102'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/6061621773298907102'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/10/processing-batch-sql-statement.html' title='Processing a Batch SQL Statement'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-308410808430532258</id><published>2008-09-27T16:44:00.002+05:30</published><updated>2008-09-27T16:47:12.725+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Creating DataSet'/><title type='text'>Creating a Strongly Typed DataSet</title><content type='html'>&lt;p class="docText"&gt;A strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; is a collection of classes that inherit from and extend the &lt;tt&gt;DataSet&lt;/tt&gt;, &lt;tt&gt;DataTable&lt;/tt&gt;, and &lt;tt&gt;DataRow&lt;/tt&gt; classes, and provide additional properties, methods, and events based on the &lt;tt&gt;DataSet&lt;/tt&gt; schema. You can use all of the functionality in classes from which the strongly typed classes inherit in the same way as with untyped classes.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;A strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; class contains, in addition to a single class extending the &lt;tt&gt;DataSet&lt;/tt&gt; class, three classes for each table in the &lt;tt&gt;DataSet&lt;/tt&gt; extending each of the &lt;tt&gt;DataTable&lt;/tt&gt;, &lt;tt&gt;DataRow&lt;/tt&gt;, and &lt;tt&gt;DataRowChangeEvent&lt;/tt&gt; classes. This recipe describes these classes and discusses their commonly used methods and properties.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;There is a class named &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;DataTable&lt;/tt&gt; for each table in the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;. It has the base class &lt;tt&gt;DataTable. Below Table&lt;/tt&gt; lists commonly used methods of this class specific to the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docTableTitle"&gt;Table 1. TableNameDataTable methods&lt;/h5&gt;&lt;br /&gt;&lt;table cellpadding="4" style="margin-left:10px;" width="90%" border="1" cellspacing="0" rules="all"&gt;&lt;colgroup span="2"/&gt;&lt;thead&gt;&lt;tr&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Method&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Description&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Add&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Adds a row to the table. The method has two overloads: one takes a &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; object as the argument, while the other takes a set of arguments containing the column values.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;FindBy&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;PrimaryKeyField1&lt;/em&gt;&lt;/tt&gt; ... &lt;tt&gt;&lt;em&gt;PrimaryKeyFieldN&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Takes &lt;tt&gt;&lt;em&gt;N&lt;/em&gt;&lt;/tt&gt; arguments which are the values of the primary key fields of the row to find. Returns a &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; object, if found.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;New&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Takes no arguments and returns a new &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; object with the same schema as the table to be used for adding new rows to the table in the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;p class="docText"&gt;There is a class named &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; for each table in the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;. It has the base class &lt;tt&gt;DataRow&lt;/tt&gt; and represents a row of data in the below table which lists commonly used properties and methods of this class specific to the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docTableTitle"&gt;Table 2. TableNameRow class properties and methods&lt;/h5&gt;&lt;br /&gt;&lt;table cellpadding="4" style="margin-left:10px;" width="90%" border="1" cellspacing="0" rules="all"&gt;&lt;colgroup span="2"/&gt;&lt;thead&gt;&lt;tr&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Property/method&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Description&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Typed Accessor&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Sets and get the value of a column. The typed accessor is exposed as a property having the same name as the underlying data column.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Is&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;ColumnName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Null( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Returns a Boolean value indicating whether the field contains a null value.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Set&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;ColumnName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Null( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Sets the value of the underlying field to a null value.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Get&lt;/tt&gt;&lt;tt&gt;&lt;em&gt;ChildTableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Rows( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Returns the rows for the table as an array of &lt;tt&gt;&lt;em&gt;ChildTableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; objects.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;&lt;em&gt;ParentTableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row( )&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Returns the parent row as an object of type &lt;tt&gt;&lt;em&gt;ParentTableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;p class="docText"&gt;There is a class named &lt;tt&gt;&lt;em&gt;TableNameRow&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;ChangeEvent&lt;/tt&gt; for each table in the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;. It has the base class &lt;tt&gt;EventArgs. Table 3 d&lt;/tt&gt;escribes the properties of this class.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docTableTitle"&gt;Table 3. TableNameRowChangeEvent properties&lt;/h5&gt;&lt;br /&gt;&lt;table cellpadding="4" width="90%" border="1" style="margin-left:10px;" cellspacing="0" rules="all"&gt;&lt;colgroup span="2"/&gt;&lt;thead&gt;&lt;tr&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Property&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;p class="docText"&gt;Description&lt;/p&gt;&lt;br /&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Action&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;A value from the &lt;tt&gt;System.Data.DataRowAction&lt;/tt&gt; enumeration that describes the action performed on a row that caused the event to be raised.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;&lt;tt&gt;Row&lt;/tt&gt;&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;&lt;em&gt;TableName&lt;/em&gt;&lt;/tt&gt;&lt;tt&gt;Row&lt;/tt&gt; object for which the event was raised.&lt;/p&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;p class="docText"&gt;A strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; has some advantages over using an untyped &lt;tt&gt;DataSet&lt;/tt&gt;:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;The schema information is contained within the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; resulting in a performance over retrieving schema information at runtime. The schema of an untyped &lt;tt&gt;DataSet&lt;/tt&gt; can also be defined programmatically, as &lt;a href="http://adodotnetslackers.blogspot.com/2008/09/retrieving-hierarchical-data-into.html"&gt;discussed here&lt;/a&gt;, resulting in similar performance.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Programming is more intuitive and code is easier to maintain. Table, column, and other object names are accessed through properties having names based on the underlying data source object names rather than by using index or delimited string arguments. The Visual Studio .NET IDE provides autocomplete functionality for strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; names.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Type mismatch errors and errors resulting from misspelled or out of bounds arguments used with &lt;tt&gt;DataSet&lt;/tt&gt; objects can be detected during compilation, rather than at runtime.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p class="docText"&gt;The disadvantages of a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; object include:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Additional overhead when executing. If strongly typed functionality is not required, performance is better with an untyped &lt;tt&gt;DataSet&lt;/tt&gt; rather than with a typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;A strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; must be regenerated when the structure of the underlying data source changes. Applications using these strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; objects will need to be rebuilt with a reference to the new strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;. With an untyped &lt;tt&gt;DataSet&lt;/tt&gt;, as long as the new schema is a superset of the old schema, existing clients can simply ignore any new data and do not need to be recompiled.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p class="docText"&gt;Four ways to generate a typed &lt;tt&gt;DataSet&lt;/tt&gt; class are described in the following subsections.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Using the Visual Studio .NET IDE to generate a typed DataSet&lt;/h5&gt;&lt;br /&gt;&lt;p class="docText"&gt;The first and easiest method uses Visual Studio .NET following these steps:&lt;/p&gt;&lt;br /&gt;&lt;ol type="1" class="docList"&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Drop a &lt;tt&gt;DataAdapter&lt;/tt&gt; object from the Data tab in the Visual Studio .NET Toolbox onto a design surface such as a form or a component. The Data Adapter Configuration Wizard will appear.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Press Next to continue to the Choose Your Data Connection dialog.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Select an existing connection or create a new one as required. Press Next to continue to the Choose a Query Type dialog.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Select the Use SQL statements radio button and press Next to continue to the Generate the SQL Statements dialog.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Press the Advanced Options... button.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Uncheck all three check boxes on the Advanced SQL Generation Options dialog and press OK.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Press the Query Builder... button.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Select only one table on the Add Table dialog and press Add. Press Close.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Check the columns from the table to include or (All Columns) and press OK.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Press Next to continue to the View Wizard Results dialog.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Press Finish to complete the wizard.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Repeat steps 1-11 for the other tables that you want to have included in the strongly typed DataSet.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Right-click on the design surface and select Generate DataSet.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Provide a name for the strongly typed DataSet, select the tables to be included, and press OK to generate the new strongly typed DataSet.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;To relate tables in the new strongly typed DataSet, open the XSD file for the new DataSet in the Solution Explorer window.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Right-click on the child table in XSD schema designer, select Add &lt;img src="mk:@MSITStore:C:/Documents and Settings/admin/Desktop/BLOG s/Oreilly.ADO.Dot.NET.Cookbook.eBook-LiB.chm::/FILES/U2192.GIF" height="13" border="0" width="25"/&gt; New Relation . . . from the shortcut menu, and complete the dialog. Repeat this step to create all required relationships.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Instances of the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; can now be created programmatically either by using the &lt;tt&gt;new&lt;/tt&gt; keyword in C# or the &lt;tt&gt;New&lt;/tt&gt; keyword in Visual Basic .NET or by dragging the &lt;tt&gt;DataSet&lt;/tt&gt; object from the Data tab in the Visual Studio .NET Toolbox onto a design surface such as a component or form.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Using the TypedDataSetGenerator class to generate a typed DataSet&lt;/h5&gt;&lt;br /&gt;&lt;p class="docText"&gt;The second technique is to derive a class from the &lt;tt&gt;TypedDataSetGenerator&lt;/tt&gt; class. The static &lt;tt&gt;Generate( )&lt;/tt&gt; method of the &lt;tt&gt;TypedDataSetGenerator&lt;/tt&gt; class is used to create a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt;. The prototype of the method is:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;public static void Generate(DataSet dataSet, CodeNamespace codeNamespace,&lt;br /&gt;ICodeGenerator codeGenerator);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The arguments of the method are:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;DataSet&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p class="docList"&gt;The &lt;tt&gt;DataSet&lt;/tt&gt; used to specify the schema for the typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;/dd&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;codeNamespace&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p class="docList"&gt;The target namespace for the typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;/dd&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;codeGenerator&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;dd&gt;&lt;p class="docList"&gt;A class capable of dynamically rendering source code in a specific language and used to create the typed &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;/dd&gt;&lt;/dl&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Using an XSD schema file to generate a typed DataSet&lt;/h5&gt;&lt;br /&gt;&lt;p class="docText"&gt;The other two methods require an XSD schema file. You can generate this file in a number of ways: using the Visual Studio .NET tools, third-party tools, or the &lt;tt&gt;DataSet&lt;/tt&gt; &lt;tt&gt;WriteXmlSchema( )&lt;/tt&gt; method. You can create a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; from the &lt;span class="docEmphasis"&gt;XSD&lt;/span&gt; schema file using Visual Studio .NET or using the XML Schema Definition Tool.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;To create a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; from the &lt;span class="docEmphasis"&gt;XSD&lt;/span&gt; schema using Visual Studio .NET, follow these steps:&lt;/p&gt;&lt;br /&gt;&lt;ol type="1" class="docList"&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Right-click on the project in the Solution Explorer window, choose Add/Existing Item... from the shortcut menu, and select the &lt;span class="docEmphasis"&gt;XSD&lt;/span&gt; file to add it to the project.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Select the XSD schema file from the Solution Explorer window and open it in the designer window.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Right-click on the designer window and select Generate DataSet.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Instances of the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; can now be created programmatically by using the &lt;tt&gt;new&lt;/tt&gt; keyword in C# or the &lt;tt&gt;New&lt;/tt&gt; keyword in Visual Basic .NET or by dragging the &lt;tt&gt;DataSet&lt;/tt&gt; object from the Data tab in the Visual Studio .NET Toolbox onto a design surface such as a component or form.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;p class="docText"&gt;The second way to create a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; from an XSD schema is to use the XML Schema Definition Tool (&lt;span class="docEmphasis"&gt;XSD.EXE&lt;/span&gt;) found in the .NET Framework SDK &lt;em&gt;bin&lt;/em&gt; directory. Follow these steps:&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Generate the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; class file from the &lt;span class="docEmphasis"&gt;XSD&lt;/span&gt; schema file by issuing the following command from the command prompt:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;xsd mySchemaFile.xsd /d /l:CS&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;/d&lt;/tt&gt; switch specifies that source code for a strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; should be created.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;/l:CS&lt;/tt&gt; switch specifies that the utility should use the C# language, which is the default if not specified. For VB.NET, use the switch &lt;tt&gt;/l:VB&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The XML Schema Definition Tool offers other options. For more information, see the .NET Framework SDK documentation or the MSDN Library.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The class file that is generated for the strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; is named using the &lt;tt&gt;DataSet&lt;/tt&gt; name in the XSD schema and has an extension corresponding to the programming language: &lt;tt&gt;&lt;em&gt;.cs&lt;/em&gt;&lt;/tt&gt; for C# and &lt;tt&gt;&lt;em&gt;.vb&lt;/em&gt;&lt;/tt&gt; for VB.NET. The strongly typed &lt;tt&gt;DataSet&lt;/tt&gt; class can now be added to a project.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-308410808430532258?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/308410808430532258/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/creating-strongly-typed-dataset.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/308410808430532258'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/308410808430532258'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/creating-strongly-typed-dataset.html' title='Creating a Strongly Typed DataSet'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-2315651269067408186</id><published>2008-09-27T16:40:00.001+05:30</published><updated>2008-09-27T16:40:32.885+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Building DataSet'/><title type='text'>Building a DataSet Programmatically</title><content type='html'>&lt;p class="docText"&gt;The following example shows how to build a complex &lt;tt&gt;DataSet&lt;/tt&gt; programmatically, including how to build and add tables, columns, primary key constraints, relations, and column mappings. Use this as a template for building your own &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code creates a &lt;tt&gt;DataSet&lt;/tt&gt;. A &lt;tt&gt;DataTable&lt;/tt&gt; object is created representing the Orders table in Northwind. Columns are added, including the auto-increment primary key, to the table. The table is added to the &lt;tt&gt;DataSet&lt;/tt&gt;. The process is repeated for a &lt;tt&gt;DataTable&lt;/tt&gt; representing the Order Details table in Northwind. A &lt;tt&gt;DataRelation&lt;/tt&gt; is created relating the two tables. Finally, the tables are filled with data from Northwind.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown below.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;File: BuildDataSetProgramaticallyForm.cs&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;// Create the DataSet.&lt;br /&gt;DataSet ds = new DataSet("MyDataSet");&lt;br /&gt;&lt;br /&gt;// Build the Orders (parent) table.&lt;br /&gt;DataTable parentTable = new DataTable("Orders");&lt;br /&gt;&lt;br /&gt;DataColumnCollection cols = parentTable.Columns;&lt;br /&gt;// Add the identity field.&lt;br /&gt;DataColumn column = cols.Add("OrderID", typeof(System.Int32));            &lt;br /&gt;column.AutoIncrement = true;&lt;br /&gt;column.AutoIncrementSeed = -1;&lt;br /&gt;column.AutoIncrementStep = -1;&lt;br /&gt;// Add the other fields.&lt;br /&gt;cols.Add("CustomerID", typeof(System.String)).MaxLength = 5;  &lt;br /&gt;cols.Add("EmployeeID", typeof(System.Int32));  &lt;br /&gt;cols.Add("OrderDate", typeof(System.DateTime));  &lt;br /&gt;cols.Add("RequiredDate", typeof(System.DateTime));  &lt;br /&gt;cols.Add("ShippedDate", typeof(System.DateTime));  &lt;br /&gt;cols.Add("ShipVia", typeof(System.Int32));  &lt;br /&gt;cols.Add("Freight", typeof(System.Decimal));  &lt;br /&gt;cols.Add("ShipName", typeof(System.String)).MaxLength = 40;  &lt;br /&gt;cols.Add("ShipAddress", typeof(System.String)).MaxLength = 60;  &lt;br /&gt;cols.Add("ShipCity", typeof(System.String)).MaxLength = 15;  &lt;br /&gt;cols.Add("ShipRegion", typeof(System.String)).MaxLength = 15;  &lt;br /&gt;cols.Add("ShipPostalCode", typeof(System.String)).MaxLength = 10;  &lt;br /&gt;cols.Add("ShipCountry", typeof(System.String)).MaxLength = 15;  &lt;br /&gt;// Set the primary key.&lt;br /&gt;parentTable.PrimaryKey = new DataColumn[] {cols["OrderID"]};&lt;br /&gt;// Add the Orders table to the DataSet.&lt;br /&gt;ds.Tables.Add(parentTable);&lt;br /&gt;&lt;br /&gt;// Build the Order Details (child) table.&lt;br /&gt;DataTable childTable = new DataTable("Order Details");&lt;br /&gt;&lt;br /&gt;cols = childTable.Columns;&lt;br /&gt;// Add the PK fields.&lt;br /&gt;cols.Add("OrderID", typeof(System.Int32)).AllowDBNull = false;  &lt;br /&gt;cols.Add("ProductID", typeof(System.Int32)).AllowDBNull = false;  &lt;br /&gt;// Add the other fields.&lt;br /&gt;cols.Add("UnitPrice", typeof(System.Decimal)).AllowDBNull = false;  &lt;br /&gt;cols.Add("Quantity", typeof(System.Int16)).AllowDBNull = false;  &lt;br /&gt;cols.Add("Discount", typeof(System.Single)).AllowDBNull = false;  &lt;br /&gt;// Set the primary key.&lt;br /&gt;childTable.PrimaryKey = new DataColumn[]&lt;br /&gt;    {&lt;br /&gt;        cols["OrderID"],&lt;br /&gt;        cols["ProductID"]&lt;br /&gt;    };&lt;br /&gt;// Add the Order Details table to the DataSet.&lt;br /&gt;ds.Tables.Add(childTable);&lt;br /&gt;&lt;br /&gt;// Add the relationship between parent and child tables.&lt;br /&gt;ds.Relations.Add("Order_OrderDetails_Relation",&lt;br /&gt;    parentTable.Columns["OrderID"], childTable.Columns["OrderID"], true);&lt;br /&gt;&lt;br /&gt;// Fill the tables from the data source.&lt;br /&gt;SqlDataAdapter da;&lt;br /&gt;String sqlText;&lt;br /&gt;&lt;br /&gt;sqlText = "SELECT OrderID, CustomerID, EmployeeID, OrderDate, " +&lt;br /&gt;    "RequiredDate, ShippedDate, ShipVia, Freight, ShipName, " +&lt;br /&gt;    "ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry " +&lt;br /&gt;    "FROM Orders";&lt;br /&gt;da = new SqlDataAdapter(sqlText,&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;da.Fill(parentTable);&lt;br /&gt;&lt;br /&gt;sqlText = "SELECT OrderID, ProductID, UnitPrice, Quantity, Discount " +&lt;br /&gt;    "FROM [Order Details]";&lt;br /&gt;da = new SqlDataAdapter(sqlText,&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;da.Fill(childTable);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p/&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;br /&gt;&lt;p class="docText"&gt;The steps to build a complex &lt;tt&gt;DataSet&lt;/tt&gt; programmatically, as shown in the code for the solution, are:&lt;/p&gt;&lt;br /&gt;&lt;ol type="1" class="docList"&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;Design the &lt;tt&gt;DataSet&lt;/tt&gt; identifying the tables, columns, indexes, constraints, and data relations that need to be created.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Create a new &lt;tt&gt;DataSet&lt;/tt&gt;, naming it in the constructor.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Create a new &lt;tt&gt;DataTable&lt;/tt&gt;, naming it in the constructor.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Add a column to the &lt;tt&gt;ColumnCollection&lt;/tt&gt; of the table using the &lt;tt&gt;Add( )&lt;/tt&gt; method exposed by the &lt;tt&gt;Columns&lt;/tt&gt; property of the &lt;tt&gt;DataTable&lt;/tt&gt; specifying the name and data type of the column. If the column is a character-type column, define its maximum length. If the column is an auto-increment column, set the &lt;tt&gt;AutoIncrement&lt;/tt&gt; property to &lt;tt&gt;true&lt;/tt&gt; and set both the &lt;tt&gt;AutoIncrementSeed&lt;/tt&gt; and &lt;tt&gt;AutoIncrementStep&lt;/tt&gt; properties of the column to &lt;tt&gt;-1&lt;/tt&gt;. Repeat step 4 for each column in the table.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Define the primary key for the table by setting the &lt;tt&gt;PrimaryKey&lt;/tt&gt; property of the &lt;tt&gt;DataTable&lt;/tt&gt; to the array of primary key &lt;tt&gt;DataColumn&lt;/tt&gt; objects.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Add the new table to the &lt;tt&gt;DataSet&lt;/tt&gt; using the &lt;tt&gt;Add( )&lt;/tt&gt; method of the &lt;tt&gt;DataTableCollection&lt;/tt&gt; exposed by the &lt;tt&gt;Tables&lt;/tt&gt; property of the &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Repeat steps 3-6 for each table in the &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Create a data relationship between two related tables in the &lt;tt&gt;DataSet&lt;/tt&gt; by using the &lt;tt&gt;Add( )&lt;/tt&gt; method of the &lt;tt&gt;DataRelationCollection&lt;/tt&gt; exposed by the &lt;tt&gt;Relations&lt;/tt&gt; property of the &lt;tt&gt;DataSet&lt;/tt&gt;. Specify the relationship name, the related columns, and whether constraints are to be created when calling the &lt;tt&gt;Add( )&lt;/tt&gt; method. Repeat step 8 for each data relationship in the &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;p class="docText"&gt;The steps continue, demonstrating how to fill the new &lt;tt&gt;DataSet&lt;/tt&gt;:&lt;/p&gt;&lt;br /&gt;&lt;ol start="9" type="1" class="docList"&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: bold"&gt;To fill the &lt;tt&gt;DataSet&lt;/tt&gt; with data from the data source, create a &lt;tt&gt;DataAdapter&lt;/tt&gt; defining the SQL select statement and the connection string in the constructor.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Use the &lt;tt&gt;Fill( )&lt;/tt&gt; method of the &lt;tt&gt;DataSet&lt;/tt&gt; to fill the table. Specify the table name to be filled in the second argument of the &lt;tt&gt;Fill( )&lt;/tt&gt; method.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;&lt;span style="FONT-WEIGHT: normal"&gt;Repeat steps 9 and 10 for each table to be filled.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-2315651269067408186?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/2315651269067408186/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/building-dataset-programmatically.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2315651269067408186'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2315651269067408186'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/building-dataset-programmatically.html' title='Building a DataSet Programmatically'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-1423910688275031797</id><published>2008-09-27T16:38:00.002+05:30</published><updated>2008-09-27T16:49:44.760+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Retrieving Hierarchical Data'/><title type='text'>Retrieving Hierarchical Data into a DataSet</title><content type='html'>&lt;div class="docText"&gt;There are several techniques you can use to load parent and child data into a &lt;tt&gt;DataSet&lt;/tt&gt;.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains three event handlers:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Form.Load&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Sets up the sample by creating a &lt;tt&gt;DataSet&lt;/tt&gt; with table schemas for both the Orders table and the Order Details table from Northwind and a &lt;tt&gt;DataRelation&lt;/tt&gt; object relating these two tables. The default view of the parent table, Orders, is bound to a data grid on the form.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Load DataSet Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Starts by clearing the data from the &lt;tt&gt;DataSet&lt;/tt&gt; and refreshing the data grid. &lt;tt&gt;DataAdapter&lt;/tt&gt; objects are created for both the parent and the child table. The Orders and Order Details are then filled using data adapters in the sequence specified and enforcing constraints during the load as specified by the user.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;The C# code is shown in the below example.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: HierarchicalDataSetForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;private DataSet ds;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void HierarchicalDataSetForm_Load(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;ds = new DataSet( );&lt;br /&gt;&lt;br /&gt;// Get the schema for the Orders table.&lt;br /&gt;DataTable parentTable = new DataTable("Orders");&lt;br /&gt;SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders",&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;da.FillSchema(parentTable, SchemaType.Source);&lt;br /&gt;ds.Tables.Add(parentTable);&lt;br /&gt;&lt;br /&gt;// Get the schema for the Order Details table.&lt;br /&gt;DataTable childTable = new DataTable("Order Details");&lt;br /&gt;da = new SqlDataAdapter("SELECT * FROM [Order Details]",&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;da.FillSchema(childTable, SchemaType.Source);&lt;br /&gt;ds.Tables.Add(childTable);&lt;br /&gt;&lt;br /&gt;// Add the relation between the tables.&lt;br /&gt;DataRelation dr = new DataRelation("Order_OrderDetails_Relation",&lt;br /&gt;parentTable.Columns["OrderID"], childTable.Columns["OrderID"]);&lt;br /&gt;ds.Relations.Add(dr);&lt;br /&gt;&lt;br /&gt;// Bind the default view of the Orders table with the grid.&lt;br /&gt;dataGrid.DataSource = parentTable.DefaultView;        &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void loadDataSetButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Remove all data from the DataSet and refresh the grid.&lt;br /&gt;ds.Clear( );&lt;br /&gt;dataGrid.Refresh( );&lt;br /&gt;&lt;br /&gt;// Create parent and child data adapters.&lt;br /&gt;SqlDataAdapter daParent = new SqlDataAdapter("SELECT * FROM Orders",&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;SqlDataAdapter daChild = new SqlDataAdapter(&lt;br /&gt;"SELECT * FROM [Order Details]",&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;&lt;br /&gt;// Enforce constraints as specified by user.&lt;br /&gt;ds.EnforceConstraints = (enforceConstraintsCheckBox.Checked);&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;if (loadParentFirstRadioButton.Checked)&lt;br /&gt;{&lt;br /&gt;// Load parent data first.&lt;br /&gt;daParent.Fill(ds, "Orders");&lt;br /&gt;daChild.Fill(ds, "Order Details");&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;// Load child data first.&lt;br /&gt;daChild.Fill(ds, "Order Details");&lt;br /&gt;daParent.Fill(ds, "Orders");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;catch (Exception ex)&lt;br /&gt;{&lt;br /&gt;MessageBox.Show(ex.Message);&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;ds.EnforceConstraints = true;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;By default, a &lt;tt&gt;DataRelation&lt;/tt&gt; is created with constraints as in the example; however, an overloaded constructor can override this behavior if necessary. If constraints are created, it is important that each record in the child table refers to a valid parent record, otherwise a &lt;tt&gt;ConstraintException&lt;/tt&gt; is raised. Two techniques can be used to load parent and related child data without error into a &lt;tt&gt;DataSet&lt;/tt&gt; with a schema that includes data relations defined:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Load data from the parent tables before loading data from the child table. This ensures that each record in the child table refers to a valid parent record.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;The &lt;tt&gt;EnforceConstraints&lt;/tt&gt; property of the &lt;tt&gt;DataSet&lt;/tt&gt; indicates whether constraint rules are followed when data is added to or modified in the &lt;tt&gt;DataSet&lt;/tt&gt;. Turn constraints off by setting the &lt;tt&gt;EnforceConstraints&lt;/tt&gt; property to &lt;tt&gt;false&lt;/tt&gt; prior to loading the data and back to &lt;tt&gt;true&lt;/tt&gt; once the data is loaded. With this approach the order in which the data is loaded is not important. If one or more constraints cannot be enforced when &lt;tt&gt;EnforceConstraints&lt;/tt&gt; is set back to true, a &lt;tt&gt;ConstraintException&lt;/tt&gt; will be raised and &lt;tt&gt;EnforceConstraints&lt;/tt&gt; stay set to &lt;tt&gt;false&lt;/tt&gt;.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-1423910688275031797?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/1423910688275031797/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/retrieving-hierarchical-data-into.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1423910688275031797'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1423910688275031797'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/retrieving-hierarchical-data-into.html' title='Retrieving Hierarchical Data into a DataSet'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8882213721288147986</id><published>2008-09-25T13:06:00.005+05:30</published><updated>2008-09-27T16:51:49.658+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting Text File in ADO.NET'/><title type='text'>Connecting to a Text File</title><content type='html'>&lt;div class="docText"&gt;Use the OLE DB Jet provider to access data in a text file.&lt;/div&gt;&lt;div class="docText"&gt;The sample code creates an &lt;tt&gt;OleDbDataAdapter&lt;/tt&gt; that uses the Jet OLE DB provider to load the contents of the text file &lt;i&gt;Categories.txt&lt;/i&gt;, shown here, into a &lt;tt&gt;DataTable&lt;/tt&gt; and displays the contents in a data grid on the form.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example-. File: Categories.txt&lt;/h5&gt;&lt;pre&gt;"CategoryID","CategoryName","Description"&lt;br /&gt;1,"Beverages","Soft drinks, coffees, teas, beers, and ales"&lt;br /&gt;2,"Condiments","Sweet and savory sauces, relishes, spreads, and seasonings"&lt;br /&gt;3,"Confections","Desserts, candies, and sweet breads"&lt;br /&gt;4,"Dairy Products","Cheeses"&lt;br /&gt;5,"Grains/Cereals","Breads, crackers, pasta, and cereal"&lt;br /&gt;6,"Meat/Poultry","Prepared meats"&lt;br /&gt;7,"Produce","Dried fruit and bean curd"&lt;br /&gt;8,"Seafood","Seaweed and fish"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: ConnectTextFileForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;// Create the data adapter to retrieve all rows from text file.&lt;br /&gt;OleDbDataAdapter da =&lt;br /&gt;new OleDbDataAdapter("SELECT * FROM [Categories.txt]",&lt;br /&gt;ConfigurationSettings.AppSettings["TextFile_0119_ConnectString"]);&lt;br /&gt;&lt;br /&gt;// Create and fill the table.&lt;br /&gt;DataTable dt = new DataTable("Categories");&lt;br /&gt;da.Fill(dt);&lt;br /&gt;&lt;br /&gt;// Bind the default view of the table to the grid.&lt;br /&gt;categoriesDataGrid.DataSource = dt.DefaultView;&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;The Jet OLE DB provider can read records from and insert records into a text file data source. The Jet database engine can access other database file formats through Indexed Sequential Access Method (ISAM) drivers specified in the &lt;tt&gt;Extended&lt;/tt&gt; &lt;tt&gt;Properties&lt;/tt&gt; attribute of the connection. Text files are supported with the &lt;tt&gt;text&lt;/tt&gt; source database type as shown in the following example:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\MyTextFileDirectory;&lt;br /&gt;Extended Properties="text;HDR=yes;FMT=Delimited";&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Extended Properties&lt;/tt&gt; attribute can, in addition to the ISAM version property, specify whether or not tables include headers as field names in the first row of a range using an &lt;tt&gt;HDR&lt;/tt&gt; attribute.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;It is not possible to define all characteristics of a text file through the connection string. You can access files that use non-standard text delimiters and fixed-width text files by creating a &lt;i&gt;schema.ini&lt;/i&gt; file in the same directory as the text file. As an example, a possible &lt;i&gt;schema.ini&lt;/i&gt; file for the &lt;i&gt;Categories.txt&lt;/i&gt; file used in this solution is:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;[Categories.txt]&lt;br /&gt;Format=CSVDelimited&lt;br /&gt;ColNameHeader=True&lt;br /&gt;MaxScanRows=0&lt;br /&gt;Character=OEM&lt;br /&gt;Col1=CategoryID Long Width 4 &lt;br /&gt;Col2=CategoryName Text Width 15&lt;br /&gt;Col3=Description Text Width 100&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;i&gt;schema.ini&lt;/i&gt; file provides the following schema information about the data in the text file:&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;br /&gt;&lt;div class="docList"&gt;Filename&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;File format&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Field names, widths, and data types&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Character set&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Special data type conversions&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;The first entry in the &lt;i&gt;schema.ini&lt;/i&gt; file is the text filename enclosed in square brackets. For example:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;[Categories.txt]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Format&lt;/tt&gt; option specifies the text file format.Below Table describes the different options.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docTableTitle"&gt;Table 1. Schema.ini format options&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Format&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;CSV Delimited&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Fields are delimited with commas:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Format=CSVDelimited&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;This is the default value.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Custom Delimited&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Fields are delimited with a custom character. You can use any single character except the double quotation mark (") as a delimiter:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Format=Delimited(customCharacter)&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Fixed Length&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Fields are fixed length:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Format=FixedLength&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;If the &lt;tt&gt;ColumnNameHeader&lt;/tt&gt; option is &lt;tt&gt;True&lt;/tt&gt;, the first line containing the column names must be comma-delimited.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Tab Delimited&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Fields are delimited with tabs:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Format=TabDelimited&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;You can specify the fields in the text file in two ways:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Include the field names in the first row of the text file and set the &lt;tt&gt;ColNameHeader&lt;/tt&gt; option to &lt;tt&gt;True&lt;/tt&gt;.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Identify each column using the format &lt;tt&gt;Col&lt;/tt&gt;&lt;tt&gt;&lt;i&gt;N&lt;/i&gt;&lt;/tt&gt; (where &lt;tt&gt;&lt;i&gt;N&lt;/i&gt;&lt;/tt&gt; is the one-based column number) and specify the name, width, and data type for each column.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;The &lt;tt&gt;MaxScanRows&lt;/tt&gt; option indicates how many rows should be scanned to automatically determine column type. A value of &lt;tt&gt;0&lt;/tt&gt; indicates that all rows should be scanned.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Col&lt;/tt&gt;&lt;tt&gt;&lt;i&gt;N&lt;/i&gt;&lt;/tt&gt; entries specify the name, width, and data type for each column. This entry is required for fixed-length formats and optional for character-delimited formats. The syntax of the &lt;tt&gt;Col&lt;/tt&gt;&lt;tt&gt;&lt;i&gt;N&lt;/i&gt;&lt;/tt&gt; entry is:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;ColN=columnName dataType [Width n]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The parameters in the entry are:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;i&gt;columnName&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The name of the column. If the column name contains spaces, it must be enclosed in double quotation marks.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;dataType&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The data type of the column. This value can be &lt;tt&gt;Bit&lt;/tt&gt;, &lt;tt&gt;Byte&lt;/tt&gt;, &lt;tt&gt;Currency&lt;/tt&gt;, &lt;tt&gt;DateTime&lt;/tt&gt;, &lt;tt&gt;Double&lt;/tt&gt;, &lt;tt&gt;Long&lt;/tt&gt;, &lt;tt&gt;Memo&lt;/tt&gt;, &lt;tt&gt;Short&lt;/tt&gt;, &lt;tt&gt;Single&lt;/tt&gt;, or &lt;tt&gt;Text&lt;/tt&gt;.&lt;/div&gt;&lt;div class="docList"&gt;&lt;tt&gt;DateTime&lt;/tt&gt; values must be in one of the following formats: &lt;tt&gt;dd-mmm-yy&lt;/tt&gt;, &lt;tt&gt;mm-dd-yy&lt;/tt&gt;, &lt;tt&gt;mmm-dd-yy&lt;/tt&gt;, &lt;tt&gt;yyyy-mm-dd&lt;/tt&gt;, or &lt;tt&gt;yyyy-mmm-dd&lt;/tt&gt;, where &lt;tt&gt;mm&lt;/tt&gt; is the month number and &lt;tt&gt;mmm&lt;/tt&gt; are the characters specifying the month.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Width n&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The literal value &lt;tt&gt;Width&lt;/tt&gt; followed by the integer value specifying the column width.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Character&lt;/tt&gt; option specifies the character set; you can set it to either &lt;tt&gt;ANSI&lt;/tt&gt; or &lt;tt&gt;OEM&lt;/tt&gt;.&lt;/div&gt;&lt;br /&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Connecting+Text+File+in+ADO.NET" rel="tag"&gt;Connecting Text File in ADO.NET&lt;/a&gt; &lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8882213721288147986?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8882213721288147986/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-text-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8882213721288147986'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8882213721288147986'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-text-file.html' title='Connecting to a Text File'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-1364978191642695154</id><published>2008-09-25T13:02:00.003+05:30</published><updated>2008-10-20T17:51:13.891+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Changing DB in ADO.NET'/><title type='text'>Changing the Database for an Open Connection</title><content type='html'>&lt;p class="docText"&gt;Use the &lt;tt&gt;ChangeDatabase( )&lt;/tt&gt; method to change the database for a connection.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code creates a &lt;tt&gt;Connection&lt;/tt&gt; to the Northwind database using the SQL Server .NET data provider. The connection is changed to use the pubs database. Finally the connection is closed. The &lt;tt&gt;Database&lt;/tt&gt; property of the &lt;tt&gt;SqlConnection&lt;/tt&gt; object is displayed throughout the sample for the different connection states.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown here.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example 1-12. File: ChangeDatabaseForm.cs&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;&lt;br /&gt;// Create the connection accessing Northwind database.&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;    ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;result.Append("Connection String:" + Environment.NewLine);&lt;br /&gt;result.Append(conn.ConnectionString + Environment.NewLine + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Open the connection.&lt;br /&gt;conn.Open( );&lt;br /&gt;result.Append("Connection.State: " + conn.State + Environment.NewLine);&lt;br /&gt;result.Append("Database: " + conn.Database + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Change the database to pubs.&lt;br /&gt;conn.ChangeDatabase("pubs");&lt;br /&gt;result.Append("Database: " + conn.Database + Environment.NewLine);&lt;br /&gt;&lt;br /&gt;// Close the connection.&lt;br /&gt;conn.Close( );&lt;br /&gt;result.Append("Connection.State: " + conn.State + Environment.NewLine);&lt;br /&gt;result.Append("Database: " + conn.Database);&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;ChangeDatabase( )&lt;/tt&gt; method is defined in the &lt;tt&gt;IDbConnection&lt;/tt&gt; interface that represents a connection to a data source and is implemented by .NET data providers for relational databases including those for SQL Server, Oracle, and OLE DB. The &lt;tt&gt;ChangeDatabase( )&lt;/tt&gt; method is used to change the current database for an open connection. It takes a single parameter that specifies the name of the database to use in place of the current database. The name of the database must be valid or an &lt;tt&gt;ArgumentException&lt;/tt&gt; will be raised. If the connection is not open when the method is called, an &lt;tt&gt;InvalidOperationException&lt;/tt&gt; is raised. A provider-specific exception is raised if the database cannot be changed for any reason.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;Database&lt;/tt&gt; property of the &lt;tt&gt;Connection&lt;/tt&gt; object is updated dynamically and returns the current database for an open connection or the name of a database that will be used by a closed connection when it is opened.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;When the &lt;tt&gt;Connection&lt;/tt&gt; is closed after &lt;tt&gt;ChangeDatabase( )&lt;/tt&gt; is called, the database is reset to that specified in the original connection string.&lt;/p&gt;&lt;br /&gt;&lt;p xmlns="" class="zoundry_raven_tags"&gt;&lt;br /&gt;  &lt;!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com --&gt;&lt;br /&gt;  &lt;span class="ztags"&gt;&lt;span class="ztagspace"&gt;Technorati&lt;/span&gt; : &lt;a href="http://www.technorati.com/tag/Changing+DB+in+ADO.NET" class="ztag" rel="tag"&gt;Changing DB in ADO.NET&lt;/a&gt;&lt;/span&gt; &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-1364978191642695154?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/1364978191642695154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/changing-database-for-open-connection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1364978191642695154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/1364978191642695154'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/changing-database-for-open-connection.html' title='Changing the Database for an Open Connection'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-7082674382537585869</id><published>2008-09-24T12:32:00.001+05:30</published><updated>2008-09-24T12:32:14.800+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Using Transactions'/><title type='text'>Using Transactions with Pooled Connections</title><content type='html'>&lt;p class="docText"&gt;Connections participating in transactions are drawn from the connection pool and assigned based on an exact match with the transaction context of the requesting thread and with the connection string.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;Each connection pool is divided into a subdivision for connections without a transaction context and zero or more subdivisions for connections associated with a particular transaction context. Each of these subdivisions, whether associated with a transaction context or not, uses connection pooling based on exact matching of the connection string as described &lt;a href="http://adodotnetslackers.blogspot.com/2008/09/taking-advantage-of-connection-pooling.html"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;When a thread associated with a particular transaction context requests a connection, one from the appropriate pool enlisted with that transaction is automatically returned.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;When a connection is closed it is returned to the appropriate subdivision in the connection pool based on the transaction context. This allows a connection to be closed without generating an error even if a distributed transaction is still pending. The transaction can committed or aborted later.&lt;/p&gt;&lt;br /&gt;&lt;p xmlns="" class="zoundry_raven_tags"&gt;&lt;br /&gt;  &lt;!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com --&gt;&lt;br /&gt;  &lt;span class="ztags"&gt;&lt;span class="ztagspace"&gt;Technorati&lt;/span&gt; : &lt;a href="http://www.technorati.com/tag/Using+Transactions" class="ztag" rel="tag"&gt;Using Transactions&lt;/a&gt;&lt;/span&gt; &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-7082674382537585869?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/7082674382537585869/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-transactions-with-pooled.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7082674382537585869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7082674382537585869'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-transactions-with-pooled.html' title='Using Transactions with Pooled Connections'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-226168491801672755</id><published>2008-09-24T12:29:00.003+05:30</published><updated>2008-09-27T16:55:08.954+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Collection Pooling in ADO.NET'/><title type='text'>Setting Connection Pooling Options</title><content type='html'>&lt;div class="docText"&gt;Use the connection string to control connection pooling for the SQL Server, OLE DB .NET, Oracle, or ODBC.NET data provider.&lt;/div&gt;&lt;br /&gt;The sample code contains a method and four event handlers:&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Form.Load&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates a &lt;tt&gt;Connection&lt;/tt&gt;, attaches an event handler to its &lt;tt&gt;StateChange&lt;/tt&gt; event, and sets default properties for controls on the form that are used to specify connection properties. The &lt;tt&gt;UpdateConnection( )&lt;/tt&gt; method is called to dynamically construct a connection string from the specified properties.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;UpdateConnectionString( )&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;This method dynamically constructs a connection string from the connection string properties specified by the user in text boxes on the form. This method is called to update the connection string when the user changes the value of any of the controls used to specify connection string properties.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Open Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Opens the &lt;tt&gt;Connection&lt;/tt&gt; that is based on the connection string constructed in the &lt;tt&gt;UpdateConnectionString( )&lt;/tt&gt; method.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Close Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Closes the connection string.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Connection.StateChange&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Displays original and current state information about the connection when its state changes.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example-File: ConnectionPoolingOptionsForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;private SqlConnection conn;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void ConnectionPoolingOptionsForm_Load(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{    &lt;br /&gt;conn = new SqlConnection( );&lt;br /&gt;conn.StateChange += new StateChangeEventHandler(conn_StateChange);&lt;br /&gt;&lt;br /&gt;connectionStringTextBox.Text =&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"];&lt;br /&gt;connectTimeoutTextBox.Text = "15";&lt;br /&gt;connectLifetimeTextBox.Text = "0";&lt;br /&gt;minPoolSizeTextBox.Text = "0";&lt;br /&gt;maxPoolSizeTextBox.Text = "100";&lt;br /&gt;poolCheckBox.Checked = true;&lt;br /&gt;&lt;br /&gt;UpdateConnectionString( );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void UpdateConnectionString( )&lt;br /&gt;{&lt;br /&gt;connectionStringTextBox.Text =&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_ConnectString"] +&lt;br /&gt;"Connection Timeout = " + connectTimeoutTextBox.Text + ";" +&lt;br /&gt;"Connection Lifetime = " + connectLifetimeTextBox.Text + ";" +&lt;br /&gt;"Min Pool Size = " + minPoolSizeTextBox.Text + ";" +&lt;br /&gt;"Max Pool Size = " + maxPoolSizeTextBox.Text + ";" +&lt;br /&gt;"Pooling = " + poolCheckBox.Checked.ToString( );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void openButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;conn.ConnectionString = connectionStringTextBox.Text;&lt;br /&gt;conn.Open( );&lt;br /&gt;}&lt;br /&gt;catch(SqlException ex)&lt;br /&gt;{&lt;br /&gt;MessageBox.Show("ERROR: " + ex.ToString( ), "Open Connection",&lt;br /&gt;MessageBoxButtons.OK, MessageBoxIcon.Error);&lt;br /&gt;}&lt;br /&gt;catch(InvalidOperationException ex)&lt;br /&gt;{&lt;br /&gt;MessageBox.Show("ERROR: " + ex.ToString( ), "Open Connection",&lt;br /&gt;MessageBoxButtons.OK, MessageBoxIcon.Error);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void closeButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;conn.Close( );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void conn_StateChange(object sender, StateChangeEventArgs e)&lt;br /&gt;{&lt;br /&gt;connectionStateTextBox.Text =&lt;br /&gt;"Connection.StateChange event occurred" +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"OriginalState = " + e.OriginalState.ToString( ) +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"CurrentState = " + e.CurrentState.ToString( );&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;The following subsections describe how to control connection pooling for SQL Server, Oracle, OLE DB, and ODBC .NET data providers.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt;&lt;div class="docText"&gt;The connection string attributes that control connection pooling for the SQL Server .NET data provider are described below.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table1 . SQL Server connection string pooling attributes&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Attribute&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connection Lifetime&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Length of time in seconds after creation after which a connection is destroyed. The default is 0 indicating that connection will have the maximum time-out.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connection Reset&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Specifies whether the connection is reset when removed from the pool. The default is &lt;tt&gt;true&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Enlist&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Specifies whether the connection is automatically enlisted in the current transaction context of the creation thread if that transaction context exists. The default is &lt;tt&gt;true&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Max Pool Size&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Maximum number of connections allowed in the pool. The default is 100.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Min Pool Size&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Minimum number of connections maintained in the pool. The default is 0.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Pooling&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Specifies whether the connection is drawn from a pool or when necessary created and added to a pool. The default is &lt;tt&gt;true&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt;&lt;div class="docText"&gt;The connection string attributes that control connection pooling for the Oracle .NET data provider are described in Table2.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docTableTitle"&gt;Table2. Oracle connection string pooling attributes&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Attribute&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connection Lifetime&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Length of time in seconds after creation after which a connection is destroyed. The default is 0 indicating that connection will have the maximum time-out.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Enlist&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Specifies whether the connection is automatically enlisted in the current transaction context of the creation thread if that transaction context exists. The default is &lt;tt&gt;true&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Max Pool Size&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Maximum number of connections allowed in the pool. The default is 100.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Min Pool Size&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Minimum number of connections maintained in the pool. The default is 0.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Pooling&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Specifies whether the connection is drawn from a pool or when necessary created and added to a pool. The default is &lt;tt&gt;true&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;OLE DB&lt;/h5&gt;&lt;div class="docText"&gt;The OLE DB .NET data provider uses resource pooling support provided by the OLE DB Service component. You can override the default OLE DB provider services by specifying a value for the &lt;tt&gt;OLE DB Services&lt;/tt&gt; attribute in the connection string. OLE DB Resource pooling configuration is controlled using registry entries. There is no user interface to configure these entries-the registry must be edited directly. The registry entries are identified by the &lt;tt&gt;&lt;i&gt;&amp;lt;Provider's&lt;/i&gt;&lt;/tt&gt; &lt;tt&gt;&lt;i&gt;CLSID&amp;gt;&lt;/i&gt;&lt;/tt&gt;. CLSID values for some Microsoft OLE DB providers are:&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;SQLOLEDB (SQL Server):&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;HKEY_CLASSES_ROOT\CLSID\{0C7FF16C-38E3-11d0-97AB-00C04FC2AD98}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Microsoft.Jet.OLEDB.4.0 (Jet):&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;HKEY_CLASSES_ROOT\CLSID\{dee35070-506b-11cf-b1aa-00aa00b8de95}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;MSDAORA (Oracle):&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;HKEY_CLASSES_ROOT\CLSID\{e8cc4cbe-fdff-11d0-b865-00a0c9081c1d}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;MSDASQL (OLE DB Provider for ODBC):&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;HKEY_CLASSES_ROOT\CLSID\{c8b522cb-5cf3-11ce-ade5-00aa0044773d}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div class="docText"&gt;Some OLE DB provider configuration options set by registry entries are:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;HKEY_CLASSES_ROOT\CLSID\&amp;lt;Provider's CLSID&amp;gt;\SPTimeout&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The session pooling timeout is the number of seconds that an unused session remains in the pool before timing out and being closed. This is a &lt;tt&gt;DWORD&lt;/tt&gt; value with a default of &lt;tt&gt;&lt;i&gt;60&lt;/i&gt;&lt;/tt&gt; if the registry entry is not specified.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following registry entries are global to all providers:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DataAccess\Session Pooling\Retry Wait&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The amount of time that the service component will wait until attempting to contact the server again in the event of a failed connection attempt. This is a &lt;tt&gt;DWORD&lt;/tt&gt; value with a default of &lt;tt&gt;&lt;i&gt;64&lt;/i&gt;&lt;/tt&gt; if no registry value is present.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DataAccess\Session Pooling\ExpBackOff&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Determines the factor by which the service components will wait between reconnect attempts in the event of a failed connection attempt. This is a &lt;tt&gt;DWORD&lt;/tt&gt; value with a default of &lt;tt&gt;&lt;i&gt;2&lt;/i&gt;&lt;/tt&gt; if no registry value is present.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;HKEY_CLASSES_ROOT\CLSID\{2206CDB0-19C1-11D1-89E0-00C04FD7A829}&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;A &lt;tt&gt;DWORD&lt;/tt&gt; value that specifies the maximum lifetime in seconds of a pooled connection. The default is &lt;tt&gt;&lt;i&gt;600&lt;/i&gt;&lt;/tt&gt;. The CLSID is for the MSDAINITIALIZE component, which is the OLE DB service component manager that is used to parse OLE DB connection strings and initialize the appropriate provider.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;ODBC&lt;/h5&gt;&lt;div class="docText"&gt;The ODBC .NET data provider uses the connection pooling support provided by the ODBC Driver Manager (DM). Connection pooling is supported by Version 3.0 or later of the ODBC DM; the version of the ODBC driver does not matter.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following two registry settings control ODBC connection pooling:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Wait Retry&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The time in seconds that that the pool is blocked when the server is not responding. This setting affects all applications using the ODBC driver. The registry key specifies a REG_SZ value:&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;pre&gt;HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\&amp;lt;Driver_Name&amp;gt;\CPTimeout&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;CPTimeout&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The time in seconds that unused connections remain in the pool. This setting affects all ODBC drivers on the system. The registry key specifies a REG_SZ value:&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;pre&gt;HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Connection Pooling&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;You can control ODBC connection pooling in three ways:&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Using the &lt;tt&gt;ODBC&lt;/tt&gt; &lt;tt&gt;Data&lt;/tt&gt; &lt;tt&gt;Source&lt;/tt&gt; &lt;tt&gt;Administrator&lt;/tt&gt; to enable or disable pooling for the entire driver, and to control the &lt;tt&gt;CPTimeout&lt;/tt&gt; and &lt;tt&gt;Wait Retry&lt;/tt&gt; settings&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Editing the registry settings described above.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Using the ODBC API to control pooling options from an ODBC application. For more information about the ODBC API, see the &lt;i&gt;ODBC Programmer's Reference&lt;/i&gt; in the MSDN Library.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;&lt;br /&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Collection+Pooling+in+ADO.NET" rel="tag"&gt;Collection Pooling in ADO.NET&lt;/a&gt; &lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-226168491801672755?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/226168491801672755/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/setting-connection-pooling-options.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/226168491801672755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/226168491801672755'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/setting-connection-pooling-options.html' title='Setting Connection Pooling Options'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-2451769981382135223</id><published>2008-09-22T23:11:00.002+05:30</published><updated>2008-09-27T16:57:01.298+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connection Pooling in ADO.NET'/><title type='text'>Taking Advantage of Connection Pooling in ADO.NET</title><content type='html'>&lt;div class="docText"&gt;Connection pooling allows an application to reuse connections from a pool instead of repeatedly creating and destroying new connections. Connection pooling can significantly improve the performance and scalability of applications by allowing a smaller number of connections to service the connection requirements of an application and because the overhead of establishing a new connection is eliminated.&lt;/div&gt;&lt;div class="docText"&gt;A connection pool is created for each unique connection string. An algorithm associates items in the pool based on an exact match with the connection string; this includes capitalization, order of name value pairs, and even spaces between name/value pairs. Dynamically generated connection strings must be identical so that connection pooling is used. If delegation is used, there will be one pool per delegate user. When transactions are used, one pool is created per transaction context. When the connection pool is created, connection objects are created and added to the pool to satisfy the minimum pool size specified.&lt;/div&gt;&lt;div class="docText"&gt;When a connection is requested by an application and the maximum pool size has been reached, the request is queued. The request is satisfied by reallocating a connection that is released back to the pool when the &lt;tt&gt;Connection&lt;/tt&gt; is closed or disposed. The connection pool manager removes expired connections and connections that have had their connection with the server severed from the pool.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Connection&lt;/tt&gt; object should be closed as soon as it is no longer needed so that it is added to or returned to the connection pool. This is done by calling either the &lt;tt&gt;Close( )&lt;/tt&gt; or &lt;tt&gt;Dispose( )&lt;/tt&gt; method of the &lt;tt&gt;Connection&lt;/tt&gt;. Connections that are not explicitly closed might not be added to or returned to the connection pool.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;SQL Server and Oracle&lt;/h5&gt;&lt;div class="docText"&gt;The .NET data providers for SQL Server and Oracle provide efficient, transaction-aware support for connection pooling. Pools are created for each process and not destroyed until the process ends. Connection pooling is enabled by default.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Controlling SQL Server and Oracle .NET data provider connection pooling with connection string attribute/value pairs.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;OLE DB&lt;/h5&gt;&lt;div class="docText"&gt;The OLE DB .NET data provider pools connections by using resource pooling provided by the OLE DB core components.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The default OLE DB services that are enabled for a provider are specified by the value for the registry &lt;tt&gt;HKEY_CLASSES_ROOT\CLSID\&amp;lt;Provider&lt;/tt&gt;'s &lt;tt&gt;CLSID&amp;gt;\OLE_DBSERVICES DWORD&lt;/tt&gt; value. Table1 describes the alternatives.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table 1. OLE DB services enabled values&lt;/h5&gt;&lt;br /&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;OLE_DBSERVICES value&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0xffffffff&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services (default).&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0xfffffffe&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Pooling and AutoEnlistment.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0xfffffffb&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Client Cursor.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0xfffffff0&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Pooling, AutoEnlistment, and Client Cursor.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0x00000000&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;No services.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;missing value&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;No aggregation. All services are disabled.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;You can override the default OLE DB provider services by specifying a value for the &lt;tt&gt;OLE&lt;/tt&gt; &lt;tt&gt;DB&lt;/tt&gt; &lt;tt&gt;Services&lt;/tt&gt; attribute in the connection string. Below Table describes possible values.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table 2. OLE DB services connection string values&lt;/h5&gt;&lt;br /&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;OLE DB Services attribute value&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Default services enabled&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;-1&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services (default)&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;-2&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Pooling and AutoEnlistment&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;-5&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Client Cursor&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;-7&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;All services except Pooling, AutoEnlistment, and Client Cursor&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;0&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;No services&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following three configurable settings control OLE DB connection pooling:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;SPTimeout&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The length of time in seconds that an unused connection remains in the pool before it is released. This can be configured for each provider and defaults to 60 seconds.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Retry Wait&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The length of time in seconds before an attempt to acquire a connection is reattempted when the server is not responding. This is global to all providers and defaults to 64 seconds.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;ExpBackOff&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The factor by which the retry wait time is increased when a connection attempt fails before reattempting the connection. This is global to all providers and defaults to a factor of 2.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;OLE DB connection pooling is enabled by default; you can control it in three different ways:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Specify a value for the &lt;tt&gt;OLE DB Services&lt;/tt&gt; attribute in the connection string.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Edit the registry to enable or disable pooling for an individual provider or globally by changing registry values.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Use the OLE DB API (Application Programming Interface) from an application to enable or disable connection pooling. The &lt;tt&gt;SPTimeout&lt;/tt&gt; and &lt;tt&gt;Retry&lt;/tt&gt; &lt;tt&gt;Wait&lt;/tt&gt; can be configured programmatically only by manipulating the registry entries. For more information about the OLE DB API, see the OLE DB Programmer's Reference in MSDN Library.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h5 class="docSection3Title"&gt;ODBC&lt;/h5&gt;&lt;div class="docText"&gt;The ODBC .NET data provider pools connections by using the connection pooling provided by the ODBC Driver Manager (DM). Pooling parameters for an ODBC driver affect all applications that use that driver, unless changed from within a native ODBC application.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following two configurable settings control ODBC connection pooling:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;CPTimeout&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The length of time in seconds that an unused connection remains in the pool before it is released.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Wait Retry&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The length of time before an attempt to acquire a connection is reattempted when the server is not responding.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;Connection pooling is enabled by default. You can enable, disable, and configure it in three ways:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Use the &lt;tt&gt;ODBC&lt;/tt&gt; &lt;tt&gt;Data&lt;/tt&gt; &lt;tt&gt;Source&lt;/tt&gt; &lt;tt&gt;Administrator&lt;/tt&gt;, introduced with ODBC 3.5 (MDAC 1.5), to enable or disable pooling for the entire driver and to control the &lt;tt&gt;CPTimeout&lt;/tt&gt; and &lt;tt&gt;Wait&lt;/tt&gt; &lt;tt&gt;Retry&lt;/tt&gt; settings.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Edit the registry.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Use the ODBC API from an ODBC application to limit the scope of pooling to the environment handler or to the driver, and to configure other pooling options. For more information about the ODBC API, see the ODBC Programmer's Reference in the MSDN Library.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Connection+Pooling+in+ADO.NET" rel="tag"&gt;Connection Pooling in ADO.NET&lt;/a&gt; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-2451769981382135223?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/2451769981382135223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/taking-advantage-of-connection-pooling.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2451769981382135223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2451769981382135223'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/taking-advantage-of-connection-pooling.html' title='Taking Advantage of Connection Pooling in ADO.NET'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-6155191505127034624</id><published>2008-09-22T23:09:00.004+05:30</published><updated>2008-10-20T18:03:41.164+05:30</updated><title type='text'>Monitoring Connections in ADO.NET</title><content type='html'>&lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt;&lt;div class="docText"&gt;You can monitor SQL Server connections and connection pooling using the SQL Server Profiler or the Windows Performance Monitor as described in the following subsections.&lt;/div&gt;&lt;h5 class="docSection4Title"&gt;1. SQL Server Profiler&lt;/h5&gt;&lt;div class="docText"&gt;To use the SQL Server Profiler to monitor connection pooling:&lt;/div&gt;&lt;ol class="docList" type="1"&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: bold;"&gt;Start the Profiler using one of the following methods&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;From Windows desktop: Start --] All Programs --] Microsoft SQL Server --] Profiler.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;From SQL Enterprise Manager: Tools --] SQL Profiler.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;When the SQL Server Profiler appears, select File --] New --] Trace.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Supply connection details and click OK. The Trace Properties dialog box will appear.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Select the Events tab of the Trace Properties dialog box.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;In the Selected Events list box, ensure that the &lt;tt&gt;Audit&lt;/tt&gt; &lt;tt&gt;Login&lt;/tt&gt; and &lt;tt&gt;Audit&lt;/tt&gt; &lt;tt&gt;Logout&lt;/tt&gt; events appear beneath the Security Audit node. Remove all other events from the list. Click the Run button to start the trace.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;The new Profiler window will display a table containing &lt;tt&gt;Audit&lt;/tt&gt; &lt;tt&gt;Login&lt;/tt&gt; events when connections are established and &lt;tt&gt;Audit&lt;/tt&gt; &lt;tt&gt;Logout&lt;/tt&gt; events when connections are closed.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h5 class="docSection4Title"&gt;2. Windows Performance Monitor&lt;/h5&gt;&lt;div class="docText"&gt;To use the Windows Performance Monitor to monitor connection pooling:&lt;/div&gt;&lt;ol class="docList" type="1"&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: bold;"&gt;Start Performance Monitor by selecting Start --] All Programs --] Administrative Tools --] Performance.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Add performance counters to monitor connection pooling with one of the following methods:&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Right-click the graph and select Add Counters from the popup menu.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Click the add button above the graph.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;In the Performance object drop down list, select ".NET CLR Data."&lt;/span&gt;&lt;/div&gt;&lt;div class="docList"&gt;The SQL Server .NET data provider adds performance counters that can tune connection pooling and troubleshoot pooling problems. Below table describes the counters.&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h5 class="docTableTitle"&gt;Table . SQL Server .NET provider performance counters&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" height="468" rules="all" style="width: 605px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Counter&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;SqlClient: Current # of pooled and nonpooled connections&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Current number of connections, both pooled and non-pooled&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;SqlClient: Current # pooled connections&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Current number of pooled connections&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;SqlClient: Current # connection pools&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Current number of connection pools&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;SqlClient: Peak # pooled connections&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The largest number of connections in all pools since the process started&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;SqlClient: Total # failed connects&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The total number of attempts to open a connection that have failed for any reason&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;ol class="docList" start="4" type="1"&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: bold;"&gt;Select the counters to monitor from the list box and click the Add button. Click the Close button.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h5 class="docSection3Title"&gt;ODBC&lt;/h5&gt;&lt;div class="docText"&gt;To enable ODBC performance monitoring:&lt;/div&gt;&lt;ol class="docList" type="1"&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: bold;"&gt;Open the ODBC Data Source Administrator in Control Panel --] Administrative Tools.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Select the Connection Pooling tab.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Ensure that the PerfMon Enable checkbox is checked.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Start Performance Monitor by selecting Start --] All Programs --] Administrative Tools --] Performance.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;Add performance counters to monitor connection pooling with one of the following methods:&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Right-click the graph and select Add Counters from the popup menu.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Click the add button above the graph.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;&lt;span style="font-weight: normal;"&gt;In the Performance object drop down list, select ODBC Connection Pooling. Table2 describes the ODBC Connection Pooling counters.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h5 class="docTableTitle"&gt;Table2. ODBC connection pooling counters&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" height="420" rules="all" style="width: 560px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Counter&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connections Currently Active&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of connections currently used by applications&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connections Currently Free&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of connections in the pool available for requests&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connections/Sec Hard&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of real connections per second&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Connections/Sec Soft&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of connections server from the pool per second&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Disconnections/Sec Hard&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of real disconnects per second&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Disconnections/Sec Soft&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Number of disconnects from the pool per second&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-6155191505127034624?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/6155191505127034624/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/monitoring-connections-in-adonet.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/6155191505127034624'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/6155191505127034624'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/monitoring-connections-in-adonet.html' title='Monitoring Connections in ADO.NET'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-9057671376684406748</id><published>2008-09-22T23:08:00.002+05:30</published><updated>2008-09-27T17:01:00.117+05:30</updated><title type='text'>Using the Data Link Properties Dialog Box</title><content type='html'>&lt;div class="docText"&gt;Use COM interop with the OLE DB Service Component to display the Data Link Properties dialog box.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;You'll need a reference to the Primary Interop Assembly (PIA) for ADO provided in the file &lt;i&gt;ADODB.DLL&lt;/i&gt;; select &lt;tt&gt;adodb&lt;/tt&gt; from the .NET tab in Visual Studio .NET's Add Reference Dialog. You'll also need a reference to the Microsoft OLE DB Service Component 1.0 Type Library from the COM tab in Visual Studio .NET's Add Reference Dialog.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains a single event handler:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Data Link Dialog Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and displays a Data Link Properties dialog box using the Microsoft OLE DB Service Component through COM interop.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: DataLinkDialogForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void dataLinkDialogButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;ADODB.Connection conn = new ADODB.Connection( );&lt;br /&gt;object oConn = (object) conn;&lt;br /&gt;&lt;br /&gt;MSDASC.DataLinks dlg = new MSDASC.DataLinks( );&lt;br /&gt;dlg.PromptEdit(ref oConn);&lt;br /&gt;&lt;br /&gt;connectionStringTextBox.Text = conn.ConnectionString;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-9057671376684406748?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/9057671376684406748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-data-link-properties-dialog-box.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/9057671376684406748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/9057671376684406748'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-data-link-properties-dialog-box.html' title='Using the Data Link Properties Dialog Box'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8941556537311635685</id><published>2008-09-22T22:52:00.001+05:30</published><updated>2008-10-20T17:58:41.488+05:30</updated><title type='text'>Connecting to SQL Server Using Integrated Security from ASP.NET in ADO.NET</title><content type='html'>&lt;p class="docText"&gt;Connecting to a SQL Server database provides two different authentication modes:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;br /&gt;&lt;dt&gt;&lt;em&gt;&lt;span class="docPubcolor"&gt;Windows Authentication&lt;/span&gt;&lt;/em&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Uses the current security identity from the Windows NT or Windows 2000 user account to provide authentication information. It does not expose the user ID and password and is the recommended method for authenticating a connection.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;em&gt;&lt;span class="docPubcolor"&gt;SQL Server Authentication&lt;/span&gt;&lt;/em&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;Uses a SQL Server login account providing a user ID and password.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;p class="docText"&gt;Integrated security requires that the SQL Server is running on the same computer as IIS and that all application users are on the same domain so that their credentials are available to IIS. The following areas of the application need to be configured:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Configure the ASP.NET application so that &lt;tt&gt;Integrated&lt;/tt&gt; &lt;tt&gt;Windows&lt;/tt&gt; &lt;tt&gt;Authentication&lt;/tt&gt; is enabled and &lt;tt&gt;Anonymous&lt;/tt&gt; &lt;tt&gt;Access&lt;/tt&gt; is disabled.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;The &lt;em&gt;web.config&lt;/em&gt; file establishes the authentication mode that the application uses and that the application will run as or impersonate the user. Add the following elements to the &lt;em&gt;web.config&lt;/em&gt; file:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;authentication mode="Windows" /&amp;gt;&lt;br /&gt;&amp;lt;identity impersonate="true" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;The connection string must contain attributes that tell the SQL Server that integrated security is used. Use the &lt;tt&gt;Integrated&lt;/tt&gt; &lt;tt&gt;Security=SSPI&lt;/tt&gt; attribute-and-value pair instead of the &lt;tt&gt;User&lt;/tt&gt; &lt;tt&gt;ID&lt;/tt&gt; and &lt;tt&gt;Password&lt;/tt&gt; attributes in the connection string. The older attribute-and-value pair &lt;tt&gt;Trusted_Connection=Yes&lt;/tt&gt; is also supported.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Add users and groups from the domain and set their access permissions as required.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p class="docText"&gt;By default, ASP.NET applications run in the context of a local user &lt;tt&gt;ASPNET&lt;/tt&gt; on IIS. The account has limited permissions and is local to the IIS computer and therefore not recognized as a user on remote computers. To overcome this limitation when SQL Server is not on the same computer as IIS, run the web application in the context of a domain user recognized on both IIS and SQL Server computers.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;In addition to the areas identified where IIS and SQL Server are on the same computer, the following additional items must be configured if the SQL Server is on a different computer:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Ensure that the mapped domain user has required privileges to run the web application.&lt;/p&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;br /&gt;&lt;p class="docList"&gt;Configure the web application to impersonate the domain user. Add the following elements to the &lt;em&gt;web.config&lt;/em&gt; file for the web application:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;lt;authentication mode="Windows" /&amp;gt;&lt;br /&gt;&amp;lt;identity impersonate="true" userName="domain\username"&lt;br /&gt;        password="myPassword" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8941556537311635685?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8941556537311635685/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-sql-server-using_22.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8941556537311635685'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8941556537311635685'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-sql-server-using_22.html' title='Connecting to SQL Server Using Integrated Security from ASP.NET in ADO.NET'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5857088063152901462</id><published>2008-09-22T22:51:00.002+05:30</published><updated>2008-09-25T13:37:52.687+05:30</updated><title type='text'>Storing Connection Strings in ADO.NET</title><content type='html'>&lt;div class="docText"&gt;A connection string is made up of a semi-colon delimited collection of attribute/value pairs that define how to connect a data source. Although connection strings tend to look similar, the available and required attributes are different depending on the data provider and on the underlying data source. There are a variety of options providing differing degrees of flexibility and security.&lt;/div&gt;&lt;br /&gt;&lt;table border="1" cellspacing="0"  style="width: 90%;"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;h2 class="docSidebarTitle"&gt;Persist Security Info&lt;/h2&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Persist&lt;/tt&gt; &lt;tt&gt;Security&lt;/tt&gt; &lt;tt&gt;Info&lt;/tt&gt; connection string attribute specifies whether the data source can hang on to, or persist, sensitive information such as user authentication credentials. Its value should be kept at the default &lt;tt&gt;false&lt;/tt&gt;. If its value is &lt;tt&gt;true&lt;/tt&gt;, the connection information-including the password-can be obtained by querying the connection, allowing an untrusted party to have access to sensitive information when a &lt;tt&gt;Connection&lt;/tt&gt; is passed or persisted to a disk. This is an issue only when passing connected objects such as &lt;tt&gt;Connection&lt;/tt&gt; or &lt;tt&gt;DataAdapter&lt;/tt&gt;; disconnected objects such as &lt;tt&gt;DataSet&lt;/tt&gt; and &lt;tt&gt;DataTable&lt;/tt&gt; do not store information about the original source of their data.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Before a data source object is initialized for the first time, sensitive information can be retrieved from it regardless of the setting of the &lt;tt&gt;Persist&lt;/tt&gt; &lt;tt&gt;Security&lt;/tt&gt; &lt;tt&gt;Info&lt;/tt&gt; property. Avoid passing uninitialized data source objects.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;Persist&lt;/tt&gt; &lt;tt&gt;Security&lt;/tt&gt; &lt;tt&gt;Info&lt;/tt&gt; connection string attribute is supported by the SQL Server, OLE DB, and Oracle .NET Framework data providers. Although not supported by the ODBC .NET Framework data provider, its behavior is as if &lt;tt&gt;Persist&lt;/tt&gt; &lt;tt&gt;Security&lt;/tt&gt; &lt;tt&gt;Info&lt;/tt&gt; is &lt;tt&gt;false&lt;/tt&gt; and cannot be changed. Check the documentation for other data providers to determine specific implementation details.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;Connecting to a database server requires passing credentials-username and password-to the server in a connection string. These credentials, together with the data source name, need to be kept private to protect unauthorized access to the data source. There are two approaches for obtaining these credentials:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Prompting for connection credentials at runtime.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Storing predetermined connection credentials on the server and using them at runtime to connect to the database server.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;table border="1" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;h2 class="docSidebarTitle"&gt;Integrated Security&lt;/h2&gt;&lt;div class="docText"&gt;Integrated security is the most secure way to connect to a SQL Server and should be used unless it is impractical to do so. Integrated security uses the identity of the current active user rather than an explicit user ID and password in the connection string to authorize access to the database. Integrated security avoids storing usernames and passwords in connection strings and its use is recommended where possible instead of SQL Server Authentication.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;To use integrated security in the connection string, specify the value &lt;tt&gt;SSPI&lt;/tt&gt; for the &lt;tt&gt;Integrated Security&lt;/tt&gt; attribute and do not specify &lt;tt&gt;User ID&lt;/tt&gt; and &lt;tt&gt;Password&lt;/tt&gt; connection string attributes:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Integrated Security=SSPI&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;See &lt;a href="http://adodotnetslackers.blogspot.com/2008/09/connecting-to-sql-server-using.html"&gt;this post&lt;/a&gt; for information about connecting to SQL Server using integrated security from ASP.NET.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;Often, it is not practical to prompt for connection credentials because of disadvantages including:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;i&gt;Security&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Transferring connection information from the browser to the server can expose connection credentials if they are not encrypted.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;Connection pooling&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Each user must be recognized separately by the server. This does not allow effective connection pooling and can limit the scalability of the application.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;Single sign-on&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;It is difficult to integrate with single sign-on strategies, which are becoming increasingly important in enterprise environments (for example, where numerous applications are aggregated into portals).&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;Server applications&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Cannot be used by applications that otherwise have no user interface, such as an XML web service.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;There are a number of techniques that you can use to store predetermined connection credentials. These, together with their advantages and drawbacks, are discussed in the following subsections.&lt;/div&gt;&lt;br /&gt;&lt;table align="center" bgcolor="black" border="0" cellpadding="1" cellspacing="0" height="171" style="width: 558px;"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;br /&gt;&lt;table bgcolor="white" border="0" cellpadding="6" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td valign="top" width="60"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Always configure predetermined accounts with the minimum permissions required.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Never use &lt;tt&gt;sa&lt;/tt&gt; or any other administrative account.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Never use blank passwords.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Hardcode in the application&lt;/h5&gt;&lt;br /&gt;&lt;div class="docText"&gt;An obvious technique for storing connection strings is hardcoding them into the application. Although this approach results in the best performance, it has poor flexibility; the application needs to be recompiled if the connection string needs to be changed for any reason. Security is poor. The code can be disassembled to expose connection string information. Caching techniques together with external storage techniques eliminate nearly all performance benefits of hardcoding over external storage techniques.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Hardcoding connection string information is not advised; external server-side storage is preferred in nearly all cases because of the increased flexibility, security, and configuration ease. A discussion of available external storage options follows.&lt;/div&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Application configuration file&lt;/h5&gt;&lt;br /&gt;&lt;div class="docText"&gt;An application configuration file is an XML-based text file that is used to store application-specific settings used at runtime by the application. The naming convention for and deployment location of the file depend on the type of application:&lt;/div&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;i&gt;Executable application&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The name of the configuration file is the name of the application executable with a &lt;i&gt;.config&lt;/i&gt; extension-for example, &lt;i&gt;myApplication.exe.config&lt;/i&gt;. It is located in the same directory as the executable file.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;ASP.NET application&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;A web application can have multiple configuration files all named &lt;i&gt;web.config&lt;/i&gt;. Each configuration file supplies configuration settings for its directory and all of its child directories; it also overrides any configuration settings inherited from parent directories.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;table align="center" bgcolor="black" border="0" cellpadding="1" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;br /&gt;&lt;table bgcolor="white" border="0" cellpadding="6" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td valign="top" width="60"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;br /&gt;&lt;div class="docText"&gt;The machine configuration file-&lt;i&gt;machine.config&lt;/i&gt;, located in the &lt;i&gt;CONFIG&lt;/i&gt; subdirectory of the .NET runtime installation-contains configuration information that applies to the computer. The &lt;i&gt;machine.config&lt;/i&gt; file is checked for configuration settings defined in an &lt;tt&gt;&amp;lt;appSettings&amp;gt;&lt;/tt&gt; element before the application configuration file is checked.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;It is best to put application settings in the application configuration file both to facilitate deployment and to keep the machine configuration file manageable and secure.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;&amp;lt;appSettings&amp;gt;&lt;/tt&gt; element of the application file is used to store custom application settings as a collection of key-value pairs. You can store a connection string as shown:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;lt;add key="ConnectionString"&lt;br /&gt;value="Data Source=(local);Initial Catalog=Northwind;User ID=sa;password=;"&lt;br /&gt;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;AppSettings&lt;/tt&gt; property of the &lt;tt&gt;System.Configuration.ConfigurationSettings&lt;/tt&gt; class is used to retrieve the value for a specific key within the &lt;tt&gt;appSettings&lt;/tt&gt; element; the &lt;tt&gt;ConfigurationSettings&lt;/tt&gt; class cannot be used to write settings to a configuration file.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Application configuration files facilitate deployment because the files are simply installed alongside other application files. One drawback is that application configuration files are not inherently secure since they store information as clear text in a file that is accessible through the file system. Encrypt the connection and other sensitive information within the configuration file and ensure that NTFS file permissions are set to restrict access to the file.&lt;/div&gt;&lt;br /&gt;&lt;table align="center" bgcolor="black" border="0" cellpadding="1" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;br /&gt;&lt;table bgcolor="white" border="0" cellpadding="6" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td valign="top" width="60"&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Make sure you name the application configuration file for a Windows Forms application &lt;i&gt;App.config-&lt;/i&gt;this is the default. At build time, this file is automatically copied into the startup directory by Visual Studio .NET with the name &lt;i&gt;applicationName.exe.config&lt;/i&gt;.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;If you name the application configuration file &lt;i&gt;applicationName.exe.config&lt;/i&gt; within your solution, you will have to copy it to the startup directory each time you modify it and each time you build the solution; the build process deletes it from the startup directory.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;&lt;span style="font-size: large;"&gt;Universal data link (UDL) file&lt;/span&gt;&lt;/h5&gt;&lt;div class="docText"&gt;The OLE DB .NET data providers supports UDL filenames in its connection string. The UDL file is a resource external to the application that encapsulates connection properties in a separate file. It must be protected using NTFS security to prevent connection information from being exposed or altered. The SQL Server .NET data provider does not support UDL files in its connection string. UDL files are not encrypted; cryptography cannot be used to increase security. NTFS directory and file encryption can secure a UDL file so that even if unauthorized access is gained to the file or the physical disk is stolen, the user ID and password of the user who encrypted the file would still be required to access its contents.&lt;/div&gt;&lt;br /&gt;&lt;table border="1" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;h2 class="docSidebarTitle"&gt;NTFS Encryption&lt;/h2&gt;&lt;div class="docText"&gt;NTFS was enhanced in Windows 2000 with the Encrypted File System (EFS) that provides file- and directory-level encryption. Actually, EFS encrypts only files-directories are simply marked so that new files in the directory are encrypted. Encryption and decryption of files is both automatic and transparent for the user who set the encryption.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Encrypted files are visible to any user who can access the system but the contents of the encrypted files can only be viewed by the user who set the encryption. If necessary, standard NT security methods can hide directories and files from view of specific users and user groups.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;EFS is a separate mechanism that is used together with the standard security subsystem.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;h5 class="docSection3Title"&gt;Windows registry&lt;/h5&gt;&lt;div class="docText"&gt;You can store connection strings in the Windows registry as a subkey of &lt;tt&gt;HKEY_LOCAL_MACHINE\SOFTWARE&lt;/tt&gt;. You can encrypt these settings within the registry subkey and restrict access to the subkey to increase the security of this technique. This technique is easy to use because of programmatic support for registry access in .NET classes &lt;tt&gt;Registry&lt;/tt&gt; and &lt;tt&gt;RegistryKey&lt;/tt&gt; in the &lt;tt&gt;Microsoft.Win32&lt;/tt&gt; namespace.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Storing connection strings in the registry is usually discouraged because of deployment issues; the registry settings must be deployed with the application, defeating benefits of &lt;tt&gt;&lt;i&gt;xcopy&lt;/i&gt;&lt;/tt&gt; deployment. Application code can also be restricted in its access to the registry, further complicating deployment.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;&lt;span style="font-size: large;"&gt;Custom file&lt;/span&gt;&lt;/h5&gt;&lt;div class="docText"&gt;A custom file is any file that is used to for proprietary storage of application settings that are typically used at runtime. There is generally no particular advantage to using a custom file to store connection information so the technique is not recommended. The approach requires extra coding and forces concurrency and other issues to be explicitly addressed.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5857088063152901462?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5857088063152901462/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/storing-connection-strings-in-adonet.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5857088063152901462'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5857088063152901462'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/storing-connection-strings-in-adonet.html' title='Storing Connection Strings in ADO.NET'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5149745961926985686</id><published>2008-09-20T20:44:00.003+05:30</published><updated>2008-10-20T17:58:57.921+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='DB Independent code'/><title type='text'>Writing Database-Independent Code</title><content type='html'>&lt;p class="docText"&gt;The solution shows how to use interfaces that are inherited by .NET connected classes (such as &lt;tt&gt;Connection&lt;/tt&gt; and &lt;tt&gt;DataReader&lt;/tt&gt;) to create provider-independent code that can be used with provider-specific code to access unique functionality.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The sample code contains a method and two event handlers:&lt;/p&gt;&lt;br /&gt;&lt;dl class="docList"&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;GetData( )&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;This method is a .NET data provider-independent method that accepts .NET data provider-specific &lt;tt&gt;Connection&lt;/tt&gt; and &lt;tt&gt;DataAdapter&lt;/tt&gt; arguments as generic &lt;tt&gt;IDbConnection&lt;/tt&gt; and &lt;tt&gt;IDbDataAdapter&lt;/tt&gt; interface types. The interfaces are used to fill a &lt;tt&gt;DataSet&lt;/tt&gt; from the Customers table in Northwind. The default view of the Customers &lt;tt&gt;DataTable&lt;/tt&gt; is bound to the data grid on the form.&lt;/p&gt;&lt;br /&gt;&lt;p class="docList"&gt;Finally, the provider-specific &lt;tt&gt;Connection&lt;/tt&gt; for the &lt;tt&gt;IDbConnection&lt;/tt&gt; is identified and provider-specific logic executed.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;SQL &lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;Button.Click&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;This event handler is provider-specific code that creates a &lt;tt&gt;SqlConnection&lt;/tt&gt; and a &lt;tt&gt;SqlDataAdapter&lt;/tt&gt; object and passes them as arguments into the provider-independent &lt;tt&gt;GetData( )&lt;/tt&gt; method.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;span class="docPubcolor"&gt;OLE DB &lt;span class="docPubcolor"&gt;&lt;span class="docMonofont"&gt;Button.Click&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;br /&gt;&lt;p class="docList"&gt;This event handler is provider-specific code that creates an &lt;tt&gt;OleDbConnection&lt;/tt&gt; and an &lt;tt&gt;OleDbDataAdapter&lt;/tt&gt; object and passes them as arguments into the provider-independent &lt;tt&gt;GetData( )&lt;/tt&gt; method.&lt;/p&gt;&lt;br /&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;p class="docText"&gt;The C# code is shown here.&lt;/p&gt;&lt;br /&gt;&lt;h5 class="docExampleTitle"&gt;Example 1-9. File: DatabaseIndependentCodeForm.cs&lt;/h5&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.Common;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void GetData(IDbConnection conn, IDbDataAdapter da)&lt;br /&gt;{&lt;br /&gt;    // Create the command and assign it to the IDbDataAdapter interface.&lt;br /&gt;    IDbCommand cmd = conn.CreateCommand( );&lt;br /&gt;    cmd.CommandText = "SELECT * FROM Customers";&lt;br /&gt;    da.SelectCommand = cmd;&lt;br /&gt;    // Add a table mapping.&lt;br /&gt;    da.TableMappings.Add("Table", "Customers");&lt;br /&gt;&lt;br /&gt;    dataGrid.DataSource = null;&lt;br /&gt;    &lt;br /&gt;    // Fill the DataSet.&lt;br /&gt;    DataSet ds = new DataSet( );&lt;br /&gt;    da.Fill(ds);&lt;br /&gt;&lt;br /&gt;    // Bind the default view for the Customer table to the grid.&lt;br /&gt;    dataGrid.DataSource = ds.Tables["Customers"].DefaultView;&lt;br /&gt;&lt;br /&gt;    // Identify provider-specific connection type and process appropriately.&lt;br /&gt;    if (conn is SqlConnection)&lt;br /&gt;    {&lt;br /&gt;        MessageBox.Show("Specific processing for SQL data provider.");&lt;br /&gt;    }&lt;br /&gt;    else if(conn is OleDbConnection)&lt;br /&gt;    {&lt;br /&gt;        MessageBox.Show("Specific processing for OLE DB data provider.");&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void sqlButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;    // Create a SQL Connection and DataAdapter.&lt;br /&gt;    SqlConnection conn = new SqlConnection(&lt;br /&gt;        ConfigurationSettings.AppSettings["Sql_ConnectString"]);&lt;br /&gt;    SqlDataAdapter da = new SqlDataAdapter( );&lt;br /&gt;&lt;br /&gt;    dataGrid.CaptionText = "SQL .NET Provider";&lt;br /&gt;&lt;br /&gt;    // Call provider-independent function to retrieve data.&lt;br /&gt;    GetData(conn, da);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void oleDbButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;    // Create a OLE DB Connection and DataAdapter.&lt;br /&gt;    OleDbConnection conn = new OleDbConnection(&lt;br /&gt;        ConfigurationSettings.AppSettings["OleDb_ConnectString"]);&lt;br /&gt;    OleDbDataAdapter da = new OleDbDataAdapter( );&lt;br /&gt;&lt;br /&gt;    dataGrid.CaptionText = "OLE DB .NET Provider";&lt;br /&gt;&lt;br /&gt;    // Call provider-independent function to retrieve data.&lt;br /&gt;    GetData(conn, da);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;tt&gt;IDbConnection&lt;/tt&gt;, &lt;tt&gt;IDbCommand&lt;/tt&gt;, &lt;tt&gt;IDataAdapter&lt;/tt&gt;, and &lt;tt&gt;IDataReader&lt;/tt&gt; interfaces are implemented by &lt;tt&gt;Connection&lt;/tt&gt;, &lt;tt&gt;Command&lt;/tt&gt;, &lt;tt&gt;DataAdapter&lt;/tt&gt;, and &lt;tt&gt;DataReader&lt;/tt&gt; classes in .NET data providers. You can pass these provider-independent base classes as interface arguments instead of the provider-specific inherited classes. This allows applications that support multiple data providers to reuse common provider-independent code.&lt;/p&gt;&lt;br /&gt;&lt;p class="docText"&gt;The provider-specific functionality of the classes is not available when the base interfaces are used. The &lt;tt&gt;is&lt;/tt&gt; operator is used to identify the provider-specific class of the provider-independent interface. Branching logic is then used execute code specific to that class.&lt;/p&gt;&lt;br /&gt;&lt;p xmlns="" class="zoundry_raven_tags"&gt;&lt;br /&gt;  &lt;!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com --&gt;&lt;br /&gt;  &lt;span class="ztags"&gt;&lt;span class="ztagspace"&gt;Technorati&lt;/span&gt; : &lt;a href="http://www.technorati.com/tag/DB+Independent+code" class="ztag" rel="tag"&gt;DB Independent code&lt;/a&gt;&lt;/span&gt; &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5149745961926985686?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5149745961926985686/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/writing-database-independent-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5149745961926985686'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5149745961926985686'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/writing-database-independent-code.html' title='Writing Database-Independent Code'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8806313887496132788</id><published>2008-09-20T20:42:00.003+05:30</published><updated>2008-09-27T17:05:09.121+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to Exc or Outlook'/><title type='text'>Connecting to Exchange or Outlook</title><content type='html'>&lt;div class="docText"&gt;Use the OLE DB Jet provider to access Exchange and Outlook data.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains two event handlers:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Form.Load&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Displays a form that allows the user to specify the mailbox name and mail profile to connect to.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Connect Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and opens a connection to Outlook or Exchange data using the OLE DB .NET data provider. A &lt;tt&gt;DataAdapter&lt;/tt&gt; is used to fill a table with the &lt;tt&gt;&lt;i&gt;Subject&lt;/i&gt;&lt;/tt&gt; and &lt;tt&gt;&lt;i&gt;Content&lt;/i&gt;&lt;/tt&gt; of each message in the &lt;tt&gt;&lt;i&gt;Inbox&lt;/i&gt;&lt;/tt&gt;. The default view of the table is bound to a data grid on the form.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: ConnectExchangeDataForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void ConnectExchangeDataForm_Load(object sender,&lt;br /&gt;System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;mailboxNameTextBox.Text = "Personal Folders";&lt;br /&gt;profileTextBox.Text = "Outlook";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;String sqlText = "SELECT Subject, Contents FROM Inbox";&lt;br /&gt;&lt;br /&gt;// Build the connection string.&lt;br /&gt;String connectionString="Provider=Microsoft.Jet.OLEDB.4.0;" +&lt;br /&gt;"Outlook 9.0;" +&lt;br /&gt;"MAPILEVEL=" + mailboxNameTextBox.Text + "|;" +&lt;br /&gt;"PROFILE=" + profileTextBox.Text + ";" +&lt;br /&gt;"TABLETYPE=0;" +&lt;br /&gt;"DATABASE=" + System.IO.Path.GetTempPath( );&lt;br /&gt;&lt;br /&gt;// Create the DataAdapter.&lt;br /&gt;OleDbDataAdapter da = new OleDbDataAdapter(sqlText, connectionString);&lt;br /&gt;&lt;br /&gt;// Create and fill the table.&lt;br /&gt;DataTable dt = new DataTable("Inbox");&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;da.Fill(dt);&lt;br /&gt;dataGrid.DataSource = dt.DefaultView;&lt;br /&gt;}&lt;br /&gt;catch(Exception ex)&lt;br /&gt;{&lt;br /&gt;MessageBox.Show("ERROR: " + ex.Message);&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;The .NET data provider for OLE DB does not support OLE DB Version 2.5 interfaces including the Microsoft OLE DB Provider for Exchange. The Jet OLE DB provider can access an Outlook or Exchange store. An example of the connection string:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Microsoft.Jet.OLEDB.4.0;Outlook 9.0;MAPILEVEL=Personal Folders|;&lt;br /&gt;PROFILE=Outlook;TABLETYPE=0;DATABASE=c:\temp;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The connection string attributes-and-value pairs are described in below table.&lt;/div&gt;&lt;h5 class="docTableTitle"&gt;Table1. Outlook or Exchange connection string attributes&lt;/h5&gt;&lt;table border="1" cellpadding="4" cellspacing="0" rules="all" style="margin-left: 10px;"&gt;&lt;colgroup span="2"&gt; &lt;/colgroup&gt;&lt;thead&gt;&lt;tr&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Element&lt;/div&gt;&lt;/th&gt; &lt;th class="docTableHeader"&gt;&lt;br /&gt;&lt;div class="docText"&gt;Description&lt;/div&gt;&lt;/th&gt; &lt;/tr&gt;&lt;/thead&gt; &lt;tbody&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Database name&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;DATABASE=path&lt;/tt&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;With an Identifier of &lt;tt&gt;Outlook&lt;/tt&gt; &lt;tt&gt;9.0&lt;/tt&gt;, the path to store temporary system tables.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;With an Identifier of &lt;tt&gt;Exchange&lt;/tt&gt; &lt;tt&gt;4.0&lt;/tt&gt;, the path and filename to a Microsoft Access database in which to store temporary system tables.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Identifier&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Outlook&lt;/tt&gt; &lt;tt&gt;9.0&lt;/tt&gt; to connect to Outlook 2000 and later.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Exchange&lt;/tt&gt; &lt;tt&gt;4.0&lt;/tt&gt; to connect to Exchange 4.x and 5.x.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Password&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;PASSWORD=password&lt;/tt&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Outlook or Exchange password. This parameter is not required if your network logon password is passed to the Outlook or Exchange server. This parameter is optional.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Profile name&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;PROFILE=profile&lt;/tt&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The name of the Outlook or Exchange profile to use. If this not specified, the default profile is used.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Table path&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;MAPILEVEL=&amp;lt;storage&amp;gt;|&amp;lt;folderPath&amp;gt;&lt;/tt&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;&amp;lt;storage&amp;gt;&lt;/tt&gt; is the exact mailbox name on the server, a personal folder, or public folder as it appears in the Outlook Folder list.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;&amp;lt;folderPath&amp;gt;&lt;/tt&gt; is the path to the folder immediately above the folder to access using the SQL statement. The folder path is required only when accessing folders below the top level of folders within the store; the pipe (&lt;tt&gt;|&lt;/tt&gt;) character is always required. When listing nested folders, separate each folder name with a backslash (&lt;tt&gt;\&lt;/tt&gt;).&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;Table type&lt;/tt&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="docTableCell"&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;TABLETYPE=0&lt;/tt&gt; for folders (default value).&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;&lt;tt&gt;TABLETYPE=1&lt;/tt&gt; for address books.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;&lt;br /&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Connecting+to+Exc+or+Outlook" rel="tag"&gt;Connecting to Exc or Outlook&lt;/a&gt; &lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8806313887496132788?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8806313887496132788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-exchange-or-outlook.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8806313887496132788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8806313887496132788'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-exchange-or-outlook.html' title='Connecting to Exchange or Outlook'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-3775050552556301203</id><published>2008-09-20T20:40:00.002+05:30</published><updated>2008-09-27T17:06:17.695+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to Oracle DB'/><title type='text'>Connecting to an Oracle Database</title><content type='html'>&lt;div class="docText"&gt;You can connect to an Oracle database using either the Oracle .NET data provider or the OLE DB .NET data provider.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains two event handlers:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Oracle Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and opens a connection to an Oracle database using the Oracle .NET data provider. Information about the database is displayed from the properties of the &lt;tt&gt;OracleConnection&lt;/tt&gt; object.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;OLE DB Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and opens a connection to an Oracle database using the OLE DB .NET data provider. Information about the database is displayed from the properties of the &lt;tt&gt;OleDbConnection&lt;/tt&gt; object.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: ConnectOracleForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;using System.Data.OracleClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void oracleProviderButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Connect to Oracle using Microsoft Oracle .NET data provider.&lt;br /&gt;OracleConnection conn = new OracleConnection(&lt;br /&gt;ConfigurationSettings.AppSettings["Oracle_Scott_ConnectString"]);&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = "Connection with ORACLE Provider" +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text += "ConnectionState = " + conn.State +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"DataSource = " + conn.DataSource +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"ServerVersion = " + conn.ServerVersion +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;}&lt;br /&gt;catch(OracleException ex)&lt;br /&gt;{&lt;br /&gt;resultTextBox.Text += "ERROR: " + ex.Message;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;conn.Close( );&lt;br /&gt;resultTextBox.Text += "ConnectionState = " + conn.State;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void oleDbButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Connect to Oracle using OLE DB .NET data provider.&lt;br /&gt;OleDbConnection conn = new OleDbConnection(&lt;br /&gt;ConfigurationSettings.AppSettings["OleDb_Oracle_ConnectString"]);&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = "Connection with OLE DB Provider" +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text += "ConnectionState = " + conn.State +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"DataSource = " + conn.DataSource +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"ServerVersion = " + conn.ServerVersion +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;}&lt;br /&gt;catch(OleDbException ex)&lt;br /&gt;{&lt;br /&gt;resultTextBox.Text += "ERROR: " + ex.Message;&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;conn.Close( );&lt;br /&gt;resultTextBox.Text += "ConnectionState = " + conn.State +&lt;br /&gt;Environment.NewLine;&lt;br /&gt;}        &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;You can access an Oracle database using three different provider types: native Oracle, OLE DB, and ODBC. These alternatives are discussed in the following subsections.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;Native Oracle&lt;/h5&gt;&lt;div class="docText"&gt;The Microsoft Oracle .NET data provider accesses an Oracle database using the Oracle Call Interface (OCI) through Oracle client connectivity software. The provider can access Oracle 7.3.4 or later and requires Oracle 8i Release 3 (8.1.7) or later client software. The classes are located in the &lt;tt&gt;System.Data.OracleClient&lt;/tt&gt; namespace. An example of a connection string using integrated security is shown in the following snippet:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Data Source=myOracleDb;Integrated Security=yes;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;Without integrated security, the connection string is:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Data Source=myOracleDb;User Id=scott;Password=tiger;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The Microsoft Oracle .NET data provider is included with .NET Framework Version 1.1. It is not included with the .NET Framework Version 1.0, but you can download it from &lt;a class="docLink" href="http://msdn.microsoft.com/downloads" target="_blank"&gt;http://msdn.microsoft.com/downloads&lt;/a&gt;. The Oracle .NET data provider can access Oracle8 Release 8.0 or later and requires the Oracle9i Client Release 2 (9.2) or later.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Here are desciptions of available managed providers:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Oracle has released a .NET data provider. It is available for free download from &lt;a class="docLink" href="http://otn.oracle.com/software/tech/windows/odpnet/content.html" target="_blank"&gt;http://otn.oracle.com/software/tech/windows/odpnet/content.html&lt;/a&gt;.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Data Direct Technologies licenses a fully managed provider that does not require client libraries for Oracle8i Release 2 (8.1.6) or later databases. More information is available at &lt;a class="docLink" href="http://www.datadirect-technologies.com/products/dotnet/dotnetindex.asp" target="_blank"&gt;http://www.datadirect-technologies.com/products/dotnet/dotnetindex.asp&lt;/a&gt;.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div class="docText"&gt;Native providers generally perform better than OLE DB or ODBC providers because they are built specifically for the database and because they remove a layer of indirection from the application to the database.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;OLE DB&lt;/h5&gt;&lt;div class="docText"&gt;You can use the OLE DB .NET data provider with the Oracle OLE DB provider (MSDAORA) to access Oracle data not supported by a .NET Oracle provider. An example of the connection string is shown here:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Provider=MSDAORA;Data Source=myOracleDb;User Id=scott;Password=tiger;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The OLE DB provider should be used primarily as a bridge from applications that already use OLE DB. Use a native Oracle .NET data provider where practical.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;ODBC&lt;/h5&gt;&lt;div class="docText"&gt;Finally, the ODBC .NET data provider can connect to an Oracle database. An example of the connection string is shown here:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Driver={Microsoft ODBC for Oracle};Server=myOracleDb;&lt;br /&gt;Trusted_Connection=yes;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;The ODBC .NET data provider should be used primarily as a bridge from applications that already use ODBC. Use a native Oracle .NET data provider where practical.&lt;/div&gt;&lt;br /&gt;&lt;table border="1" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;&lt;br /&gt;&lt;h2 class="docSidebarTitle"&gt;TNSNAMES.ORA&lt;/h2&gt;&lt;br /&gt;&lt;div class="docText"&gt;Oracle uses a configuration file named &lt;i&gt;TNSNAMES.ORA&lt;/i&gt; to locate the Oracle database and determine how to connect to it based on the &lt;tt&gt;Data Source&lt;/tt&gt; or &lt;tt&gt;Database&lt;/tt&gt; attribute in the connection string.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;An example of an entry in the &lt;i&gt;TNSNAMES.ORA&lt;/i&gt; file for the alias &lt;tt&gt;MYORCLDB&lt;/tt&gt; follows:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;MYORCLDB =&lt;br /&gt;(DESCRIPTION =&lt;br /&gt;(ADDRESS_LIST =&lt;br /&gt;(ADDRESS = (PROTOCOL = TCP)&lt;br /&gt;(HOST = myserver)(PORT = 1521))&lt;br /&gt;)&lt;br /&gt;(CONNECT_DATA =&lt;br /&gt;(SERVER = DEDICATED)&lt;br /&gt;(SERVICE_NAME = orcl.myurl.com)&lt;br /&gt;)&lt;br /&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;In this simple example, the connection to the alias &lt;tt&gt;MYORLCDB&lt;/tt&gt; uses TCP/IP on port 1521 (the default) and is running on the computer called &lt;tt&gt;myserver&lt;/tt&gt;. The name of the Oracle service is &lt;tt&gt;ORCL.MYURL.COM&lt;/tt&gt;.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt; &lt;/table&gt;&lt;br /&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Connecting+to+Oracle+DB" rel="tag"&gt;Connecting to Oracle DB&lt;/a&gt; &lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-3775050552556301203?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/3775050552556301203/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-oracle-database.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/3775050552556301203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/3775050552556301203'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-oracle-database.html' title='Connecting to an Oracle Database'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8677102992550929945</id><published>2008-09-20T20:36:00.001+05:30</published><updated>2008-09-27T17:06:59.266+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to SQL Server'/><title type='text'>Connecting to SQL Server Using Integrated Security from ASP.NET</title><content type='html'>&lt;div class="docText"&gt;Connecting to a SQL Server database provides two different authentication modes:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;&lt;i&gt;Windows Authentication&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Uses the current security identity from the Windows NT or Windows 2000 user account to provide authentication information. It does not expose the user ID and password and is the recommended method for authenticating a connection.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;&lt;i&gt;SQL Server Authentication&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Uses a SQL Server login account providing a user ID and password.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;Integrated security requires that the SQL Server is running on the same computer as IIS and that all application users are on the same domain so that their credentials are available to IIS. The following areas of the application need to be configured:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Configure the ASP.NET application so that &lt;tt&gt;Integrated&lt;/tt&gt; &lt;tt&gt;Windows&lt;/tt&gt; &lt;tt&gt;Authentication&lt;/tt&gt; is enabled and &lt;tt&gt;Anonymous&lt;/tt&gt; &lt;tt&gt;Access&lt;/tt&gt; is disabled.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;The &lt;i&gt;web.config&lt;/i&gt; file establishes the authentication mode that the application uses and that the application will run as or impersonate the user. Add the following elements to the &lt;i&gt;web.config&lt;/i&gt; file:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;authentication mode="Windows" /&amp;gt;&lt;br /&gt;&amp;lt;identity impersonate="true" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;The connection string must contain attributes that tell the SQL Server that integrated security is used. Use the &lt;tt&gt;Integrated&lt;/tt&gt; &lt;tt&gt;Security=SSPI&lt;/tt&gt; attribute-and-value pair instead of the &lt;tt&gt;User&lt;/tt&gt; &lt;tt&gt;ID&lt;/tt&gt; and &lt;tt&gt;Password&lt;/tt&gt; attributes in the connection string. The older attribute-and-value pair &lt;tt&gt;Trusted_Connection=Yes&lt;/tt&gt; is also supported.&lt;/div&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Add users and groups from the domain and set their access permissions as required.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="docText"&gt;By default, ASP.NET applications run in the context of a local user &lt;tt&gt;ASPNET&lt;/tt&gt; on IIS. The account has limited permissions and is local to the IIS computer and therefore not recognized as a user on remote computers. To overcome this limitation when SQL Server is not on the same computer as IIS, run the web application in the context of a domain user recognized on both IIS and SQL Server computers.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;In addition to the areas identified where IIS and SQL Server are on the same computer, the following additional items must be configured if the SQL Server is on a different computer:&lt;/div&gt;&lt;ul&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Ensure that the mapped domain user has required privileges to run the web application.&lt;/div&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt; &lt;br /&gt;&lt;div class="docList"&gt;Configure the web application to impersonate the domain user. Add the following elements to the &lt;i&gt;web.config&lt;/i&gt; file for the web application:&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;authentication mode="Windows" /&amp;gt;&lt;br /&gt;&amp;lt;identity impersonate="true" userName="domain\username"&lt;br /&gt;password="myPassword" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8677102992550929945?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8677102992550929945/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-sql-server-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8677102992550929945'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8677102992550929945'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-sql-server-using.html' title='Connecting to SQL Server Using Integrated Security from ASP.NET'/><author><name>Sunita</name><uri>http://www.blogger.com/profile/09126980747092182401</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-446429655135979553</id><published>2008-09-18T00:47:00.002+05:30</published><updated>2008-09-27T17:07:38.681+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Conneting to Named Instance'/><title type='text'>Connecting to a Named Instance of SQL Server or Microsoft Data Engine (MSDE)</title><content type='html'>&lt;div class="docText"&gt;You need to understand what a SQL Server or MSDE named instance is and how to connect to one. The sample code contains a single event handler:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Connect Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and opens a connection to a named instance of a SQL Server. Information about the SQL Server is displayed from the properties of the &lt;tt&gt;SqlConnection&lt;/tt&gt; object.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: ConnectNamedInstanceForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;&lt;br /&gt;SqlConnection conn = new SqlConnection(&lt;br /&gt;ConfigurationSettings.AppSettings["Sql_Msde_ConnectString"]);&lt;br /&gt;&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;// Return some information about the server.&lt;br /&gt;result.Append(&lt;br /&gt;"ConnectionState = " + conn.State + Environment.NewLine +&lt;br /&gt;"DataSource = " + conn.DataSource + Environment.NewLine +&lt;br /&gt;"ConnectionState = " + conn.State + Environment.NewLine +&lt;br /&gt;"ServerVersion=" + conn.ServerVersion +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;} &lt;br /&gt;catch(Exception ex) &lt;br /&gt;{&lt;br /&gt;MessageBox.Show(ex.Message);&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;conn.Close( );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;result.Append("ConnectionState = " + conn.State);&lt;br /&gt;&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;SQL Server 2000 introduced the ability to install multiple copies of SQL Server on a single computer. Only one copy can function as the default instance at any time; it is identified by the network name of the computer on which it is running. All other copies are named instances and are identified by the network name of the computer plus an instance name. The format is &lt;tt&gt;&amp;lt;computerName&amp;gt;\&amp;lt;instanceName&amp;gt;&lt;/tt&gt;. This format is used in the connection string to specify the &lt;tt&gt;Data&lt;/tt&gt; &lt;tt&gt;Source&lt;/tt&gt; attribute for a named instance.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Each instance operates independently of the other instances installed on the same computer. Each instance has its own set of system and user databases that are not shared between instances and it runs within its own security context. The maximum number of instances supported on SQL Server 2000 is 16. The Microsoft Distributed Transaction Coordinator (DTC) and the Microsoft Search services are installed and used simultaneously by every installed instance of SQL Server. Client tools such as Enterprise Manager and Query Analyzer are also shared.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The &lt;tt&gt;System.Data.SqlClient&lt;/tt&gt; class cannot automatically discover the port number of a named instance of SQL Server listening on a port other than the default 1433. To connect to a named instance of SQL Server listening on a custom port, specify the port number following the instance name in the connection string separated by a comma. For, if the named instance &lt;tt&gt;&lt;i&gt;msde01&lt;/i&gt;&lt;/tt&gt; was set up to listen on port 1450, the following connection string might be used:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Data Source=(local)\&lt;b&gt;msde01,1450&lt;/b&gt;;Integrated security=SSPI;&lt;br /&gt;Initial Catalog=Northwind&lt;br /&gt;&lt;/pre&gt;&lt;div class="zoundry_raven_tags" xmlns=""&gt;&lt;br /&gt;Technorati : &lt;a class="ztag" href="http://www.technorati.com/tag/Conneting+to+Named+Instance" rel="tag"&gt;Conneting to Named Instance&lt;/a&gt;  &lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-446429655135979553?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/446429655135979553/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-named-instance-of-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/446429655135979553'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/446429655135979553'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-named-instance-of-sql.html' title='Connecting to a Named Instance of SQL Server or Microsoft Data Engine (MSDE)'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-7976843320066408763</id><published>2008-09-17T20:45:00.002+05:30</published><updated>2008-09-27T17:08:31.894+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting using IP Addr'/><title type='text'>Using an IP Address to Connect to SQL Server</title><content type='html'>&lt;div class="docText"&gt;Use the &lt;tt&gt;Network Address&lt;/tt&gt; and &lt;tt&gt;Network&lt;/tt&gt; &lt;tt&gt;Library&lt;/tt&gt; attributes of the connection string.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The sample code contains a single event handler:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;Connect Button.Click&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Creates and opens a connection to a SQL Server using its IP address. Information about the SQL Server is displayed from the properties of the &lt;tt&gt;SqlConnection&lt;/tt&gt; object.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;div class="docText"&gt;The C# code is shown here.&lt;/div&gt;&lt;h5 class="docExampleTitle"&gt;Example- File: ConnectSqlServerIpAddressForm.cs&lt;/h5&gt;&lt;pre&gt;// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;//  . . . &lt;br /&gt;&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;String connString =&lt;br /&gt;"Network Library=dbmssocn;Network Address=127.0.0.1&lt;br /&gt;;" +&lt;br /&gt;"Integrated security=SSPI;Initial Catalog=Northwind";&lt;br /&gt;&lt;br /&gt;SqlConnection conn = new SqlConnection(connString);&lt;br /&gt;conn.Open( );&lt;br /&gt;&lt;br /&gt;// Return some information about the server.&lt;br /&gt;resultTextBox.Text =&lt;br /&gt;"ConnectionState = " + conn.State + Environment.NewLine +&lt;br /&gt;"DataSource = " + conn.DataSource + Environment.NewLine +&lt;br /&gt;"ServerVersion = " + conn.ServerVersion + Environment.NewLine;&lt;br /&gt;&lt;br /&gt;conn.Close( );&lt;br /&gt;&lt;br /&gt;resultTextBox.Text += "ConnectionState = " + conn.State;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;div class="docText"&gt;SQL Server network libraries are dynamic-link libraries (DLLs) that perform network operations required for client computers and SQL Server computers to communicate. A server can monitor multiple libraries simultaneously; the only requirement is that each network library to be monitored is installed and configured.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Available network libraries for SQL Server 2000 include:&lt;/div&gt;&lt;dl class="docList"&gt;&lt;dt&gt;AppleTalk ADSP&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Allows Apple Macintosh to communicate with SQL Server using native AppleTalk protocol.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Banyan VINES&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Supports Banyan VINES Sequenced Packet Protocol (SPP) across Banyan VINES IP network protocol.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Multiprotocol&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Automatically chooses the first available network protocol to establish a connection generally with performance comparable to using a native network library. TCP/IP Sockets, NWLink IPX/SPX, and Named Pipes are supported.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;Named Pipes&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Interprocess communication (IPC) mechanism provided by SQL Server for communication between clients and servers.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;NWLink IPX/SPX&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;The native protocol of Novell Netware networks.&lt;/div&gt;&lt;/dd&gt;&lt;dt&gt;TCP/IP Sockets&lt;/dt&gt;&lt;dd&gt;&lt;div class="docList"&gt;Uses standard Windows sockets to communicate across the TCP/IP protocol.&lt;/div&gt;&lt;/dd&gt; &lt;/dl&gt;&lt;br /&gt;&lt;div class="docText"&gt;Clustered installations of SQL Server support only Named Pipes and TCP/IP protocols. AppleTalk, Banyan Vines, and Multiprotocol protocols are unavailable if named instances are installed on the server.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;For more information about network libraries and configuring network libraries, see Microsoft SQL Server Books Online.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The use of the SQL Server TCP/IP Sockets improves performance and scalability with high volumes of data. It avoids some security issues associated with named pipes. As with any protocol, the client and the server must be configured to use TCP/IP.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;To connect to SQL Server using an IP address, the TCP/IP network library must be used to connect to the SQL Server. This is done by specifying the library in the connection string as either the attribute &lt;tt&gt;Net&lt;/tt&gt; or &lt;tt&gt;Network Library&lt;/tt&gt; with a value of &lt;tt&gt;&lt;i&gt;dbmssocn&lt;/i&gt;&lt;/tt&gt;. Specify the IP address using the &lt;tt&gt;Data&lt;/tt&gt; &lt;tt&gt;Source&lt;/tt&gt;, &lt;tt&gt;Server&lt;/tt&gt;, &lt;tt&gt;Address&lt;/tt&gt;, &lt;tt&gt;Addr&lt;/tt&gt;, or &lt;tt&gt;Network&lt;/tt&gt; &lt;tt&gt;Address&lt;/tt&gt; parameter. The following connection string demonstrates using an IP address to specify the data source:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Network Library=dbmssocn;Network Address=127.0.0.1;&lt;br /&gt;Integrated security=SSPI;Initial Catalog=Northwind&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;In the example, the IP address is the local machine. This could also be specified as (&lt;tt&gt;local&lt;/tt&gt;). To specify a SQL Server other than a local instance, specify the IP address of the computer on which SQL Server is installed.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Default instances of SQL Server listen on port 1433. Named instances of SQL Server dynamically assign a port number when they are first started. The example above does not specify the port number and therefore uses the default port 1433 of the SQL Server. If the SQL Server is configured to listen on another port, specify the port number following the IP address specified by the &lt;tt&gt;Network&lt;/tt&gt; &lt;tt&gt;Address&lt;/tt&gt; attribute separated by a comma as shown in the following snippet, which connects to a local SQL Server listening on port 1450:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;Network Address=(local),1450&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-7976843320066408763?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/7976843320066408763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-ip-address-to-connect-to-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7976843320066408763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/7976843320066408763'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/using-ip-address-to-connect-to-sql.html' title='Using an IP Address to Connect to SQL Server'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5338832231103909251</id><published>2008-09-17T20:36:00.002+05:30</published><updated>2008-09-27T17:08:48.818+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to Access DB'/><title type='text'>Connecting to an Access Database from ASP.NET</title><content type='html'>&lt;div class="docText"&gt;When a user retrieves a page from an ASP.NET web site, code runs on the server to generate and deliver the page. By default, IIS (Internet Information Server) uses the system account to provide the security context for all processes. This account can access the IIS computer, but is not allowed to access network shares on other computers.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;To allow an ASP.NET application to connect to a Microsoft Access database, IIS must be configured to use an account other than the system account. The new account must be configured to have permission to access all files and folders needed to use the Access database. If the Access database is on a remote computer, the account also requires access to that computer.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The following sections describe how to configure the IIS Server and the Access computer to allow ASP.NET to connect to an Access database.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;Configure IIS&lt;/h5&gt;&lt;div class="docText"&gt;The system account cannot authenticate across a network. Enable impersonation in the &lt;tt&gt;web.config&lt;/tt&gt; file for a given ASP.NET application so that ASP.NET impersonates an account on the Microsoft Access computer with the required access permissions to the Access database. For example:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;identity impersonate="true" userName="domain\username"&lt;br /&gt;password="myPassword" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div class="docText"&gt;This method stores the username and password in clear text on the server. Ensure that IIS is configured to prevent users of the web site from viewing the contents of the &lt;i&gt;web.config&lt;/i&gt; file-this is the default configuration. Other ways to impersonate a user from an ASP page are described in the Microsoft Knowledge Base article Q248187.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The Microsoft Jet engine uses the &lt;i&gt;TEMP&lt;/i&gt; folder on the IIS computer that is accessing the Access database. The user identity requires NTFS (Windows NT File System) full-control permissions on the &lt;i&gt;TEMP&lt;/i&gt; folder. Ensure that the &lt;tt&gt;TEMP&lt;/tt&gt; and &lt;tt&gt;TMP&lt;/tt&gt; environment variables are properly configured.&lt;/div&gt;&lt;h5 class="docSection3Title"&gt;Configure the Access server&lt;/h5&gt;&lt;div class="docText"&gt;On the Access computer, the user account that is used to access the database requires Read, Write, Execute, and Change permissions on the database file. The user identity needs Read, Write, Execute, Delete, and Change permissions on the folder containing the database files. The user account requires permissions to access the share that contains the database file and folders.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;The user account must be recognized by the Access computer. For a domain user account, add it to the permissions list on both computers. For a user account local to the IIS computer, create a duplicate account on the Access computer with the same name and password.&lt;/div&gt;&lt;br /&gt;&lt;div class="docText"&gt;Grant the user account Log on Locally and Access this Computer from the Network permission to access the computer in the local security policy. These permissions are assigned within the &lt;tt&gt;Security Settings&lt;/tt&gt; \&lt;tt&gt;Local&lt;/tt&gt; &lt;tt&gt;Policies&lt;/tt&gt;\&lt;tt&gt;User&lt;/tt&gt; &lt;tt&gt;Rights&lt;/tt&gt; &lt;tt&gt;Assignment&lt;/tt&gt; node in the Local Security Policy tool.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5338832231103909251?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5338832231103909251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-access-database-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5338832231103909251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5338832231103909251'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-access-database-from.html' title='Connecting to an Access Database from ASP.NET'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-3458052212666077641</id><published>2008-09-17T12:56:00.001+05:30</published><updated>2008-09-27T17:09:14.676+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to Secured DB'/><title type='text'>Connecting to a Secured Access Database</title><content type='html'>Use the Jet OLEDB:System Database attribute in the connection string to specify the path and filename of the workgroup information file or system database.&lt;br /&gt;The sample code contains a single event handler:&lt;a href="" name="adonetckbk-CHP-1-ITERM-1901"&gt;&lt;/a&gt;&lt;br /&gt;Connect Button.Click&lt;br /&gt;Creates and opens a connection to &lt;a href="" name="adonetckbk-CHP-1-ITERM-1901"&gt;&lt;/a&gt;a Microsoft Access database secured with user-level security and a workgroup file using the OLE DB .NET data provider. Information about the database is displayed from the properties of the OleDbConnection object.&lt;br /&gt;&lt;br /&gt;The &lt;a href="" name="adonetckbk-CHP-1-ITERM-1902"&gt;&lt;/a&gt;C# code is shown here.&lt;br /&gt;&lt;br /&gt;Example- File: AccessSecureForm.cs// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;// . . .&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;// Build the connection string with security information.&lt;br /&gt;String connectionString =&lt;br /&gt;ConfigurationSettings.AppSettings["MsAccess_ConnectString"] +&lt;br /&gt;@"Jet OLEDB:System database=" +&lt;br /&gt;ConfigurationSettings.AppSettings["MsAccess_SecureMdw_Filename"] +&lt;br /&gt;";" + "User ID=" + userIdTextBox.Text + ";" +&lt;br /&gt;"Password=" + passwordTextBox.Text + ";" +&lt;br /&gt;Environment.NewLine + Environment.NewLine;&lt;br /&gt;result.Append(connectionString);&lt;br /&gt;// Create the connection.&lt;br /&gt;OleDbConnection conn = new OleDbConnection(connectionString);&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;// Attempt to open the connection.&lt;br /&gt;conn.Open( );&lt;br /&gt;result.Append(&lt;br /&gt;"Connection State: " + conn.State + Environment.NewLine +&lt;br /&gt;"OLE DB Provider: " + conn.Provider +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"Server Version: " + conn.ServerVersion +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;conn.Close( );&lt;br /&gt;result.Append("Connection State: " + conn.State +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;}&lt;br /&gt;catch(System.Data.OleDb.OleDbException ex)&lt;br /&gt;{&lt;br /&gt;result.Append("ERROR: " + ex.Message);&lt;br /&gt;}&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;}&lt;a href="" name="adonetckbk-CHP-1-SECT-4.3"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Microsoft Access user-level security requires an additional file—the workgroup information or MDW&lt;a href="" name="adonetckbk-CHP-1-ITERM-1903"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1904"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1905"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1906"&gt;&lt;/a&gt;file—in addition to the database or MDB file. This file contains the user and group information for the secured database while the actual permissions are stored in the database file.&lt;br /&gt;When you connect to a secured Jet database, the user ID and password are validated against the values in the MDW file. The permissions are obtained from the MDB file. To connect, the location of both the database file and the workgroup file must be supplied.&lt;br /&gt;&lt;br /&gt;The OLE DB provider for Microsoft Jet has several provider-specific connection string attributes in addition to those defined by ADO.NET. To open a database secured by Microsoft Access user-level security, use the Jet OLEDB:System Database attribute in the connection string to specify the path and filename of the workgroup information file or system database. This &lt;a href="" name="adonetckbk-CHP-1-ITERM-1907"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1908"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1909"&gt;&lt;/a&gt;corresponds to the OLE DB property &lt;a href="" name="adonetckbk-CHP-1-ITERM-1910"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1911"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1912"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1913"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1914"&gt;&lt;/a&gt;DBPROP_JETOLEDB_SYSDBPATH.&lt;a href="mk:@MSITStore:C:/Documents%20and%20Settings/admin/Desktop/BLOG%20s/Oreilly.ADO.Dot.NET.Cookbook.eBook-LiB.chm::/0596004397_23061533.html"&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-3458052212666077641?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/3458052212666077641/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-secured-access-database.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/3458052212666077641'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/3458052212666077641'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-secured-access-database.html' title='Connecting to a Secured Access Database'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-8774664343560898736</id><published>2008-09-17T12:42:00.001+05:30</published><updated>2008-09-27T17:09:27.513+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to Pwd DB'/><title type='text'>Connecting to a Password-Protected Access Database</title><content type='html'>Use the Jet OLEDB:Database Password attribute in the connection string to specify the password.&lt;br /&gt;&lt;br /&gt;The sample code contains a single event handler:&lt;br /&gt;Connect Button.Click&lt;br /&gt;Creates and opens a connection to a password-secured Microsoft Access database using the OLE DB .NET data provider. Information about the database is displayed from the properties of the OleDbConnection object.&lt;br /&gt;The C# code is shown here.&lt;br /&gt;&lt;br /&gt;&lt;a href="" name="adonetckbk-CHP-1-EX-3"&gt;&lt;/a&gt;Example- File: AccessPasswordForm.cs// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.OleDb;&lt;br /&gt;// . . .&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;StringBuilder result = new StringBuilder( );&lt;br /&gt;// Build the connections string incorporating the password.&lt;br /&gt;String connectionString =&lt;br /&gt;ConfigurationSettings.AppSettings["MsAccess_Secure_ConnectString"]+&lt;br /&gt;"Jet OLEDB:Database Password=" + passwordTextBox.Text + ";";&lt;br /&gt;result.Append("ConnectionString: " + Environment.NewLine +&lt;br /&gt;connectionString + Environment.NewLine + Environment.NewLine);&lt;br /&gt;OleDbConnection conn = new OleDbConnection(connectionString);&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;conn.Open( );&lt;br /&gt;// Retrieve some database information.&lt;br /&gt;result.Append(&lt;br /&gt;"Connection State: " + conn.State + Environment.NewLine +&lt;br /&gt;"OLE DB Provider: " + conn.Provider +&lt;br /&gt;Environment.NewLine +&lt;br /&gt;"Server Version: " + conn.ServerVersion +&lt;br /&gt;Environment.NewLine);&lt;br /&gt;conn.Close( );&lt;br /&gt;result.Append("Connection State: " + conn.State);&lt;br /&gt;}&lt;br /&gt;catch(System.Data.OleDb.OleDbException ex)&lt;br /&gt;{&lt;br /&gt;conn.Close( );&lt;br /&gt;result.Append("ERROR: " + ex.Message);&lt;br /&gt;}&lt;br /&gt;resultTextBox.Text = result.ToString( );&lt;br /&gt;}&lt;a href="" name="adonetckbk-CHP-1-SECT-3.3"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;A Microsoft Access database password requires that users enter a password to obtain access to the database and database objects. This is also known as share-level security. A password does not allow groups or users to have distinct levels of access or permissions. Anyone with the password has unrestricted access to the database.&lt;br /&gt;The Set&lt;a href="" name="adonetckbk-CHP-1-ITERM-1885"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1886"&gt;&lt;/a&gt;Database command from the Tools Security menu is used to set up a database password.&lt;br /&gt;&lt;br /&gt;The OLE DB provider for Microsoft Jet has several provider-specific connection string attributes in addition to those defined by ADO.NET. To open a database secured by a Microsoft Access database password, use the Jet OLEDB:Database Password attribute in the connection string to specify the password. This corresponds&lt;a href="" name="adonetckbk-CHP-1-ITERM-1887"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1888"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1889"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1890"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1891"&gt;&lt;/a&gt;to the OLE DB property DBPROP_JETOLEDB_DATABASEPASSWORD.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-8774664343560898736?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/8774664343560898736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-password-protected-access.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8774664343560898736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/8774664343560898736'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-password-protected-access.html' title='Connecting to a Password-Protected Access Database'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-5267265810916932049</id><published>2008-09-17T12:34:00.001+05:30</published><updated>2008-09-27T17:09:42.185+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to MS Excel'/><title type='text'>Connecting to a Microsoft Excel Workbook</title><content type='html'>Use the OLE DB Jet &lt;a href="" name="adonetckbk-CHP-1-ITERM-1851"&gt;&lt;/a&gt;provider to create, access, and modify data stored in an Excel workbook.&lt;br /&gt;The sample code contains two event handlers:&lt;br /&gt;Form.Load&lt;br /&gt;Creates an OleDbDataAdapter that uses the Jet OLE DB provider to access an Excel workbook. Custom insert and update logic is created for the DataAdapter. A DataTable is filled from the first worksheet, Sheet1, in the Excel workbook and the default view of the table is bound to a data grid on the form.&lt;br /&gt;Update Button.Click&lt;br /&gt;Uses the DataAdapter created in the Form.Load event handler to update the Excel workbook with the programmatic changes.&lt;br /&gt;&lt;br /&gt;The C# code is shown here.&lt;br /&gt;Example- File: ExcelForm.cs// Namespaces, Variables, and Constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;private OleDbDataAdapter da;&lt;br /&gt;private DataTable dt;&lt;br /&gt;// . . .&lt;br /&gt;private void ExcelForm_Load(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Create the DataAdapter.&lt;br /&gt;da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",&lt;br /&gt;ConfigurationSettings.AppSettings["Excel_0115_ConnectString"]);&lt;br /&gt;// Create the insert command.&lt;br /&gt;String insertSql = "INSERT INTO [Sheet1$] " +&lt;br /&gt;"(CategoryID, CategoryName, Description) " +&lt;br /&gt;"VALUES (?, ?, ?)";&lt;br /&gt;da.InsertCommand =&lt;br /&gt;new OleDbCommand(insertSql, da.SelectCommand.Connection);&lt;br /&gt;da.InsertCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0,&lt;br /&gt;"CategoryID");&lt;br /&gt;da.InsertCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15,&lt;br /&gt;"CategoryName");&lt;br /&gt;da.InsertCommand.Parameters.Add("@Description", OleDbType.VarChar, 100,&lt;br /&gt;"Description");&lt;br /&gt;// Create the update command.&lt;br /&gt;String updateSql = "UPDATE [Sheet1$] " +&lt;br /&gt;"SET CategoryName=?, Description=? " +&lt;br /&gt;"WHERE CategoryID=?";&lt;br /&gt;da.UpdateCommand =&lt;br /&gt;new OleDbCommand(updateSql, da.SelectCommand.Connection);&lt;br /&gt;da.UpdateCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15,&lt;br /&gt;"CategoryName");&lt;br /&gt;da.UpdateCommand.Parameters.Add("@Description", OleDbType.VarChar, 100,&lt;br /&gt;"Description");&lt;br /&gt;da.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0,&lt;br /&gt;"CategoryID");&lt;br /&gt;// Fill the table from the Excel spreadsheet.&lt;br /&gt;dt = new DataTable( );&lt;br /&gt;da.Fill(dt);&lt;br /&gt;// Define the primary key.&lt;br /&gt;dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};&lt;br /&gt;// Records can only be inserted using this technique.&lt;br /&gt;dt.DefaultView.AllowDelete = false;&lt;br /&gt;dt.DefaultView.AllowEdit = true;&lt;br /&gt;dt.DefaultView.AllowNew = true;&lt;br /&gt;// Bind the default view of the table to the grid.&lt;br /&gt;dataGrid.DataSource = dt.DefaultView;&lt;br /&gt;}&lt;br /&gt;private void updateButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;da.Update(dt);&lt;br /&gt;}&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You can use the &lt;a href="" name="adonetckbk-CHP-1-ITERM-1852"&gt;&lt;/a&gt;Jet OLE DB provider to access Microsoft Excel as a data source. The Jet database engine can access other database file formats through &lt;a href="" name="adonetckbk-CHP-1-ITERM-1853"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1854"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1855"&gt;&lt;/a&gt;Indexed Sequential Access Method (ISAM) drivers specified in the Extended Properties attribute of the connection. Excel 2000 and 2002 are supported with the Excel 8.0 source database type as shown in the following example:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myBook.xls;&lt;br /&gt;Extended Properties="Excel 8.0;HDR=YES";&lt;br /&gt;&lt;br /&gt;The Extended Properties&lt;a href="" name="adonetckbk-CHP-1-ITERM-1856"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1857"&gt;&lt;/a&gt;attribute can, in addition to the ISAM version property, specify whether or not tables include headers as field names in the first row of a range using an HDR attribute.&lt;br /&gt;&lt;br /&gt;There are three ways in which you can&lt;a href="" name="adonetckbk-CHP-1-ITERM-1858"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1859"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1860"&gt;&lt;/a&gt;reference Excel workbook data within a SQL statement:&lt;br /&gt;Specify the worksheet name followed by a dollar sign to access the entire range used in the worksheet:SELECT * FROM [MySheet$]&lt;br /&gt;Specify a range explicitly using cells:SELECT * FROM [MySheet$A1:E5]&lt;br /&gt;Specify a range with a defined name, as shown in the solution:SELECT * FROM MyRange&lt;br /&gt;The following subsections discuss how to use Excel as an ADO.NET data source.&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3.1"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Create table&lt;/b&gt;&lt;br /&gt;The CREATE TABLE&lt;a href="" name="adonetckbk-CHP-1-ITERM-1861"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1862"&gt;&lt;/a&gt;command will create a table in an Excel workbook. The workbook for the connection will be created if it does not exist. For example:CREATE TABLE MySheet (Field1 char(10), Field2 float, Field3 date)&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3.2"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Create data&lt;br /&gt;&lt;/b&gt;You can use the INSERT&lt;a href="" name="adonetckbk-CHP-1-ITERM-1863"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1864"&gt;&lt;/a&gt;command, either static or parameterized,&lt;a href="" name="adonetckbk-CHP-1-ITERM-1865"&gt;&lt;/a&gt; to &lt;a href="" name="adonetckbk-CHP-1-ITERM-1866"&gt;&lt;/a&gt;insert data into a worksheet or range:INSERT INTO [MySheet$] (Field1, Field2, Field3)&lt;br /&gt;VALUES ('testdata', 1.234, '09/28/1979');&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3.3"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Retrieve data&lt;/b&gt;&lt;br /&gt;Use either a DataAdapter&lt;a href="" name="adonetckbk-CHP-1-ITERM-1867"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1868"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1869"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1870"&gt;&lt;/a&gt;or a DataReader to retrieve data from an Excel workbook. Create a SQL SELECT statement referencing a worksheet or a range in an Excel workbook and execute the statement to fill a DataSet using a DataAdapter or to create a DataReader. For example:SELECT * FROM [MySheet$]&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3.4"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Update data&lt;br /&gt;&lt;/b&gt;The UPDATE&lt;a href="" name="adonetckbk-CHP-1-ITERM-1871"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1872"&gt;&lt;/a&gt;command, either static or parameterized, can update data in a worksheet or range. For example:UPDATE [MySheet$]&lt;br /&gt;SET Field2 = '2.345',&lt;br /&gt;Field3 = '10/18/1964'&lt;br /&gt;WHERE&lt;br /&gt;Field1 = 'testdata'&lt;a href="" name="adonetckbk-CHP-1-SECT-2.3.5"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Delete data&lt;/b&gt;&lt;br /&gt;The Jet OLE DB provider does not allow DELETE operations. An error will be raised if an attempt is made to execute a&lt;a href="" name="adonetckbk-CHP-1-ITERM-1873"&gt;&lt;/a&gt; &lt;a href="" name="adonetckbk-CHP-1-ITERM-1874"&gt;&lt;/a&gt;DELETE statement &lt;a href="" name="adonetckbk-CHP-1-ITERM-1875"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1876"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1877"&gt;&lt;/a&gt;affecting one or more records.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-5267265810916932049?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/5267265810916932049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-microsoft-excel-workbook.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5267265810916932049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/5267265810916932049'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-microsoft-excel-workbook.html' title='Connecting to a Microsoft Excel Workbook'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-597990716815320904.post-2433166496459039222</id><published>2008-09-17T12:29:00.001+05:30</published><updated>2008-09-27T17:09:52.872+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Connecting to ODBC'/><title type='text'>Connecting to an ODBC Data Source</title><content type='html'>Use the ODBC .NET data provider to&lt;a href="" name="adonetckbk-CHP-1-ITERM-1842"&gt;&lt;/a&gt; access &lt;a href="" name="adonetckbk-CHP-1-ITERM-1843"&gt;&lt;/a&gt;data exposed through an ODBC driver.&lt;br /&gt;The sample code contains a single event handler:&lt;br /&gt;Connect Button.Click&lt;br /&gt;Creates an OdbcDataAdapter and uses it to fill a DataTable with the Category table from the Northwind sample database. The default view of the table is bound to a data grid on the form.&lt;br /&gt;&lt;br /&gt;The C# code is shown here.&lt;br /&gt;Example File: OdbcConnectForm.cs// Namespaces, variables, and constants&lt;br /&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.Odbc;&lt;br /&gt;// . . .&lt;br /&gt;private void connectButton_Click(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Create the DataAdapter.&lt;br /&gt;String sqlSelect = "SELECT CategoryID, CategoryName, Description " +&lt;br /&gt;"FROM Categories";&lt;br /&gt;OdbcDataAdapter da = new OdbcDataAdapter(sqlSelect,&lt;br /&gt;ConfigurationSettings.AppSettings["Odbc_ConnectString"]);&lt;br /&gt;// Create the table, fill it, and bind the default view to the grid.&lt;br /&gt;DataTable dt = new DataTable( );&lt;br /&gt;da.Fill(dt);&lt;br /&gt;dataGrid.DataSource = dt.DefaultView;&lt;br /&gt;}&lt;a href="" name="adonetckbk-CHP-1-SECT-1.3"&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The ODBC .NET data provider communicates with native ODBC drivers through COM interop (.NET's interoperability layer for COM). The following ODBC providers are guaranteed to be compatible with the ODBC.NET data provider:&lt;br /&gt;Microsoft SQL Server ODBC Driver&lt;br /&gt;Microsoft ODBC Driver for Oracle&lt;br /&gt;Microsoft Access (Jet) ODBC Driver&lt;br /&gt;&lt;br /&gt;The .NET data provider for ODBC connects to ODBC data sources through the OdbcConnection object. The ODBC driver connection string is specified using the ConnectionString property. It includes settings needed to establish the connection to the data source. The connection string format closely matches the ODBC connection string format. Additionally, you can specify an ODBC data source name (DSN) or file DSN by setting the ConnectionString attribute to "DSN=myDSN" or "FileDSN=myFileDSN". For more information about specifying ODBC connection strings, see the topic "SQLDriverConnect" in the ODBC Programmer's Reference within MSDN Library.&lt;br /&gt;Visual Studio also supports creating ODBC data source connections visually:&lt;br /&gt;Create a data connection in Server Explorer and drag it onto a form or design surface. Configure the OdbcConnection object that appears in the component tray.&lt;br /&gt;Drag an OdbcConnection from the Data tab of the Toolbox onto a form or design surface. Configure the ConnectionString property in the Properties window of the OdbcConnection object that appears.&lt;br /&gt;&lt;br /&gt;The .NET ODBC data provider requires a reference to the System.Data.Odbc namespace in .NET Framework Version 1.1. In Version 1.0, the namespace is Microsoft.Data.Odbc. Add a .NET Reference to Microsoft.Data.Odbc.dll &lt;a href="" name="adonetckbk-CHP-1-ITERM-1844"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1845"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1846"&gt;&lt;/a&gt;&lt;a href="" name="adonetckbk-CHP-1-ITERM-1847"&gt;&lt;/a&gt;for a .NET Framework Version 1.0 project.&lt;br /&gt;The .NET ODBC .NET data provider ships with .NET Framework Version 1.1. The data provider can be downloaded from &lt;a class="docLink" href="http://msdn.microsoft.com/downloads" target="_blank"&gt;http://msdn.microsoft.com/downloads&lt;/a&gt; for .NET Framework Version 1.0.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/597990716815320904-2433166496459039222?l=adodotnetslackers.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://adodotnetslackers.blogspot.com/feeds/2433166496459039222/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-odbc-data-source.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2433166496459039222'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/597990716815320904/posts/default/2433166496459039222'/><link rel='alternate' type='text/html' href='http://adodotnetslackers.blogspot.com/2008/09/connecting-to-odbc-data-source.html' title='Connecting to an ODBC Data Source'/><author><name>vijay</name><uri>http://www.blogger.com/profile/03249092863649882869</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
