TEST SNIPPET- DataAdapter Handler

Some time ago I wrote a VB.NET "DataAdapter Handler". I am testing out our CodeSnippet utility, and this is a c# revision of same. You can pass in a DataTable, and the names of update, insert and delete procs, and it takes care of the rest.

//DataAdapterHandler - C# version
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace CNI.AdminUI
{


public class DAHandler
{
// Methods
public DAHandler()
{
}

private static SqlCommand CreateDeleteViaSPCommand(string spName, string strConn)
{
IEnumerator enumerator1=null;
SqlConnection connection1 = new SqlConnection(strConn);
SqlCommand command1 = new SqlCommand(spName, connection1);
command1.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder builder1 = new SqlCommandBuilder();
connection1.Open();
SqlCommandBuilder.DeriveParameters(command1);
connection1.Close();

try
{
enumerator1 = command1.Parameters.GetEnumerator();
while (enumerator1.MoveNext())
{
SqlParameter parameter1 = (SqlParameter) enumerator1.Current;
parameter1.SourceVersion = DataRowVersion.Current;
parameter1.SourceColumn = parameter1.ParameterName.Remove(0, 1);
}
}
finally
{
if (enumerator1 is IDisposable)
{
((IDisposable) enumerator1).Dispose();
}
}
return command1;
}

private static SqlCommand CreateInsertViaSPCommand(string spName, string strConn)
{
IEnumerator enumerator1=null;
SqlConnection connection1 = new SqlConnection(strConn);
SqlCommand command1 = new SqlCommand(spName, connection1);
command1.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder builder1 = new SqlCommandBuilder();
connection1.Open();
SqlCommandBuilder.DeriveParameters(command1);
connection1.Close();
try
{
enumerator1 = command1.Parameters.GetEnumerator();
while (enumerator1.MoveNext())
{
SqlParameter parameter1 = (SqlParameter) enumerator1.Current;
parameter1.SourceVersion = DataRowVersion.Current;
parameter1.SourceColumn = parameter1.ParameterName.Remove(0, 1);
}
}
finally
{
if (enumerator1 is IDisposable)
{
((IDisposable) enumerator1).Dispose();
}
}
return command1;
}

private static SqlCommand CreateUpdateViaSPCommand(string spName, string strConn)
{
IEnumerator enumerator1=null;
SqlConnection connection1 = new SqlConnection(strConn);
SqlCommand command1 = new SqlCommand(spName, connection1);
command1.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder builder1 = new SqlCommandBuilder();
connection1.Open();
SqlCommandBuilder.DeriveParameters(command1);
connection1.Close();
try
{
enumerator1 = command1.Parameters.GetEnumerator();
while (enumerator1.MoveNext())
{
SqlParameter parameter1 = (SqlParameter) enumerator1.Current;
parameter1.SourceVersion = DataRowVersion.Current;
parameter1.SourceColumn = parameter1.ParameterName.Remove(0, 1);
}
}
finally
{

if (enumerator1 is IDisposable)
{
((IDisposable) enumerator1).Dispose();
}

}
return command1;
}

public static bool SubmitChanges(ref DataSet DS, string ConnectionString, string spUpDateName, string spInsertName, string spDeleteName)
{

DataTable table1 = DS.Tables[0];
DAHandler.SubmitChanges(ref table1, ConnectionString, spUpDateName, spInsertName, spDeleteName);
return true;
}

public static bool SubmitChanges(ref DataTable tbl, string ConnectionString, string spUpDateName, string spInsertName, string spDeleteName)
{
try
{
SqlDataAdapter adapter1 = new SqlDataAdapter();
if (spUpDateName!= "")
{
adapter1.UpdateCommand = DAHandler.CreateUpdateViaSPCommand(spUpDateName, ConnectionString);
}
if (spInsertName!= "")
{
adapter1.InsertCommand = DAHandler.CreateInsertViaSPCommand(spInsertName, ConnectionString);
}
if (spDeleteName!="")
{
adapter1.DeleteCommand = DAHandler.CreateDeleteViaSPCommand(spDeleteName, ConnectionString);
}
adapter1.ContinueUpdateOnError = true;
adapter1.Update(tbl);
}
catch
{

throw;

}
return true;
}

}
}


Submission Date:  9/23/2005 4:07:03 PM
Submitted By:  Peter Bromberg
My Home Page:  http://www.eggheadcafe.com

By Peter Bromberg   Popularity  (231 Views)