How to create a connection string in asp.net c#
Asked Answered
P

7

8

I am working on asp.net c# project, for connection I used:

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");

but I want to get this connection string to get configure and be like this, so can any one help how to create this kind of connection.

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);
Passover answered 1/3, 2014 at 11:27 Comment(0)
A
8

Demo :

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

Based on your question:

<connectionStrings>
    <add name="itmall" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
    </connectionStrings>

Refer links:

http://www.connectionstrings.com/store-connection-string-in-webconfig/

Retrive connection string from web.config file:

write the below code in your file where you want;

string connstring=ConfigurationManager.ConnectionStrings["itmall"].ConnectionString;

SqlConnection con = new SqlConnection(connstring);

or you can go in your way like

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);

Note:

The "name" which you gave in web.config file and name which you used in connection string must be same(like "itmall" in this solution.)

Antedate answered 1/3, 2014 at 11:42 Comment(0)
C
2

add this in web.config file

           <configuration>
              <appSettings>
                 <add key="ConnectionString" value="Your connection string which contains database id and password"/>
            </appSettings>
            </configuration>

.cs file

 public ConnectionObjects()
 {           
    string connectionstring= ConfigurationManager.AppSettings["ConnectionString"].ToString();
 }

Hope this helps.

Cordage answered 16/6, 2017 at 10:51 Comment(0)
L
1

Add this in your web.config file

<connectionStrings>
<add name="itmall" 
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-
02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Ludwigshafen answered 1/3, 2014 at 11:31 Comment(2)
But what when i copy it to another folder, cant it like where we don't have to get update each time we change it's folder?Passover
it willbe accessible from anywhere untill unless you are maintainig only one web.config file.Ludwigshafen
G
1

Add this connection string tag in web.config file:

<connectionStrings>
  <add name="itmall" 
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True"/>
</connectionStrings>

And use it like you mentioned. :)

Granulite answered 1/3, 2014 at 11:32 Comment(0)
K
1
string connectionstring="DataSource=severname;InitialCatlog=databasename;Uid=; password=;"
SqlConnection con=new SqlConnection(connectionstring)
Katakana answered 31/1, 2015 at 7:19 Comment(1)
how will you pass values for InitialCatalog, Uid, and password?Ul
C
1

It occurs when IIS is not being connected to SQL SERVER. For a solution, see this screenshot: Solution

Cortes answered 1/6, 2017 at 12:8 Comment(1)
can you please type out the solution? as some stack overflow users cant open picturesSolipsism
P
0
        // Web config file into connection string //
        <connectionStrings>
                <add name="constr" connectionString="Data Source=sourcename;Initial Catalog=database name ; Integrated Security=True;" providerName="System.Data.SqlClient"/>
            </connectionStrings>
   //end web config file //

// below this code use in web form page //
    
     protected void Page_Load(object sender, EventArgs e)
            {
                string cs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
          
                SqlConnection conn = new SqlConnection(cs);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    Response.Write("<script>alert('open')</script>");
                }
                else
                {
                    Response.Write("<script>alert('not open')</script>");
                }
    
            }
Peneus answered 14/6 at 6:36 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Pluto

© 2022 - 2024 — McMap. All rights reserved.