An Example with Several SQL Statements
This is the listing for the ClassMain.cs file, the main file of our little sample Console Application. A few of the tabs have been removed to try to fit all the code into the space of the pages here.
using
System;using System.Data;
using System.Data.SqlClient;
namespace Godel.ADONET_Connected
{
class ClassMain
{
static void Main(string[] args)
{
DataSet SampleDS = new DataSet( "SampleDS" );
string ConnectionString = "Data Source=localhost; InitialCatalog=Northwind; User id=sa; Password=;";
SqlConnection Connection = new SqlConnection( ConnectionString );
SqlDataAdapter SampleDA = new SqlDataAdapter( "SELECT RegionID,
RegionDescription FROM Region", Connection );
DataRow TempRow;
// now lets provide SQL statements that support the rest of the
// UPDATE, DELETE, and INSERT functionality.
// -- UPDATE --
qlCommand UpdateCommand = new SqlCommand( "UPDATE Region SET
RegionDescription = @RegionDescription WHERE RegionID =
@RegionID", Connection );
SqlParameter UpdateKeyParam = new SqlParameter( "@RegionID",
SqlDbType.Int );
UpdateKeyParam.SourceColumn = "RegionID";
UpdateKeyParam.SourceVersion = DataRowVersion.Original;
UpdateCommand.Parameters.Add( "@RegionDescription", SqlDbType.NChar,
50, "RegionDescription" );
UpdateCommand.Parameters.Add( UpdateKeyParam );
SampleDA.UpdateCommand = UpdateCommand;
// -- DELETE --
SqlCommand DeleteCommand = new SqlCommand( "DELETE Region WHERE RegionID
= @RegionID", Connection );
SqlParameter DeleteKeyParam = new SqlParameter( "@RegionID",
SqlDbType.Int );
DeleteKeyParam.SourceColumn = "RegionID";
DeleteKeyParam.SourceVersion = DataRowVersion.Original;
DeleteCommand.Parameters.Add( DeleteKeyParam );
SampleDA.DeleteCommand = DeleteCommand;
// -- INSERT --
SqlCommand InsertCommand = new SqlCommand( "INSERT INTO
Region(RegionDescription, RegionID) VALUES(@RegionDescription,
@RegionID)", Connection );
InsertCommand.Parameters.Add( "@RegionDescription", SqlDbType.NChar, 50,
"RegionDescription" );
InsertCommand.Parameters.Add( "@RegionID", SqlDbType.Int, 4,
"RegionID" );
SampleDA.InsertCommand = InsertCommand;
// now load the dataset with the results of our SQL Query.
// notice that we're not explicitly opening our conection. Our
// DataAdapter is doing all that work for us, closing it as soon as it
// completes its task.
SampleDA.Fill( SampleDS, "Region" );
// create a new row
// the Region table doesn't have an autonumbering identity,
// so we have to supply our own region ID.
DataRow NewRow;
NewRow = SampleDS.Tables["Region"].NewRow();
NewRow["RegionDescription"] = "Central";
NewRow["RegionID"] = 5;
Console.WriteLine("New Row Created using NewRow(), RowState is: {0}",NewRow.RowState );
SampleDS.Tables["Region"].Rows.Add( NewRow );
Console.WriteLine("New Row Added to Table RowState is: {0}",
NewRow.RowState );
// modify the first row
TempRow = SampleDS.Tables["Region"].Rows[0];
Console.WriteLine("Modifying First Row, Pre-Change State is: {0}",
TempRow.RowState );
TempRow["RegionDescription"] = "Reeeeeaalllly Far East";
Console.WriteLine("Modifying First Row, Post-Change State is: {0}",
TempRow.RowState );
// call the update method to save the new row and update the first
Console.WriteLine("Calling Update() to Commit New Row and First Row
Change.");
SampleDA.Update( SampleDS, "Region" );
// delete the second row
Console.WriteLine("Deleting Our New Row, Post-Delete State is: {0}",
NewRow.RowState );
NewRow.Delete();
Console.WriteLine("Deleting Our New Row, Post-Delete State is: {0}",
NewRow.RowState );
// now call the update method.
Console.WriteLine("Calling Update() - this will post ALL of our changes,
not just the update.");
SampleDA.Update( SampleDS, "Region" );
Console.WriteLine
("Region Table after Update()\n------------------------------");
foreach (DataRow tRow in SampleDS.Tables["Region"].Rows )
{
Console.WriteLine( tRow["RegionDescription"] );
SampleDS.Dispose();SampleDA. Dispose ();
}
}
}
There is a way to get VS to build all these commands for us, but it will only work if VS is connected directly to the source to interrogate it. If we were working on an n-tier solution where VS only sees derived classes, we'd have to do this manually anyway. It's worth noting here than when creating commands to commit changes for the DataAdapter, it is important to indicate the source column. You can do that by setting the SourceColumn property on the Parameter object, or by passing it as the argument immediately following the parameter's data type size.

Join the conversation! Your thoughts help the community grow.