public static bool CreateMsSqlUdlFile(string connectionStringPath, UdlCredentials cred) { if (cred.Server == "csql.database.windows.net" && !cred.Username.EndsWith("@csql")) { cred.Username = String.Format("{0}@csql", cred.Username); } try { StringBuilder udl = new StringBuilder(); udl.AppendLine("[oledb]"); udl.AppendLine("; Everything after this line is an OLE DB initstring"); udl.AppendFormat("Provider=SQLNCLI11.1; Password=\"{0}\";User ID={1}; Initial Catalog=altium_library; Data Source={2}; Initial File Name=\"\"; Server SPN=\"\"\r\n", cred.Password, cred.Username, cred.Server); File.WriteAllText(connectionStringPath, udl.ToString(), Encoding.Unicode); } catch (Exception err) { Debug.Write(err.Message); return(false); } return(true); }
public static UdlCredentials ReadUdlFile(string connectionStringPath) { string udl = ""; UdlCredentials cred = new UdlCredentials(); Regex tokenRex = new Regex(@"^(?=[^;])(?:(?<Token>.+?)=(?<Value>.+?);)+", RegexOptions.Multiline); if (!File.Exists(connectionStringPath)) { cred.Error = "Connection file is missing."; return(cred); } try { udl = File.ReadAllText(connectionStringPath); if (!tokenRex.IsMatch(udl)) { cred.Error = "UDL file has not been configured correctly."; return(cred); } } catch (Exception err) { cred.Error = err.Message; return(cred); } var match = tokenRex.Match(udl); for (int i = 0; i < match.Groups["Token"].Captures.Count; i++) { if (match.Groups["Token"].Captures[i].Value == "User ID") { cred.Username = match.Groups["Value"].Captures[i].Value; } else if (match.Groups["Token"].Captures[i].Value == "Password") { cred.Password = match.Groups["Value"].Captures[i].Value; } else if (match.Groups["Token"].Captures[i].Value == "Data Source") { cred.Server = match.Groups["Value"].Captures[i].Value; } } return(cred); }