I tried to test if my application is connected to the local database. I don't get any errors, so couldn't quite figure out why it's not working. I only get "no connection" output. I tried to debug it but get connection = null. I have SQL Server 2008 R2 (mixed authentication) and Visual Studio 2008 sp1. I tried connecting using both Windows and SQL Server authentication however neither worked.
This is my web.config
file.
<connectionStrings>
<add name="MyDbConn"
connectionString="Data Source=local;Initial Catalog=Sample;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
Default.aspx.cs
public partial class _Default: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTestDb_Click(object sender, EventArgs e)
{
try {
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Sample; Integrated Security=SSPI");
connection.Open();
if (connection != null && connection.State == ConnectionState.Closed) {
Response.Write("Connection OK!");
connection.Close();
} else {
Response.Write("No Connection!");
}
} catch {
Response.Write("No Connection!");
}
}
}
Copyright Notice:Content Author:「user3399326」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30768469/connection-to-sql-server-2008-r2-isnt-working