Two ways to get connectionstring case details

Time:2022-7-25

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>  

See the following for the added position

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

    <connectionStrings/><!--1.-->
    <system.web>
        <!-- 
            Set compilation debug= "true" to insert debugging symbols
            In the compiled page. But because this will 
            Affect performance, so only use this value during development 
            Set to true.
        -->
        <compilation debug="false"></compilation>
        <!--
            ASP can be configured through the <authentication> section Net for 
            Identify the user's
            Secure authentication mode. 
        -->
        <authentication mode="Windows" />
        <!--
            If an unhandled error occurs during the execution of the request,
            Then the corresponding processing steps can be configured through the <customerrors> section. Specifically,
            This section allows developers to configure
            HTML error page to display
            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>

Whether the database connection is successful

using System. Data; using Oracle.DataAccess. Client;  // note the addition of 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))
{
  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)
{// the connection succeeds and the data is 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; }
}

This is the end of this article about two ways to get connectionstring. For more information about the two ways to get connectionstring, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]