R
RWC
Hello,
What I want I fairly simple (I guess), but I can't work it out.
In one application I export data from a SQL Server database to an XML
file. I have used the DataSet.WriteXML method to do this. My XML file
looks like this (simplified):
<?xml version="1.0" standalone="yes"?>
<brokers>
<broker>
<broker_number>1</broker_number>
<broker_name>Peter</broker_name>
</broker>
<broker>
<broker_number>2</broker_number>
<broker_name>Eric</broker_name>
</broker>
</brokers>
As you can guess this is just an export from a table with two fields:
broker_nummer and broker_name.
Now I want to fill the table "broker" in another SQL Server database.
I can not read from the XML file directly, but I have to read from a
string (which is just the content of the XML file). In the new
database the field names are slightly different: broker_number is
called BrokerId and broker_name is called Name
I have produced the following:
private bool ImportXML(string docIn, ref string ErrorMsg)
{
const string selectquery = "SELECT * FROM broker WHERE BrokerID
= @BrokerID";
const string insertquery = "INSERT INTO broker (Name) VALUES
(@Name)";
const string updatequery = "UPDATE broker SET BrokerID =
@BrokerID, Name= @Name WHERE BrokerID = @BrokerID";
const string deletequery = "DELETE FROM broker WHERE BrokerID =
@BrokerID";
try
{
//create a custom adapter
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
//fill dataset with XML-data
DataSet ds = new DataSet();
//we need a string reader to read from string
StringReader sr = new StringReader(docIn);
// Create the SelectCommand
SqlCommand query = new SqlCommand(selectquery, m_Connection);
query.Parameters.Add("@BrokerId", SqlDbType.Int, 4);
sqlDataAdapter.SelectCommand = query;
// Create the DeleteCommand
query = new SqlCommand(deletequery, m_Connection);
query.Parameters.Add("@BrokerId", SqlDbType.Int, 4);
sqlDataAdapter.DeleteCommand = query;
// Create the InsertCommand.
query = new SqlCommand(insertquery, m_Connection);
query.Parameters.Add("@Name", SqlDbType.NVarChar, 50,
"broker_name");
sqlDataAdapter.InsertCommand = query;
// Create the UpdateCommand.
query = new SqlCommand(updatequery, m_Connection);
query.Parameters.Add("@pBrokerID", SqlDbType.Int);
query.Parameters["@pBrokerID"].SourceVersion =
DataRowVersion.Current;
query.Parameters["@pBrokerID"].SourceColumn = "broker_number";
query.Parameters.Add("@pNaam", SqlDbType.VarChar);
query.Parameters["@pNaam"].SourceVersion =
DataRowVersion.Current;
query.Parameters["@pNaam"].SourceColumn = "broker_naam";
sqlDataAdapter.UpdateCommand = query;
ds.ReadXml(sr);
sqlDataAdapter.Update(ds, "Broker");
return true;
}
catch (Exception e)
{
ErrorMsg = e.Message;
return false;
}
}
I have got a few questions:
1. Is this the right approach?
2. Are the queries correct?
3. I am inserting data in an empty table. Why I keep getting this
message after inseting 1 record?
"Violation of UNIQUE KEY constraint 'IX_Broker_1'. Cannot insert
duplicate key in object 'dbo.broker'.\r\nThe statement has been
terminated."
Can you help me out. Thanks.
What I want I fairly simple (I guess), but I can't work it out.
In one application I export data from a SQL Server database to an XML
file. I have used the DataSet.WriteXML method to do this. My XML file
looks like this (simplified):
<?xml version="1.0" standalone="yes"?>
<brokers>
<broker>
<broker_number>1</broker_number>
<broker_name>Peter</broker_name>
</broker>
<broker>
<broker_number>2</broker_number>
<broker_name>Eric</broker_name>
</broker>
</brokers>
As you can guess this is just an export from a table with two fields:
broker_nummer and broker_name.
Now I want to fill the table "broker" in another SQL Server database.
I can not read from the XML file directly, but I have to read from a
string (which is just the content of the XML file). In the new
database the field names are slightly different: broker_number is
called BrokerId and broker_name is called Name
I have produced the following:
private bool ImportXML(string docIn, ref string ErrorMsg)
{
const string selectquery = "SELECT * FROM broker WHERE BrokerID
= @BrokerID";
const string insertquery = "INSERT INTO broker (Name) VALUES
(@Name)";
const string updatequery = "UPDATE broker SET BrokerID =
@BrokerID, Name= @Name WHERE BrokerID = @BrokerID";
const string deletequery = "DELETE FROM broker WHERE BrokerID =
@BrokerID";
try
{
//create a custom adapter
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
//fill dataset with XML-data
DataSet ds = new DataSet();
//we need a string reader to read from string
StringReader sr = new StringReader(docIn);
// Create the SelectCommand
SqlCommand query = new SqlCommand(selectquery, m_Connection);
query.Parameters.Add("@BrokerId", SqlDbType.Int, 4);
sqlDataAdapter.SelectCommand = query;
// Create the DeleteCommand
query = new SqlCommand(deletequery, m_Connection);
query.Parameters.Add("@BrokerId", SqlDbType.Int, 4);
sqlDataAdapter.DeleteCommand = query;
// Create the InsertCommand.
query = new SqlCommand(insertquery, m_Connection);
query.Parameters.Add("@Name", SqlDbType.NVarChar, 50,
"broker_name");
sqlDataAdapter.InsertCommand = query;
// Create the UpdateCommand.
query = new SqlCommand(updatequery, m_Connection);
query.Parameters.Add("@pBrokerID", SqlDbType.Int);
query.Parameters["@pBrokerID"].SourceVersion =
DataRowVersion.Current;
query.Parameters["@pBrokerID"].SourceColumn = "broker_number";
query.Parameters.Add("@pNaam", SqlDbType.VarChar);
query.Parameters["@pNaam"].SourceVersion =
DataRowVersion.Current;
query.Parameters["@pNaam"].SourceColumn = "broker_naam";
sqlDataAdapter.UpdateCommand = query;
ds.ReadXml(sr);
sqlDataAdapter.Update(ds, "Broker");
return true;
}
catch (Exception e)
{
ErrorMsg = e.Message;
return false;
}
}
I have got a few questions:
1. Is this the right approach?
2. Are the queries correct?
3. I am inserting data in an empty table. Why I keep getting this
message after inseting 1 record?
"Violation of UNIQUE KEY constraint 'IX_Broker_1'. Cannot insert
duplicate key in object 'dbo.broker'.\r\nThe statement has been
terminated."
Can you help me out. Thanks.