#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Data.Common; using System.Data.SqlClient; using HeuristicLab.DataImporter.DbExplorer.Interfaces; namespace HeuristicLab.DataImporter.DbExplorer.MsSqlServer { public class SqlServerConnection : DbConnectionBase { private string constring; public SqlServerConnection(string serverName, string databaseName, string userName, string password, bool useWindowsAuthentication) : base(databaseName, userName, password) { this.serverName = serverName; this.useWindowsAuthentication = useWindowsAuthentication; this.constring = "Server=" + serverName + "; Initial Catalog=" + databaseName; if (useWindowsAuthentication) this.constring += @"; Integrated Security=true"; else this.constring += "; User ID=" + userName + "; Password=" + password; } private string serverName; public string ServerName { get { return this.serverName; } set { this.serverName = value; } } private bool useWindowsAuthentication; public bool UseWindowsAuthentication { get { return this.useWindowsAuthentication; } set { this.useWindowsAuthentication = value; } } public override DbConnection CreateConnection() { return new SqlConnection(constring); } public override bool TestConnection() { System.Data.Common.DbConnection con = null; try { con = this.CreateConnection(); con.Open(); this.ErrorMessage = ""; return true; } catch (SqlException e) { this.ErrorMessage = e.Message; this.CreateConnection().Close(); return false; } finally { if (con != null) con.Close(); } } } }