0

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#

ByronMcGrath
  • 365
  • 2
  • 3
  • 20
  • Well, store it in your web.config as a connection string and use it in your application. use www.connectionstrings.com as help to create it. – DatRid Aug 27 '14 at 13:54
  • 1
    Oh this should also help you: http://stackoverflow.com/questions/6134359/read-connection-string-from-web-config (The second answer shows how you can call the connectionstring.) If you have specific questions just ask. But there are enough examples on google for this question which would fit. – DatRid Aug 27 '14 at 14:00

2 Answers2

2

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

Peter Huys
  • 86
  • 2
1

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

Izzy
  • 6,740
  • 7
  • 40
  • 84