ByronMcGrath
ByronMcGrath

Reputation: 375

New to c#/ Connection string

normally when I create a website I use the connection string in classic ASP and I use the following

<%
    Sub RemainingHols
        if      Session("UserID") = "" then
                response.Redirect("LoginPage.asp")
        else
        ' Initialise the db connection
        Set     objDBConn = Server.CreateObject("ADODB.Connection")
        Set     objDBCommand = Server.CreateObject("ADODB.Command")
                objDBConn.Open Application("Conn")
                objDBCommand.ActiveConnection = objDBConn
                objDBCommand.CommandText = "spHolidaysRemaining"
                objDBCommand.CommandType = adCmdStoredProc
        ' Set the parameters
                objDBCommand.Parameters.Append objDBCommand.CreateParameter("@EmployeeID", adInteger) 
                objDBCommand("@EmployeeID") = session("UserID")
        'Initialise the Recordset
        Set     objDBRS = Server.CreateObject("ADODB.RecordSet")                           
        'Execute  
                objDBRS.open objDBCommand,,adOpenForwardOnly
                Session("HollidayAllowance") = (objDBRS(0))
                Session("BookedHolidays") = (objDBRS(1)).value 
                Session("TotalHolidays") = (objDBRS(2)).value 
                Session("Date") = (objDBRS(3))
                Session("Time") = (objDBRS(4))

        'Close and Destroy Objects - Start*******************************************************
        Set     objDBCommand=nothing
                objDBConn.Close
        Set     objDBConn=nothing
        'Close and Destroy Objects - End*********************************************************
        end if
    end sub 
%>

I store the actually connection string in a global.asa page and the above in a functions page.

And I would call the sub on the page I want to functionality to perform on.

I have started to develop a website in ASP.net C# for practice and I was wondering how to do this in C#

Upvotes: 0

Views: 196

Answers (2)

Izzy
Izzy

Reputation: 6866

You'll have your connection string in the web.config

<connectionStrings><add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" /></connectionStrings>

And to read the connection string into your code, use the ConfigurationManager class

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

You can also refer to The Connection Strings Reference

Upvotes: 1

Peter Huys
Peter Huys

Reputation: 86

Common practice is to have the connection string in the connection string section in the web.config file of your application.

See http://msdn.microsoft.com/en-us/library/vstudio/ms178411%28v=vs.100%29.aspx

Upvotes: 2

Related Questions