PC Review


Reply
Thread Tools Rate Thread

Checking to see if a column exists in a datatable

 
 
RSH
Guest
Posts: n/a
 
      16th Jun 2006
Hi,

Iam struggling with an application where I am trying to transfer a datarow
from one sql server to another instance of sql server. The schmeas may be
slightly different and I am getting an exception when they are different.
Is there anyway i can modify the code below so that if the schemas are
different I can drop the offending column in the appropriate datatable?

Thanks,
Ron

SqlConnection cnProductionSQL = new SqlConnection("Data Source=" +
cmbSourceServer.SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master");

cnProductionSQL.Open();

String strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlConnection cnDevelopmentSQL = new SqlConnection("Data Source=" +
cmbDestinationServer.SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master");

cnDevelopmentSQL.Open();

String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";



SqlDataAdapter daProduction = new SqlDataAdapter(strProductionSQL,
cnProductionSQL);

DataSet dsProduction = new DataSet();

daProduction.Fill(dsProduction, "Companies");

SqlCommandBuilder cbProduction = new SqlCommandBuilder(daProduction);

SqlDataAdapter daDevelopment = new SqlDataAdapter(strDevelopmentSQL,
cnDevelopmentSQL);

DataSet dsDevelopment = new DataSet();

daDevelopment.Fill(dsDevelopment, "Companies");

SqlCommandBuilder cbDevelopment = new SqlCommandBuilder(daDevelopment);

if (dsProduction.Tables["Companies"].Rows.Count > 0)

{

DataRow drProd;

drProd = dsProduction.Tables["Companies"].Rows[0];

if (dsDevelopment.Tables["Companies"].Rows.Count > 0)

{

// Update if row exists

DataRow drDev;

drDev = dsDevelopment.Tables["Companies"].Rows[0];

drDev.BeginEdit();

for (Int16 i = 0; i < drProd.ItemArray.Length; i++)

{


if (drDev.Table.Columns[i].ToString() == drProd.Table.Columns[i].ToString())

{

drDev[i] = drProd[i];

}

}

drDev.EndEdit();





Console.WriteLine(dsDevelopment.HasChanges(DataRowState.Modified));

if (dsDevelopment.HasChanges(DataRowState.Modified))

{

daDevelopment.Update(dsDevelopment, "Companies");

}

}

else

{

//Insert If row doesn't exist

DataRow drDevAdd;

drDevAdd = dsDevelopment.Tables["Companies"].NewRow();

for (Int16 i = 0; i < drProd.ItemArray.Length; i++)

{

if (drDevAdd.Table.Columns[i].ToString() ==
drProd.Table.Columns[i].ToString())

{

drDevAdd[i] = drProd[i];

}

}

dsDevelopment.Tables["Companies"].Rows.Add(drDevAdd);

daDevelopment.Update(dsDevelopment, "Companies");

}

}

}


 
Reply With Quote
 
 
 
 
Balasubramanian Ramanathan
Guest
Posts: n/a
 
      16th Jun 2006
You have already posted this....its better to databale.merge instead of
processing every rows. it has got missingschemaaction...there you can ignore
the column which does't exist. Take a look at DataTable.Merge method in
MSDN
"RSH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Iam struggling with an application where I am trying to transfer a datarow
> from one sql server to another instance of sql server. The schmeas may be
> slightly different and I am getting an exception when they are different.
> Is there anyway i can modify the code below so that if the schemas are
> different I can drop the offending column in the appropriate datatable?
>
> Thanks,
> Ron
>
> SqlConnection cnProductionSQL = new SqlConnection("Data Source=" +
> cmbSourceServer.SelectedItem + "; Integrated Security=SSPI; Initial
> Catalog=Master");
>
> cnProductionSQL.Open();
>
> String strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
> CompanyID ='" + strSQLDestDatabase + "'";
>
> SqlConnection cnDevelopmentSQL = new SqlConnection("Data Source=" +
> cmbDestinationServer.SelectedItem + "; Integrated Security=SSPI; Initial
> Catalog=Master");
>
> cnDevelopmentSQL.Open();
>
> String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
> CompanyID ='" + strSQLDestDatabase + "'";
>
>
>
> SqlDataAdapter daProduction = new SqlDataAdapter(strProductionSQL,
> cnProductionSQL);
>
> DataSet dsProduction = new DataSet();
>
> daProduction.Fill(dsProduction, "Companies");
>
> SqlCommandBuilder cbProduction = new SqlCommandBuilder(daProduction);
>
> SqlDataAdapter daDevelopment = new SqlDataAdapter(strDevelopmentSQL,
> cnDevelopmentSQL);
>
> DataSet dsDevelopment = new DataSet();
>
> daDevelopment.Fill(dsDevelopment, "Companies");
>
> SqlCommandBuilder cbDevelopment = new SqlCommandBuilder(daDevelopment);
>
> if (dsProduction.Tables["Companies"].Rows.Count > 0)
>
> {
>
> DataRow drProd;
>
> drProd = dsProduction.Tables["Companies"].Rows[0];
>
> if (dsDevelopment.Tables["Companies"].Rows.Count > 0)
>
> {
>
> // Update if row exists
>
> DataRow drDev;
>
> drDev = dsDevelopment.Tables["Companies"].Rows[0];
>
> drDev.BeginEdit();
>
> for (Int16 i = 0; i < drProd.ItemArray.Length; i++)
>
> {
>
>
> if (drDev.Table.Columns[i].ToString() ==
> drProd.Table.Columns[i].ToString())
>
> {
>
> drDev[i] = drProd[i];
>
> }
>
> }
>
> drDev.EndEdit();
>
>
>
>
>
> Console.WriteLine(dsDevelopment.HasChanges(DataRowState.Modified));
>
> if (dsDevelopment.HasChanges(DataRowState.Modified))
>
> {
>
> daDevelopment.Update(dsDevelopment, "Companies");
>
> }
>
> }
>
> else
>
> {
>
> //Insert If row doesn't exist
>
> DataRow drDevAdd;
>
> drDevAdd = dsDevelopment.Tables["Companies"].NewRow();
>
> for (Int16 i = 0; i < drProd.ItemArray.Length; i++)
>
> {
>
> if (drDevAdd.Table.Columns[i].ToString() ==
> drProd.Table.Columns[i].ToString())
>
> {
>
> drDevAdd[i] = drProd[i];
>
> }
>
> }
>
> dsDevelopment.Tables["Companies"].Rows.Add(drDevAdd);
>
> daDevelopment.Update(dsDevelopment, "Companies");
>
> }
>
> }
>
> }
>
>



 
Reply With Quote
 
RSH
Guest
Posts: n/a
 
      16th Jun 2006
Thanks!

Actually if you read carefully my other post was asking about a DataReader,
this one was asking about a DataTable. i changed the code to use a
datatable instead of the original datareader based on input on the other
thread.

Thank you for your suggestion, it worked great!
Ron

"RSH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Iam struggling with an application where I am trying to transfer a datarow
> from one sql server to another instance of sql server. The schmeas may be
> slightly different and I am getting an exception when they are different.
> Is there anyway i can modify the code below so that if the schemas are
> different I can drop the offending column in the appropriate datatable?
>
> Thanks,
> Ron
>
> SqlConnection cnProductionSQL = new SqlConnection("Data Source=" +
> cmbSourceServer.SelectedItem + "; Integrated Security=SSPI; Initial
> Catalog=Master");
>
> cnProductionSQL.Open();
>
> String strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
> CompanyID ='" + strSQLDestDatabase + "'";
>
> SqlConnection cnDevelopmentSQL = new SqlConnection("Data Source=" +
> cmbDestinationServer.SelectedItem + "; Integrated Security=SSPI; Initial
> Catalog=Master");
>
> cnDevelopmentSQL.Open();
>
> String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
> CompanyID ='" + strSQLDestDatabase + "'";
>
>
>
> SqlDataAdapter daProduction = new SqlDataAdapter(strProductionSQL,
> cnProductionSQL);
>
> DataSet dsProduction = new DataSet();
>
> daProduction.Fill(dsProduction, "Companies");
>
> SqlCommandBuilder cbProduction = new SqlCommandBuilder(daProduction);
>
> SqlDataAdapter daDevelopment = new SqlDataAdapter(strDevelopmentSQL,
> cnDevelopmentSQL);
>
> DataSet dsDevelopment = new DataSet();
>
> daDevelopment.Fill(dsDevelopment, "Companies");
>
> SqlCommandBuilder cbDevelopment = new SqlCommandBuilder(daDevelopment);
>
> if (dsProduction.Tables["Companies"].Rows.Count > 0)
>
> {
>
> DataRow drProd;
>
> drProd = dsProduction.Tables["Companies"].Rows[0];
>
> if (dsDevelopment.Tables["Companies"].Rows.Count > 0)
>
> {
>
> // Update if row exists
>
> DataRow drDev;
>
> drDev = dsDevelopment.Tables["Companies"].Rows[0];
>
> drDev.BeginEdit();
>
> for (Int16 i = 0; i < drProd.ItemArray.Length; i++)
>
> {
>
>
> if (drDev.Table.Columns[i].ToString() ==
> drProd.Table.Columns[i].ToString())
>
> {
>
> drDev[i] = drProd[i];
>
> }
>
> }
>
> drDev.EndEdit();
>
>
>
>
>
> Console.WriteLine(dsDevelopment.HasChanges(DataRowState.Modified));
>
> if (dsDevelopment.HasChanges(DataRowState.Modified))
>
> {
>
> daDevelopment.Update(dsDevelopment, "Companies");
>
> }
>
> }
>
> else
>
> {
>
> //Insert If row doesn't exist
>
> DataRow drDevAdd;
>
> drDevAdd = dsDevelopment.Tables["Companies"].NewRow();
>
> for (Int16 i = 0; i < drProd.ItemArray.Length; i++)
>
> {
>
> if (drDevAdd.Table.Columns[i].ToString() ==
> drProd.Table.Columns[i].ToString())
>
> {
>
> drDevAdd[i] = drProd[i];
>
> }
>
> }
>
> dsDevelopment.Tables["Companies"].Rows.Add(drDevAdd);
>
> daDevelopment.Update(dsDevelopment, "Companies");
>
> }
>
> }
>
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test if a value exists in a DataTable Column? MattB Microsoft ASP .NET 2 11th Nov 2005 06:38 PM
If datatable exists... John Dann Microsoft ADO .NET 2 10th Jun 2005 01:10 PM
checking to see if DDL value exists Darrel Microsoft ASP .NET 4 10th Dec 2004 03:23 PM
Re: Checking if datatable exists paul s Microsoft C# .NET 0 23rd Sep 2003 05:35 AM
Checking if datatable exists Paul Sampson Microsoft C# .NET 0 23rd Sep 2003 01:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:54 AM.