Connection string for MS SQL

Discussion in 'ASP.NET 2.0' started by peppermint-bunny, Feb 26, 2006.

  1. Can you provide sample code showing how to connect to MS SQL using ASP.net version 2.0?

    I'd like to develop locally using the Express version but change my connection string to the "big sql version" when I upload my site.

    Many thanks.
     
  2. In my web.config

    <configuration>
    <connectionStrings>
    <add name='connection' connectionString='Data Source=sql2k502.discountasp.net;Initial Catalog=YourDBname;Persist Security Info=True;User ID=YourUserID;password=yourpassword'/>
    </connectionStrings>
    </configuration


    Your database name should look something like SQL2005_235231_yourdbname

    In code I get the connection string by using

    string ConnectionString = ConfigurationManager.ConnectionStrings['connection'].ConnectionString;

    Here is example code I have in my datalayer. I pass in the stored procedure name.

    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConnectionString);

    SqlCommand command = conn.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = procedure_name;

    conn.Open();
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
    // do some stuff with reader.getValue();
    }
    reader.Close();
    conn.Close();
    return list;
     
  3. Thanks for your quick reply! -I'll
    try it out.
     
  4. I've just tried out the connection string sample you provided. Many thanks. I needed to tweak a few bits and to remember to ensure that authentication was not set to !windows" and it works fine.So now I can use VDExpress with MS SQL 2000!
     
  5.  
  6. Since it is virtual hosting, there are lots of databases on one computer. So they pre append their code in front of the database name you choose to avoid naming conflicts.
     

Share This Page