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

  • Post Author:
  • Post Category:
  • Post last modified:February 8, 2020

준비물: 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>
Kubernetes, DevSecOps, Golang, 지속적인 통합 등 다양한 주제에 관심이 많다.
follow me
  • 와우 댓글 읽는데 어지럽네. 미국이 아무리 다양한 의견이 넘치는 사회라 해도 이 정도로 분열이 심했던가 https://t.co/zNisBhldx5
    15 hours ago
  • 이래저래 흥미로운 서비스 https://t.co/MTpLdTE0vN
    16 hours ago
  • RT @Tootiredhey: 어머니가 캣맘을 그만두신다. 몇년동안 하셨고 눈에 밟히는 아이들도 많지만 이번에 관두고 급식소도 다 철거할 예정이다.
    20 hours ago
Buy me a coffeeBuy me a coffee
×
Kubernetes, DevSecOps, Golang, 지속적인 통합 등 다양한 주제에 관심이 많다.
Latest Posts