简体   繁体   English

将MSSQL数据库导出到MS Access .accdb文件

[英]Export MSSQL database to MS Access .accdb file

I have a Microsoft SQL Server database which is updated with data regularly. 我有一个Microsoft SQL Server数据库,该数据库会定期更新数据。 I would like to store this database with all tables (and preferrably relationsships) to a new Microsoft Access (.accdb) file using C#. 我想将此数据库的所有表(最好是关联关系)存储到使用C#的新Microsoft Access(.accdb)文件中。

SQL Management Studio is installed on the system so I think one solution could be to invoke BCP ( http://msdn.microsoft.com/en-us/library/ms162802.aspx ) from the code, but I haven't figured out how to use it correctly in this case. SQL Management Studio已安装在系统上,因此我认为一种解决方案可能是从代码中调用BCP( http://msdn.microsoft.com/en-us/library/ms162802.aspx ),但我还没有弄清楚在这种情况下如何正确使用它。 I guess there are much better ways doing it without using BCP though. 我猜想有很多更好的方法可以不使用BCP。

Can anyone recommend a way to achieve this? 谁能推荐实现这一目标的方法?

Thank you 谢谢

You can import MSSQL Data in Access; 您可以在Access中导入MSSQL数据。 More info on: http://office.microsoft.com/en-us/access-help/import-or-link-to-sql-server-data-HA010200494.aspx 有关更多信息: http : //office.microsoft.com/zh-cn/access-help/import-or-link-to-sql-server-data-HA010200494.aspx

Update: 更新:

Alternately you can select all tables using sqldataadapter to store everything in a dataset, see: Obtaining a dataset from a SQL Server database 或者,您可以使用sqldataadapter选择所有表以将所有内容存储在数据集中,请参阅: 从SQL Server数据库获取数据集

From there on you can save the dataset as a access database file, see: C# Dataset to Access DB 从那里可以将数据集另存为Access数据库文件,请参见: C#数据集到Access DB

Maybe this is more inline with your problem. 也许这更符合您的问题。

Thanks to Ferdy's suggestion I solved the problem. 感谢Ferdy的建议,我解决了这个问题。 Since it could be of use for others I put my working code sample here: 由于它可能对其他人有用,因此将我的工作代码示例放在此处:

//The connection strings needed: One for SQL and one for Access
String accessConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\...\\test.accdb;";
String sqlConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Your_Catalog;Integrated Security=True";   

//Make adapters for each table we want to export
SqlDataAdapter adapter1 = new SqlDataAdapter("select * from Table1", sqlConnectionString);
SqlDataAdapter adapter2 = new SqlDataAdapter("select * from Table2", sqlConnectionString);

//Fills the data set with data from the SQL database
DataSet dataSet = new DataSet();
adapter1.Fill(dataSet, "Table1");
adapter2.Fill(dataSet, "Table2");

//Create an empty Access file that we will fill with data from the data set
ADOX.Catalog catalog = new ADOX.Catalog();
catalog.Create(accessConnectionString);

//Create an Access connection and a command that we'll use
OleDbConnection accessConnection = new OleDbConnection(accessConnectionString);
OleDbCommand command = new OleDbCommand();
command.Connection = accessConnection;
command.CommandType = CommandType.Text;
accessConnection.Open();

//This loop creates the structure of the database
foreach (DataTable table in dataSet.Tables)
{
    String columnsCommandText = "(";
    foreach (DataColumn column in table.Columns)
    {
        String columnName = column.ColumnName;
        String dataTypeName = column.DataType.Name;
        String sqlDataTypeName = getSqlDataTypeName(dataTypeName);
        columnsCommandText += "[" + columnName + "] " + sqlDataTypeName + ",";
    }
    columnsCommandText = columnsCommandText.Remove(columnsCommandText.Length - 1);
    columnsCommandText += ")";

    command.CommandText = "CREATE TABLE " + table.TableName + columnsCommandText;

    command.ExecuteNonQuery();
}

//This loop fills the database with all information
foreach (DataTable table in dataSet.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        String commandText = "INSERT INTO " + table.TableName + " VALUES (";
        foreach (var item in row.ItemArray)
        {
            commandText += "'"+item.ToString() + "',";
        }
        commandText = commandText.Remove(commandText.Length - 1);
        commandText += ")";

        command.CommandText = commandText;
        command.ExecuteNonQuery();
    }
}

accessConnection.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM