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)
    ...

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x