我想使用 c# 从另一个数据库模式信息生成新的 sql 数据库
以下代码显示了从数据库中检索信息
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
sql = "Select DISTINCT(name) FROM sys.Tables";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
DataSet dset = new DataSet();
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
listBox1.Items.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM "+ds.Tables[0].Rows[i].ItemArray[0].ToString()+"",connection);
ad.Fill(dset);
}
dset.Tables[0].WriteXml(@"E:\WindowsFormsApplication2\WindowsFormsApplication2\ds.xml");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
同样,我必须从数据库中获取信息,并且必须使用此模式信息生成新数据库。