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

时间:2013-04-16 13:44:59

标签: c# sql-server ms-access bcp

我有一个Microsoft SQL Server数据库,该数据库定期更新数据。我想将此数据库与所有表(以及最好的关系)存储到使用C#的新Microsoft Access(.accdb)文件。

SQL Management Studio安装在系统上,所以我认为一个解决方案可能是从代码中调用BCP(http://msdn.microsoft.com/en-us/library/ms162802.aspx),但在这种情况下我还没弄清楚如何正确使用它。我想在没有使用BCP的情况下有更好的方法。

有人可以推荐一种方法来实现这个目标吗?

谢谢

2 个答案:

答案 0 :(得分:2)

您可以在Access中导入MSSQL数据;有关详细信息:http://office.microsoft.com/en-us/access-help/import-or-link-to-sql-server-data-HA010200494.aspx

更新

或者,您可以使用sqldataadapter选择所有表来存储数据集中的所有内容,请参阅:Obtaining a dataset from a SQL Server database

从那以后,您可以将数据集保存为访问数据库文件,请参阅:C# Dataset to Access DB

也许这更符合您的问题。

答案 1 :(得分:1)

感谢Ferdy的建议,我解决了这个问题。由于它可以用于其他人,我把我的工作代码示例放在这里:

//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();