Re: Dump SQL Server database to XML through C#

Tech-Archive recommends: Fix windows errors by optimizing your registry



Yea, I thought about that. I'm trying to use visual studio's
auto-generated stuff as much as I can (as I find its easier for other
people to update later on)... I may have to just go with the SQL
though.

That's easiest. You can just create a DataSet and keep adding tables to it.

EG:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string constr="Data Source=(local); Initial Catalog=test;Integrated
Security=true";

using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
DataTable tables = new DataTable();
string sqlTables = "select name from sys.Tables where schema_id =
1";
using (SqlDataReader rdr = new SqlCommand(sqlTables,
con).ExecuteReader())
{
tables.Load(rdr);
}

DataSet db = new DataSet();
foreach (DataRow table in tables.Rows)
{
string tableName = (string)table["name"];
string sql = string.Format("select * from [{0}]", tableName);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.TableMappings.Add("Table", tableName);
da.Fill(db);


}

db.WriteXml(Console.Out,XmlWriteMode.WriteSchema);

}

}
}
}


David

.



Relevant Pages

  • Re: SQL Server 2005 Schema from C#
    ... contains multiple databases. ... public static void Main(stringargs) ... SqlConnection con = new ... SqlDataReader rdr = cmd.ExecuteReader; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dump SQL Server database to XML through C#
    ... I'm having some troubles. ... so there may be some stupid/basic questions/assumptions involved. ... using (SqlConnection con = new SqlConnection(constr)) ... using (SqlDataReader rdr = new SqlCommand(sqlTables, ...
    (microsoft.public.dotnet.languages.csharp)