Monday, May 17, 2010

compare 2 xml files with csharp




7
20060426

test
testttt
paris
182204


8
20060426

whatever
whateverrr
where ever
182204







9
20060426

test
testttt
paris
182204


10
20060426

whatever
whateverrr
where ever
182204







7
20060426

test
testttt
paris
182204


8
20060426

whatever
whateverrr
where ever
182204


9
20060426

test
testttt
paris
182204


10
20060426

whatever
whateverrr
where ever
182204




--------------------------------------
private void PlazmosUberXML11111(){
DataSet serverXML = new DataSet("serverXML"); //server data
DataSet pcXML = new DataSet("pcXML"); //pc data
DataSet outputXML = new DataSet("Output"); //output

//read in our files
try
{
serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto);
pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

//output is server plus new records from pc i think???????
//so start with server then add the new pc records
outputXML = serverXML;


//compare each record of the pcs xml to the servers, and see if the server is missing any
//we only need to look at the first table of the dataset because that is where the xml gets read into
foreach(DataRow pcRow in pcXML.Tables[0].Rows)
{
bool hasRow = false;
foreach(DataRow serverRow in serverXML.Tables[0].Rows)
{
//this looks at only the column named "Id" in our records
//if we find a matching set bool and break out of loop
if(pcRow["Id"].ToString() == serverRow["Id"].ToString())
{
hasRow = true;
break;
}

}

if(hasRow)
{
//do something if your finding simliar rows
}
else //do something if your finding a row it doesnt have
{
DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme
newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row
outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table
}
}

//write it to the xml file
outputXML.WriteXml("newXML.xml");

}
---------------------------------------------------------------------------
private void PlazmosUberXML11111(){
DataSet serverXML = new DataSet("serverXML"); //server data
DataSet pcXML = new DataSet("pcXML"); //pc data
DataSet outputXML = new DataSet("Output"); //output

//read in our files
try
{
serverXML.ReadXml("XMLFile.xml", XmlReadMode.Auto);
pcXML.ReadXml("XMLFile2.xml", XmlReadMode.Auto);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

//output is server plus new records from pc i think???????
//so start with server then add the new pc records
outputXML = serverXML;


//compare each record of the pcs xml to the servers, and see if the server is missing any
//we only need to look at the first table of the dataset because that is where the xml gets read into
foreach(DataRow pcRow in pcXML.Tables[0].Rows)
{
bool hasRow = false;
foreach(DataRow serverRow in serverXML.Tables[0].Rows)
{
//this looks at only the column named "Id" in our records
//if we find a matching set bool and break out of loop
if(pcRow["Id"].ToString() == serverRow["Id"].ToString())
{
hasRow = true;
break;
}

}

if(hasRow)
{
//do something if your finding simliar rows
}
else //do something if your finding a row it doesnt have
{
DataRow newRow = outputXML.Tables[0].NewRow(); //make a new row with the tables scheme
newRow.ItemArray = pcRow.ItemArray; //copy over the contents of the row
outputXML.Tables[0].Rows.Add(newRow); //add the new row to our final table
}
}

//write it to the xml file
outputXML.WriteXml("newXML.xml");

}

http://www.daniweb.com/forums/thread46345.html#

Saturday, May 15, 2010

SqlTransaction Class

SqlTransaction Class

private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;

// Start a local transaction.
transaction = connection.BeginTransaction("SampleTransaction");

// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;

try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
command.ExecuteNonQuery();

// Attempt to commit the transaction.
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);

// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}

Wednesday, May 5, 2010

Restore SQL Server 2005 Suspect Database

Introduction

If your project's database is in suspect mode, then no transaction will take place until and unless you repair your database. That causes a show stopper for your up and running application. Here, you will find a way to get out of this.

Background

Your Database is in Suspect Mode. I guess, you haven't experienced this problem till now. But, if it comes to you and if the database is LIVE, then it's time to read this article to get out of this tension.

Using the Code

If you find your database in Suspect mode, then please keep your nerve strong. Just proceed step by step what I am written below. I think you will get out of this trouble. SQL Server 2005 introduced a new DB Status called Emergency. This mode can change the DB from Suspect mode to Emergency mode, so that you can retrieve the data in read only mode. The steps are... After executing the script given below, you will get back your database in operational mode. Actually I have tried with two of my existing live systems and found no data loss.

Note: Obviously there are two more options available. Run REPAIR_ALLOW_DATA_LOSS to ensure the database is returned to a structurally and transitionally consistent state. Here are a few things to bear in mind about emergency mode repair: it's a one-way operation. Anything it does cannot be rolled back or undone. If this worries you (if someone ever got into that state, then surely don't have the healthy concern about data that they should have in the first place) then make a copy of the damaged database before you run emergency mode repair.
As it's a one-way operation, you cannot wrap it in an explicit user-transaction.
It's the only repair option available in emergency mode - if you try to use REPAIR_REBUILD, then it won't work.

Collapse
EXEC sp_resetstatus 'yourDBname';
ALTER DATABASE yourDBname SET EMERGENCY
DBCC checkdb('yourDBname')
ALTER DATABASE yourDBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('yourDBname', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE yourDBname SET MULTI_USER
Points of Interest

You will be happy that your database as well as the application are still in workable condition. :)