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

Importing Data from XML to 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 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Mike.
Is this a solution for you?
http://samples.gotdotnet.com/quickst...ataFromDB.aspx
Perhaps http://www.c-sharpcorner.com/Database/DBOpsInADOPA.asp

"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:1E**********************************@microsof t.com...
>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 7 '06 #2

P: n/a
Thank you...those are very helpful.

"Mauricio Grimberg" wrote:
Hi Mike.
Is this a solution for you?
http://samples.gotdotnet.com/quickst...ataFromDB.aspx
Perhaps http://www.c-sharpcorner.com/Database/DBOpsInADOPA.asp

"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:1E**********************************@microsof t.com...
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 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.