SoFunction
Updated on 2025-03-09

Detailed explanation of two ways to obtain connectionString

Two ways to get connectionString

1.

public static string connectionString = ["ConnectionString"].ConnectionString;

<connectionStrings>  

<add name="ConnectionString" connectionString="Data Source=localhost;Persist Security Info=True;User ID=***;Password=***;Unicode=True"  

  providerName="" />  

</connectionStrings>  

2.

public static string connectionString = ["ConnectionString"]; 

<add key="ConnectionString" value="server=localhost;database=***;uid=sa;pwd=***"></add>  

See below for adding location

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;configuration&gt;
    &lt;appSettings&gt;&lt;!--2.--&gt;&lt;add key="ConnectionString" value="***"&gt;&lt;/add&gt;&lt;/appSettings&gt;

    &lt;connectionStrings/&gt;&lt;!--1.--&gt;
    &lt;&gt;
        &lt;!-- 
            set up compilation debug="true" Debug symbols can be inserted
            In the compiled page。But because this will 
            Influence performance,Therefore, this value is only included during development 
            set up为 true。
        --&gt;
        &lt;compilation debug="false"&gt;&lt;/compilation&gt;
        &lt;!--
            pass &lt;authentication&gt; Sections can be configured  Used for 
            Identify the entry user
            Secure authentication mode。 
        --&gt;
        &lt;authentication mode="Windows" /&gt;
        &lt;!--
            If an unprocessed error occurs during the execution of the request,
            则pass &lt;customErrors&gt; Sections can be configured相应的处理步骤。Specifically,
            开发人员pass该Sections can be configured
            To be displayed html Error page
            Instead of error stack trace。
        &lt;customErrors mode="RemoteOnly" defaultRedirect=""&gt;
            &lt;error statusCode="403" redirect="" /&gt;
            &lt;error statusCode="404" redirect="" /&gt;
        &lt;/customErrors&gt;
        --&gt;
    &lt;/&gt;
&lt;/configuration&gt;

Is the database connection successful?

using ;using ;  //Note to add Oracle references; SqlServer comes with DataSet dataSet = new DataSet();List<ZHInfo> listzh = new List<ZHInfo>();string sql = "SELECT CODE,NAME,PHONE_NUMBER FROM ZHXX WHERE 1=1 AND ROWNUM<10 ";using (OracleConnection oracleConnection = new OracleConnection(connectionString))
{
();
OracleCommand selectCommand = new OracleCommand(sql, oracleConnection);
OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(selectCommand);

(dataSet);
}
if ( &gt; 0 &amp;&amp; [0] != null &amp;&amp; [0]. &gt; 0)
{   //The connection is successful and data is returnedforeach (DataRow dr in [0].Rows)
{
(new ZHInfo { Code = dr["Code"].ToString(), Name = dr["Name"].ToString(), PublicNumber = dr["Phone_Number"].ToString() });
}
}
public class ZHInfo
 {
    public string Code { get; set; }
    public string Name { get; set; }
    public string PublicNumber { get; set; }
}

This is the article about the detailed explanation of the two ways to obtain connectionString. For more information about the two ways to obtain connectionString, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!