我想使用C#连接到数据库,但是我不知道自己在做什么错。
我已经尝试过此代码
/*private string connectionString;
private string server = "sql7.freemysqlhosting.net";
private string database = "**********";
private string username = "**********";
private string password = "**********";
SqlConnection connection;*/
private void LoginButton_Click(object sender, EventArgs e)
{
}
private void RegisterButton_Click(object sender, EventArgs e)
{
connectionString = @"Data Source=sql7.freemysqlhosting.net;Initial Catalog=<database>;User ID=<username>;Password=<password>";
connection = new SqlConnection(connectionString);
try
{
connection.Open();
MessageBox.Show("Connected to database");
}
catch(Exception ex)
{
MessageBox.Show("Error : "+ex);
}
finally
{
connection.Close();
}
}
}
}
它抛出一个错误,提示“错误40:无法打开与SQL Server的连接”。我试图打开任何SQL服务,但是没有用。