성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - SQL Server DB를 bacpac으로 Export/Import</h1> <p> "Microsoft SSMS(SQL Server Management Studio)"의 기능 중에는 "Export Data-tier Application", "Import Data-tier Application" 메뉴를 이용해 DB를 bacpac 단일 파일로 export/import할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Data-tier applications (DAC) ; <a target='tab' href='https://learn.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications'>https://learn.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications</a> Export to a BACPAC file - Azure SQL Database and Azure SQL Managed Instance ; <a target='tab' href='https://learn.microsoft.com/en-us/azure/azure-sql/database/database-export'>https://learn.microsoft.com/en-us/azure/azure-sql/database/database-export</a> Azure 데이터베이스를 로컬 DB로 이전하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/1667'>https://www.sysnet.pe.kr/2/0/1667</a> </pre> <br /> 재미있는 건, SSMS의 그런 기능들은 대부분 코드로도 가능하다는 점입니다. 실제로 bacpac 처리는 다음의 Q&A에도 그 방법이 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > c# create a .bacpac file from ms sql database ; <a target='tab' href='https://stackoverflow.com/questions/37705038/c-sharp-create-a-bacpac-file-from-ms-sql-database'>https://stackoverflow.com/questions/37705038/c-sharp-create-a-bacpac-file-from-ms-sql-database</a> </pre> <br /> 즉, Microsoft.SqlServer.Dac.dll 어셈블리를 이용하면 되는데요, 비주얼 스튜디오를 설치한 경우 "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC" 디렉터리에서 찾을 수 있습니다. 그런데 NuGet을 보면 동일한 이름의 패키지가 있는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <a target='tab' href='https://www.nuget.org/packages/Microsoft.SqlServer.Dac'>https://www.nuget.org/packages/Microsoft.SqlServer.Dac</a> Install-Package Microsoft.SqlServer.Dac </pre> <br /> "<a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.dac?view=sql-dacfx-162'>Project website</a>"의 링크가 단순히 learn 문서로 연결되는 것을 봤을 때 마이크로소프트의 공식 계정이 아니고 개인이 임의로 해당 DLL을 말아서 NuGet에 올린 것 같습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> bacpac 처리를 간단하게 ^^ 한번 만들어 볼까요?<br /> <br /> 우선 프로젝트 유형은 .NET Framework 유형으로 해야 합니다. (관련 DLL들이 아직은 .NET Core/5+용으로 빌드되지 않고 있습니다.)<br /> <br /> 그다음, Microsoft.SqlServer.Dac.dll과 함께 bacpac 처리에 필요한 DLL들은 (테스트로 알아낸 결과) 이렇게 필요합니다. (같은 디렉터리에 있습니다.)<br /> <br /> <ul> <li>Microsoft.Data.SqlClient.dll</li> <li>Microsoft.Data.SqlClient.SNI.x64.dll</li> <li>Microsoft.Data.Tools.Schema.Sql.dll</li> <li>Microsoft.Data.Tools.Utilities.dll</li> <li>Microsoft.Identity.Client.dll</li> <li>Microsoft.SqlServer.Dac.dll</li> <li>Microsoft.SqlServer.TransactSql.ScriptDom.dll</li> <li>System.Memory.dll</li> <li>System.Resources.Extensions.dll</li> <li>System.Runtime.CompilerServices.Unsafe.dll</li> </ul> <br /> 그중에 Microsoft.Data.SqlClient.SNI.x64.dll은 native 모듈이기 때문에 복사하는 식으로 배포하고, 다른 DLL들만 참조 추가를 하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <ItemGroup> <Content Include="..\lib\Microsoft.Data.SqlClient.SNI.x64.dll"> <Link>Microsoft.Data.SqlClient.SNI.x64.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> <ItemGroup> <Reference Include="Microsoft.Data.SqlClient"> <HintPath>..\lib\Microsoft.Data.SqlClient.dll</HintPath> </Reference> <Reference Include="Microsoft.Data.Tools.Schema.Sql"> <HintPath>..\lib\Microsoft.Data.Tools.Schema.Sql.dll</HintPath> </Reference> <Reference Include="Microsoft.Data.Tools.Utilities"> <HintPath>..\lib\Microsoft.Data.Tools.Utilities.dll</HintPath> </Reference> <Reference Include="Microsoft.Identity.Client"> <HintPath>..\lib\Microsoft.Identity.Client.dll</HintPath> </Reference> <Reference Include="Microsoft.SqlServer.Dac"> <HintPath>..\lib\Microsoft.SqlServer.Dac.dll</HintPath> </Reference> <Reference Include="Microsoft.SqlServer.TransactSql.ScriptDom"> <HintPath>..\lib\Microsoft.SqlServer.TransactSql.ScriptDom.dll</HintPath> </Reference> <Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\lib\System.Memory.dll</HintPath> </Reference> <Reference Include="System.Resources.Extensions"> <HintPath>..\lib\System.Resources.Extensions.dll</HintPath> </Reference> <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\lib\System.Runtime.CompilerServices.Unsafe.dll</HintPath> </Reference> </ItemGroup> </pre> <br /> 자, 남은 것은 이제 코딩인데요, bacpack 파일로 export하는 것은 다음과 같이 할 수 있고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Microsoft.SqlServer.Dac; using System; using System.Data.SqlClient; using System.IO; namespace ConsoleApp1; internal class Program { static int Main(string[] args) { string infoFilePath = args[0]; string downloadPath = args[1]; SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(); (string dataSource, string userId, string Password, string dbName) = LoadUserInfo(infoFilePath); string timePrefix = DateTime.Now.ToString("yyyy_MM_dd"); string bacpacFilePath = Path.Combine(downloadPath, $"{dbName}_{timePrefix}.bacpac"); csb.UserID = userId; csb.Password = Password; csb.DataSource = dataSource; csb.TrustServerCertificate = true; DacServices ds = new DacServices(csb.ConnectionString); ds.ExportBacpac(bacpacFilePath, dbName); Console.WriteLine(bacpacFilePath); return 0; } private static (string dataSource, string userId, string Password, string dbName) LoadUserInfo(string filePath) { string[] lines = File.ReadAllLines(filePath); return (lines[0], lines[1], lines[2], lines[3]); } } </pre> <br /> bacpac 파일을 다시 DB로 import하는 것은 다음과 같이 코딩할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Microsoft.SqlServer.Dac; using System; using smo = Microsoft.SqlServer.Management.Smo; namespace ConsoleApp3 { // Install-Package Microsoft.SqlServer.SqlManagementObjects internal class Program { static int Main(string[] args) { string bacpacFilePath = args[0]; string dbName = args[1]; DropDatabaseIfExists(dbName); string connectionString = "TrustServerCertificate=true; Integrated Security=SSPI;Persist Security Info=False;Data Source=."; BacPackage bp = BacPackage.Load(bacpacFilePath); DacServices ds = new DacServices(connectionString); ds.ImportBacpac(bp, dbName); return 0; } private static void DropDatabaseIfExists(string dbName) { var server = new smo.Server(Environment.MachineName); smo.Database found = null; foreach (smo.Database db in server.Databases) { if (db.Name.Equals(dbName, StringComparison.InvariantCultureIgnoreCase)) { found = db; break; } } if (found != null) { found.Drop(); } } } } </pre> <br /> <hr style='width: 50%' /><br /> <br /> 위의 코드를 이용하면 간단한 데이터베이스 백업 시스템을 만드는 것도 가능합니다. 스케줄러에 등록해 하루 단위로 이렇게 실행해 두면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C:\temp> <span style='color: blue; font-weight: bold'>type c:\temp\db.settings.txt</span> 192.168.100.50,1433 ...[DB계정]... ...[비밀번호]... TESTDB C:\temp> <span style='color: blue; font-weight: bold'>ExportBacpac "c:\temp\db.settings.txt" c:\temp</span> </pre> <br /> 192.168.100.50,1433 DB 서버에 접속해 "TESTDB" 데이터베이스를 "c:\temp\TESTDB_2024_01_05.bacpac"과 같은 식의 파일명으로 백업할 수 있습니다.<br /> <br /> 복원도 이와 유사하게 명령을 내리면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > c:\temp> <span style='color: blue; font-weight: bold'>ImportBacpac.exe "C:\temp\TESTDB_2024_01_05.bacpac" TESTDB</span> </pre> <br /> 그럼 ImportBacpac을 실행하는 localhost에 Windows 통합 계정으로 로그인을 해 TESTDB_2024_01_05.bacpac 파일의 내용을 새롭게 TESTDB 데이터베이스로 복원을 합니다.<br /> <br /> 간단하죠? ^^ 좀 더 응용해서 위의 과정을 합치는 배치 파일을 만들어 두면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > for /f %%i in ('"c:\temp\ExportBacpac.exe" "C:\temp\db.settings.txt" C:\temp') do set CREATED_FILE_PATH=%%i echo %CREATED_FILE_PATH% Created! "c:\temp\ImportBacpac.exe" "%CREATED_FILE_PATH%" TESTDB </pre> <br /> 매일 특정 DB를 로컬 측으로 동기화하도록 만들 수 있습니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=2135&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다. 크기를 줄이기 위해 참조한 DLL은 누락시켰습니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1008
(왼쪽의 숫자를 입력해야 합니다.)