C # modificar la fuente de datos odbc

C # modificar la fuente de datos odbc

 static string targetIP = "127.0.0.1,3000";
        static string targetUserName = "wangjp";
        static string targetPassWord = @"123"; //注意:密码前面最好加@

        static List<FileModel> listFile = null;
        static string strDbName = string.Empty;
        static void Main(string[] args)
        {
    
    
            listFile = new List<FileModel>();
            try
            {
    
    
                ReadDB();
                RegODBC("wangjpDB", targetIP);
                updateFile();
                Console.WriteLine("SUCCESS    " + targetIP);
            }
            catch (Exception ex)
            {
    
    
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }  
        public static void updateFile()
        {
    
    
            if (strDbName.Equals("wangjpDB")) //default是Mu3Seagate
            {
    
    
                listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().FIRSTNAME = targetUserName;
                listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().LASTNAME = targetPassWord;
            }
            listFile.Where(x => x.dns.ToUpper().Equals("wangjpDB")).ToList().FirstOrDefault().FIRSTNAME = targetUserName;
            listFile.Where(x => x.dns.ToUpper().Equals("wangjpDB")).ToList().FirstOrDefault().LASTNAME = targetPassWord;
            File.Delete(@"C:\Windows\Eado.ini");
            List<string> tempAll = new List<string>();
            listFile.ForEach(x => {
    
    
                List<string> temp = new List<string>();
                temp.Add("[" + x.dns + "]");
                temp.Add("NICKNAME=" + x.NICKNAME);
                temp.Add("FIRSTNAME=" + x.FIRSTNAME);
                temp.Add("LASTNAME=" + x.LASTNAME);
                temp.Add("SINGILECONNECTION=" + x.SINGILECONNECTION);
                temp.Add("");
                tempAll.AddRange(temp);
                //File.AppendAllLines(@"C:\Windows\Eado.ini", temp,Encoding.UTF8);      //framework4.5不支持appendAllLine
                //File.AppendAllLines(@"C:\Windows\Eado.ini", new List<string> { "" }, Encoding.UTF8);
                //File.AppendAllText(@"C:\Windows\Eado.ini", "\r\n");
            });
            string[] aa = tempAll.ToArray();
            File.WriteAllLines(@"C:\Windows\Eado.ini", tempAll.ToArray());
        }
        public static void ReadDB()
        {
    
    
            List<string> list = new List<string>();
            list = File.ReadAllLines(@"C:\Windows\Eado.ini").ToList().Where(x => !string.IsNullOrEmpty(x)).ToList();
            List<string> listdns = new List<string>(); //0,5,10
            List<string> listNICKNAME = new List<string>(); //1,6,11
            List<string> listFIRSTNAME = new List<string>();  //2,7,12
            List<string> listLASTNAME = new List<string>();   //3,8,13
            List<string> listSINGILECONNECTION = new List<string>(); //4,9,14 
            for (int i = 0; i < list.Count; i++)//等差数列算法
            {
    
    
                if (i % 5 == 0) listdns.Add(list[i].Replace("[", "").Replace("]", "").Trim());
                else if (i % 5 == 1) listNICKNAME.Add(list[i].Replace("NICKNAME", "").Replace("=", "").Trim());
                else if (i % 5 == 2) listFIRSTNAME.Add(list[i].Replace("FIRSTNAME", "").Replace("=", "").Trim());
                else if (i % 5 == 3) listLASTNAME.Add(list[i].Replace("LASTNAME", "").Replace("=", "").Trim());
                else if (i % 5 == 4) listSINGILECONNECTION.Add(list[i].Replace("SINGILECONNECTION", "").Replace("=", "").Trim());
            }
            listFile.Clear();
            for (int i = 0; i < listdns.Count; i++)
            {
    
    
                listFile.Add(new FileModel()
                {
    
    
                    dns = listdns[i],
                    NICKNAME = listNICKNAME[i],
                    FIRSTNAME = listFIRSTNAME[i],
                    LASTNAME = listLASTNAME[i],
                    SINGILECONNECTION = listSINGILECONNECTION[i]
                });
            }
            strDbName = listFile.Where(x => x.dns.ToUpper().Equals("DEFAULT")).ToList().FirstOrDefault().NICKNAME;
        }
        /// <summary>
        /// 注册ODBC数据源
        /// </summary>
        /// <param name="DsnName">ODBC数据源名称,这里要与SQL Server数据库名保持一致</param>
        /// <param name="ServerName">SQL Server数据库服务器名</param>
        /// <returns>返回是否成功</returns>
        private static void RegODBC(string DsnName, string ServerName)
        {
    
    
            string DBname = string.Empty;
            string strText = File.ReadAllText(@"C:\Windows\Eado.ini");
            Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC").
                OpenSubKey("ODBC.INI", true).DeleteSubKey(DsnName.Trim());
            //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI中创建一个子键和相应的值
            Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").
                OpenSubKey("ODBC").OpenSubKey("ODBC.INI", true).CreateSubKey(DsnName.Trim());
            regkey.SetValue("Database", "wangjpDataBase");
            regkey.SetValue("Driver", @"C:\WINDOWS\System32\SQLSRV32.dll");
            regkey.SetValue("Server", ServerName.Trim());
            regkey.SetValue("LastUser", "wangjp");
            regkey.SetValue("Trusted_Connection", "No");//如果是sqlserver账密登录,就是No,负责是Yes
            //在HKEY_LOCAL_MACHINE/Software/ODBC/ODBC.INI/ODBC Data Sources中增加一个字符串键值
            regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software").OpenSubKey("ODBC")
                .OpenSubKey("ODBC.INI", true).OpenSubKey("ODBC Data Sources", true);
            regkey.SetValue(DsnName.Trim(), "SQL Server");
        }

Supongo que te gusta

Origin blog.csdn.net/m0_50623581/article/details/108435291
Recomendado
Clasificación