I try to connect to sql-server by calling this class:
class DataCon
{
public static SqlConnection Connection { get; private set; }
public static void connectDB(string ip, string dbName, string user, string pw)
{
string connectionSt = "Server="+ip+";Database="+dbName+";User Id="+user+";Password="+pw+";";
ConnectToSql(connectionSt);
}
private static void ConnectToSql(string connectionSt) {
Connection = new SqlConnection(connectionSt);
Connection.Open();
}
public static void connectDB(string ip, string dbName)
{
string connectionSt = "Server="+ip+";Database="+dbName+";Trusted_Connection=True;";
ConnectToSql(connectionSt);
}
}
It work when using Windows Authentication but the Server Authentication that give login failed for user 'sa'.
On Sql server management studio I could login successfully with both Windows and Server Authentication with 'sa'.
This is the form I work with:
private void btnConfirm_Click(object sender, EventArgs e)
{
try
{
string dbsName = "dbsStudent";
string ip = "localhost";
string user = txtUser.Text;
string pw = txtUser.Text;
if (cbAuthentication.SelectedIndex == 0)
DataCon.connectDB(ip,dbsName);
else
DataCon.connectDB(ip,dbsName,user,pw);
this.Hide();
new ViewSt().Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}