By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

Importing XML Data using a DataSet

P: n/a
I am importing a XML file and have not been having the best of luck in doing
this, but I do have the following solution below. I will not be importing
more than 2000 records at a time, but will be importing data to many
different tables. Can someone explain if there is a better way to do what I
am doing without having to call a stored procedure for each row (like I'm
doing below)? A good example would be great as my understanding of xml or
updating a disconnected dataset is not too good yet. I am using ADO.Net 1.1.
Thanks in advance.

public static int Import()
{

string databaseName =
HttpContext.Current.Session["DatabaseName"].ToString();
string connectionString =
ConfigurationSettings.AppSettings["DatabaseConnectionString"] + databaseName;

using (SqlConnection conn = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);

foreach (DataTable table in ds.Tables)
{

for (int index = 0; index <= table.Rows.Count - 1; index++)
{
if (table.tableName == "Table1")
{
SqlCommand command = new SqlCommand("ImportDataForTable1", conn);
command.CommandType = CommandType.StoredProcedure;

SqlParameter param;
param = command.Parameters.Add("@recordID",
SqlDbType.UniqueIdentifier);
param.Value = new Guid(table.Rows[index]["RecordID"].ToString());

A bunch of other parameters...
}
else if (table.tableName == "table2")
{
SqlCommand command = new SqlCommand("ImportDataForTable2", conn);
command.CommandType = CommandType.StoredProcedure;

SqlParameter param;
param = command.Parameters.Add("@recordID",
SqlDbType.UniqueIdentifier);
param.Value = new Guid(table.Rows[index]["RecordID"].ToString());

A bunch of other parameters for Table 2...
}

conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}
}
Sep 5 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.