2013-04-16 1 views
0

데이터가 정기적으로 업데이트되는 Microsoft SQL Server 데이터베이스가 있습니다. C#을 사용하여 새로운 데이터베이스 (.accdb) 파일에 모든 테이블 (및 선호 관계)이 데이터베이스를 저장하고 싶습니다.MS Access 데이터베이스를 MS Access .accdb 파일로 내보내기

SQL Management Studio가 시스템에 설치되어 있으므로 하나의 솔루션이 코드에서 BCP (http://msdn.microsoft.com/en-us/library/ms162802.aspx)를 호출 할 수 있다고 생각하지만이 경우이 코드를 올바르게 사용하는 방법을 찾지 못했습니다. 나는 BCP를 사용하지 않고 그것을하는 훨씬 더 좋은 방법이 있다고 생각합니다.

누구나이 방법을 권장 할 수 있습니까?

감사합니다

+1

이미 만든 빈 데이터베이스가 있습니까? 이 게시물에서 팁을 확인하십시오. http://granadacoder.wordpress.com/2009/12/22/sql-server-data-to-a-jet-database/ – granadaCoder

답변

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

어쩌면 이것은 인라인과 관련된 문제 일 수 있습니다.

+0

답변 해 주셔서 감사합니다. 예, 가능 합니다만, 매번 새로운 .accdb 파일을 만들고 싶지만 항상 업데이트되는 파일을 가지고 있지는 않습니다. – Emil

+0

나는이 솔루션을 코딩하지 않고 (작성자가 'C#을 사용하고 싶다') – Konstantin

+0

@KonstantinKonstantinov 좋은 해결책이 될 수는 있지만 생성 된 .accdb 파일을 실제 SQL Server에 연결하지 않기를 바란다. , 오히려 그것의 오프라인 사본 일뿐입니다. – Emil

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(); 
+0

, 어떻게 INSERT에서 데이터 형식을 처리합니까? 모든 유형을 문자열로 변환했는지, 정수인지 NULL인지> –