Detailed explanation of two ways to obtain connectionString

  • 2021-12-04 09:49:50
  • OfStack

Two Ways to Get connectionString

1.


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

<connectionStrings>  

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

  providerName="System.Data.OracleClient" />  

</connectionStrings>  

2.


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

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

Add location as shown below


<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings><!--2.--><add key="ConnectionString" value="***"></add></appSettings>

    <connectionStrings/><!--1.-->
    <system.web>
        <!-- 
             Settings  compilation debug="true"  You can insert debug symbols into 
             In the compiled page. But because this will  
             Impact performance, so this value is only used during development  
             Set to  true . 
        -->
        <compilation debug="false"></compilation>
        <!--
             Pass  <authentication>  Section can be configured  ASP.NET  Used for  
             Object that identifies the incoming user 
             Secure authentication mode.  
        -->
        <authentication mode="Windows" />
        <!--
             If an unhandled error occurs during the execution of the request, 
             Then pass the  <customErrors>  Section can configure the corresponding processing steps. Specifically, 
             This section allows developers to configure 
             To display  html  Error page 
             To replace the error stack trace. 
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>

Is the database connection successful


using System.Data;using Oracle.DataAccess.Client;  // Note the addition Oracle Reference to    ; SqlServer Bring your own     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))
{
    oracleConnection.Open();
    OracleCommand selectCommand = new OracleCommand(sql, oracleConnection);
    OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(selectCommand);

    oracleDataAdapter.Fill(dataSet);
}
if (dataSet.Tables.Count > 0 && dataSet.Tables[0] != null && dataSet.Tables[0].Rows.Count > 0)
{   // Connection succeeded and data returned 
    foreach (DataRow dr in dataSet.Tables[0].Rows)
    {
          listzh.Add(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; }
}

Related articles: