搞定C# 建立ODBC数据源

来源:互联网 发布:fastcam套料软件 编辑:程序博客网 时间:2024/04/29 11:46
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
你观察就可以发现用windows控制面板的工具建立ODBC数据源就是在注册表中建立相应的值。
下面的函数可以建立SQL Server的ODBC数据源!可以自己观察注册表,修改一部分值!
/// <summary>
        /// 注册ODBC数据源
        /// </summary>
        /// <param name="DsnName">ODBC数据源名称,这里要与SQL Server数据库名保持一致</param>
        /// <param name="ServerName">SQL Server数据库服务器名</param>
        /// <returns>返回是否成功</returns>
        private bool RegODBC(string DsnName,string ServerName)
        {
            try
            {
                //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI中创建一个子键和相应的值
                Microsoft.Win32.RegistryKey  regkey=Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software").OpenSubKey("ODBC").OpenSubKey("ODBC.INI",true).CreateSubKey(DsnName.Trim());
                regkey.SetValue("DataBase",DsnName.Trim());
                string strSystem32=Application.LocalUserAppDataPath.Substring(0,2);
                strSystem32=strSystem32+@"/WINDOWS/System32/SQLSRV32.dll";
                regkey.SetValue("Driver",strSystem32);
                regkey.SetValue("Server",ServerName.Trim());
                regkey.SetValue("Trusted_Connection","Yes");
                //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI/ODBC Data Sources中增加一个字符串键值
                regkey=Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software").OpenSubKey("ODBC").OpenSubKey("ODBC.INI",true).OpenSubKey("ODBC Data Sources",true);
                regkey.SetValue(DsnName.Trim(),"SQL Server");
                return true;
            }
            catch(Exception Err)
            {
            }
            return false;
        }

搞定C# 建立ODBC数据源';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击