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 below but I get an exception thrown because the column
doesn't exist in Datareader B.
I'm not opposed to using another method but the same thing happened when
using a Dataset. How do I do this??
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 + "'";
SqlCommand cmdProd = new SqlCommand(strProductionSQL,cnProductionSQL);
SqlDataReader dtrProd = cmdProd.ExecuteReader();
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 + "'";
SqlCommand cmdDev = new SqlCommand(strDevelopmentSQL, cnDevelopmentSQL);
SqlDataReader dtrDev = cmdDev.ExecuteReader();
if (dtrDev.HasRows == true)
{
StringBuilder sbUpdateQuery = new StringBuilder();
sbUpdateQuery.Append("UPDATE [Global].[dbo].[Companies] SET ");
int Ordinal;
dtrDev.Read();
while (dtrProd.Read())
{
for(int i = 0; i < dtrProd.FieldCount; i++)
{
if (dtrProd.GetName(i)!= "ID")
{
Ordinal = dtrDev.GetOrdinal(dtrProd.GetName(i));
if (Ordinal > 0)
{
if (i != 1)
{
sbUpdateQuery.Append(",");
}
sbUpdateQuery.Append(dtrProd.GetName(i) + "='" + dtrProd[i].ToString() +
"'");
}
}
}
}
sbUpdateQuery.Append("WHERE CompanyID = '" + strSQLDestDatabase + "'");
dtrDev.Close();
dtrProd.Close();
//dtrDev = cmdDev.ExecuteNonQuery(sbUpdateQuery.ToString());
}