0

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

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

谁能推荐一种方法来实现这一目标?

谢谢

4

2 回答 2

2

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

更新:

或者,您可以使用 sqldataadapter 选择所有表以将所有内容存储在数据集中,请参阅:从 SQL Server 数据库获取数据集

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

也许这更符合您的问题。

于 2013-04-16T13:47:44.257 回答
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();
于 2013-04-17T15:48:50.857 回答