473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Checking to see if a column exists in a datatable

RSH
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(DataRow State.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");

}

}

}
Jun 16 '06 #1
2 7300
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" <wa*************@yahoo.com> wrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...
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(DataRow State.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");

}

}

}

Jun 16 '06 #2
RSH
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" <wa*************@yahoo.com> wrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...
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(DataRow State.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");

}

}

}

Jun 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: André Almeida Maldonado | last post by:
When I use this code: Dim dtsDataSet As DataSet = New DataSet("Dados") Dim dttUsuarios As DataTable = dtsDataSet.Tables.Add("Usuarios") Dim dtvUsuarios As DataView = New...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
0
by: Paul Sampson | last post by:
Hi, I'm using the DataAdapter/DataSet/DataTable structures and the native SQL Server provider. I have a generic function that clears my datatable then fills it again each time. However on the...
7
by: Patrick Olurotimi Ige | last post by:
I have a simple Stored Procedure with multiple select statements..doing select * from links for example. I created a DataTable and then fill the tables But the first dtTemplate DataTable doesn't...
2
by: MattB | last post by:
I know I can use dataviews and more looping to do this, but I'm wondering if there's a more elegant or more concise way to do this. I've got two DataTables with a column called guest_no. I'd love...
3
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code...
3
by: randy1200 | last post by:
I have a DataSet/DataTable read in from an XML file. I can loop through the DataRows in the DataTable, and do actions like this: string theName = dr.ToString(); My fear is that somebody is...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
46
by: John | last post by:
Hi How can I check for existence of a certain table by name in and access database and if the table exists find the number of records in it? I guess I can access this information by setting up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.