MSBuild에서 ODBC DSN 생성하고 삭제하기

준비물: MSBuildTasks

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="BuildTool\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

<!-- 테스트용 DSN 정보 -->
<ItemGroup>
	<DSNToCreate Include="Item">
		<Name>DSN_NAME1</Name>
		<Server>DatabaseServer1</Server>
		<Database>InitialCatalog1</Database>
		<Description>ODBC DSN for the Database: DatabaseServer1</Description>
		<Driver>SQL Native Client</Driver>
		<LastUser>sa</LastUser>
		<Trusted_Connection>Yes</Trusted_Connection>
	</DSNToCreate>
	<DSNToCreate Include="Item">
		<Name>DSN_NAME2</Name>
		<Server>DatabaseServer2</Server>
		<Database>InitialCatalog2</Database>
		<Description>ODBC DSN for the Database: DatabaseServer2</Description>
		<Driver>SQL Native Client</Driver>
		<LastUser>sa</LastUser>
		<Trusted_Connection>Yes</Trusted_Connection>
	</DSNToCreate>
</ItemGroup>


<!-- DSN 삭제 -->
<PropertyGroup>
    <DelDSN>
		<![CDATA[
			public static string ScriptMain() {
				System.Collections.Specialized.StringCollection dsnNames = new System.Collections.Specialized.StringCollection();
	            dsnNames.Add("DSN_NAME1");
	            dsnNames.Add("DSN_NAME2");

	            string returnMsg = string.Empty;

	            using (Microsoft.Win32.RegistryKey OurKey = Microsoft.Win32.Registry.LocalMachine)
				{
	                foreach (string dsnName in dsnNames)
	                {
	                    using (Microsoft.Win32.RegistryKey dataSourcesKey = OurKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\", true))
	                    {
	                        try
	                        {
	                            dataSourcesKey.DeleteValue(dsnName);
	                        }
	                        catch (Exception ex)
	                        {
	                            returnMsg += Environment.NewLine;
	                            returnMsg += "While deleting a registry value " + @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\" + dsnName;
	                            returnMsg += Environment.NewLine;
	                            returnMsg += ex.ToString();
	                            returnMsg += Environment.NewLine;
	                        }
	                    }

	                    try
	                    {
	                        OurKey.DeleteSubKeyTree(@"SOFTWARE\ODBC\ODBC.INI\" + dsnName);
	                    }
	                    catch (Exception ex)
	                    {
	                        returnMsg += Environment.NewLine;
	                        returnMsg += "While deleting a registry key " + @"SOFTWARE\ODBC\ODBC.INI\" + dsnName;
	                        returnMsg += Environment.NewLine;
	                        returnMsg += ex.ToString();
	                        returnMsg += Environment.NewLine;
	                    }
					}
				}

	            return returnMsg;
			}
		]]>
    </DelDSN>
</PropertyGroup>


<!-- 깔끔하게 정리 -->
<Target Name="Clean">
	<Script Language="C#" Code="$(DelDSN)" Imports="System">
		<Output TaskParameter="ReturnValue" PropertyName="DelDSNResult" />
	</Script>
	<Message Text="DSN deletion: $(DelDSNResult)" />
</Target>


<!-- 테스트용 DSN 생성 -->
<Target Name="CreateDSN">
	<RegistryRead
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\%(DSNToCreate.Driver)"
		ValueName="Driver">
		<Output TaskParameter="Value" PropertyName="DriverPath" />
	</RegistryRead>
	<Message Text="DriverPath: $(DriverPath)" />

	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Server"
		Value="%(DSNToCreate.Server)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Database"
		Value="%(DSNToCreate.Database)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Description"
		Value="%(DSNToCreate.Description)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Driver"
		Value="$(DriverPath)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="LastUser"
		Value="%(DSNToCreate.LastUser)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Trusted_Connection"
		Value="%(DSNToCreate.Trusted_Connection)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"
		ValueName="%(DSNToCreate.Name)"
		Value="%(DSNToCreate.Driver)" />
</Target>
</Project>

최 재훈

블로그, 페이스북, 트위터 고성능 서버 엔진, 데이터베이스, 지속적인 통합 등 다양한 주제에 관심이 많다.
Close Menu