dright dright - 2 months ago 8
C# Question

Database created in SQL Server Management Studios cannot be found by Visual Studio 2010

I created a new database (

AC_2012
) on my localhost in SQL Server 2008 Management Studio.

I'm trying to connect it via connection string in
web.config
in Visual Studio 2010 Premium. It won't pick it up.

<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="AC_2012"
connectionString="server=.\SQLEXPRESS;database=AC_2012; integrated security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>


And here's how I'm trying to call the connection string.. yes it isn't finished..

public static void storedProcedure(string[] paramName, string[] paramValue, string sproc)
{
SqlConnection conn = null;
SqlDataReader reader = null;

try
{
string connStr = ConfigurationManager.ConnectionStrings["AC_2012"].ConnectionString;
conn = new SqlConnection(connStr);
reader = new SqlDataReader();

DataTable dt = new DataTable();

conn.Open();
SqlCommand cmd = new SqlCommand(sproc, conn);

for (int x = 0; x < paramName.Count(); x++)
{
cmd.Parameters.Add(new SqlParameter(paramName[x], paramValue[x]));
}

reader = cmd.ExecuteReader();

while (reader.Read())
{
}
}
catch (Exception e)
...

JNK JNK
Answer

Database names with an underscore _ need to be delimited with square braces [].

Try

connectionString="server=.\SQLEXPRESS;database=[AC_2012]; integrated security=True; "

Alternatively, just don't put an underscore in your database name.