public static ConnectionParameters Parse(string connectionString) { var connectionStringTokeniser = new ConnectionStringTokeniser(); var result = new ConnectionParameters(); string dsUrl = null; foreach (var item in connectionStringTokeniser.Tokenise(connectionString)) { if (item.PropertyName.Equals("DSURL", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Directory Service URL", StringComparison.OrdinalIgnoreCase)) { dsUrl = item.PropertyValue; } else if (PropertyMap.ContainsKey(item.PropertyName)) { PropertyMap[item.PropertyName](item, result); } } ProcessDsUrl(dsUrl, result); ValidateConnectionParameters(result); return(result); }
public static ConnectionParameters Parse(string connectionString) { var connectionStringTokeniser = new ConnectionStringTokeniser(); var result = new ConnectionParameters(); string dsUrl = null; foreach (var item in connectionStringTokeniser.Tokenise(connectionString)) { if (item.PropertyName.Equals("DSURL", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Directory Service URL", StringComparison.OrdinalIgnoreCase)) { dsUrl = item.PropertyValue; } else if (item.PropertyName.Equals("Server", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Data Source", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("DataSource", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Address", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Addr", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Network Address", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Server Name", StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrWhiteSpace(item.PropertyValue)) { continue; } var parts = item.PropertyValue.Split(',', ':'); result.Server = parts[0]; if (parts.Length > 1) { result.Port = Convert.ToInt32(parts[1]); } } else if (item.PropertyName.Equals("Port", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Server Port", StringComparison.OrdinalIgnoreCase)) { result.Port = Convert.ToInt32(item.PropertyValue); } else if (item.PropertyName.Equals("Db", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Database", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Initial Catalog", StringComparison.OrdinalIgnoreCase)) { result.Database = item.PropertyValue; } else if (item.PropertyName.Equals("UID", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("User ID", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("UserID", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("User", StringComparison.OrdinalIgnoreCase)) { result.Username = item.PropertyValue; } else if (item.PropertyName.Equals("Pwd", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Password", StringComparison.OrdinalIgnoreCase)) { result.Password = item.PropertyValue; } else if (item.PropertyName.Equals("Charset", StringComparison.OrdinalIgnoreCase)) { result.Charset = item.PropertyValue; } else if (item.PropertyName.Equals("Pooling", StringComparison.OrdinalIgnoreCase)) { result.Pooling = Convert.ToBoolean(item.PropertyValue); } else if (item.PropertyName.Equals("Max Pool Size", StringComparison.OrdinalIgnoreCase)) { result.MaxPoolSize = Convert.ToInt16(item.PropertyValue); } else if (item.PropertyName.Equals("Min Pool Size", StringComparison.OrdinalIgnoreCase)) { result.MinPoolSize = Convert.ToInt16(item.PropertyValue); } else if (item.PropertyName.Equals("ApplicationName", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Application Name", StringComparison.OrdinalIgnoreCase)) { result.ApplicationName = item.PropertyValue; } else if (item.PropertyName.Equals("ClientHostName", StringComparison.OrdinalIgnoreCase)) { result.ClientHostName = item.PropertyValue; } else if (item.PropertyName.Equals("ClientHostProc", StringComparison.OrdinalIgnoreCase)) { result.ClientHostProc = item.PropertyValue; } else if (item.PropertyName.Equals("Ping Server", StringComparison.OrdinalIgnoreCase)) { result.PingServer = Convert.ToBoolean(item.PropertyValue); } else if (item.PropertyName.Equals("LoginTimeOut", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Connect Timeout", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Connection Timeout", StringComparison.OrdinalIgnoreCase)) { result.LoginTimeout = Convert.ToInt32(item.PropertyValue); } else if (item.PropertyName.Equals("ConnectionIdleTimeout", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Connection IdleTimeout", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Connection Idle Timeout", StringComparison.OrdinalIgnoreCase)) { result.ConnectionIdleTimeout = Convert.ToInt16(item.PropertyValue); } else if (item.PropertyName.Equals("ConnectionLifetime", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Connection Lifetime", StringComparison.OrdinalIgnoreCase)) { result.ConnectionLifetime = Convert.ToInt16(item.PropertyValue); } else if (item.PropertyName.Equals("PacketSize", StringComparison.OrdinalIgnoreCase) || item.PropertyName.Equals("Packet Size", StringComparison.OrdinalIgnoreCase)) { result.PacketSize = Convert.ToUInt16(item.PropertyValue); } else if (item.PropertyName.Equals("TextSize", StringComparison.OrdinalIgnoreCase)) { result.TextSize = Convert.ToInt32(item.PropertyValue); } else if (item.PropertyName.Equals("UseAseDecimal", StringComparison.OrdinalIgnoreCase)) { if (bool.TryParse(item.PropertyValue, out var parsedBool)) { result.UseAseDecimal = parsedBool; } else if (int.TryParse(item.PropertyValue, out var parsedInt)) { result.UseAseDecimal = parsedInt != 0; } else { result.UseAseDecimal = Convert.ToBoolean(item.PropertyValue); } } } if (!string.IsNullOrWhiteSpace(dsUrl)) { // file://[path]<filename>[?][servicename] var match = DsUrlRegex.Match(dsUrl); if (match.Success) { var path = match.Groups["path"].Value; if (string.IsNullOrWhiteSpace(path)) { var sybaseEnvironmentVariablePath = @"%SYBASE%\ini"; var resolvedSybasePath = Environment.ExpandEnvironmentVariables(sybaseEnvironmentVariablePath); if (!string.Equals(sybaseEnvironmentVariablePath, resolvedSybasePath)) { path = resolvedSybasePath; } } // If we got a path... if (!string.IsNullOrWhiteSpace(path)) { var filename = match.Groups["filename"].Value; var servicename = match.Groups["servicename"].Value; if (string.IsNullOrWhiteSpace(servicename)) { servicename = result.Server; } var fullpath = Path.Combine(path, filename); var iniReader = new IniReader(); var iniEntry = iniReader.Query(fullpath, servicename); if (iniEntry != null) { result.Server = iniEntry.HostName; result.Port = iniEntry.Port; } } } } if (string.IsNullOrWhiteSpace(result.Server)) { throw new ArgumentException("Data Source not specified"); } if (result.Port <= 0 || result.Port > ushort.MaxValue) { throw new ArgumentException("Valid port not specified"); } if (string.IsNullOrWhiteSpace(result.Username)) { throw new ArgumentException("Uid not specified"); // TODO - is this mandatory? What about Integrated Security? } if (string.IsNullOrWhiteSpace(result.Database)) { throw new ArgumentException("Database not specified"); } if (result.LoginTimeout < 1) { throw new ArgumentException("Login timeout must be at least 1 second"); } if (result.ConnectionIdleTimeout < 0) { throw new ArgumentException("ConnectionIdleTimeout must be at least 0 seconds"); } if (result.ConnectionLifetime < 0) { throw new ArgumentException("Connection Lifetime must be at least 0 seconds"); } if (result.PacketSize < 256) { throw new ArgumentException("PacketSize must be at least 256 (bytes)"); } if (result.Pooling && result.MaxPoolSize <= 0) { throw new ArgumentException("Max Pool Size must be at least 1 when Pooling is enabled"); } if (result.MinPoolSize > result.MaxPoolSize) { throw new ArgumentException("Min Pool Size must be at most the same as Max Pool Size"); } return(result); }