config访问connectionString

config访问connectionString

本文介绍了如何使用winform应用程序从app.config访问connectionString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用winform应用程序从app.config访问 connectionString



我编写代码来访问 connectionString 但是它给出了错误。

How to access connectionString from app.config with winform application?

I write code to access connectionString but it gives error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace WinAppThreeTier.DAL
{
  public class UserDetailDAL
    {
      SqlConnection conn = new SqlConnection();

      public void getConnection()
      {
////1st way to access ConnectionString

      conn.ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
      //this line gives error ConfigurationManager does't exits in the namespace System.Configuration;

////2nd way to access ConnectionString

      conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["MyDBConnectionString"];
          //This line gives Warning.. System.Configuration.ConfigurationSettings.AppSettings' is obsolete: ' This method is obsolete,
          //it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

推荐答案

string connectionString = Properties.Settings.Default.MyDBConnection;

将为您提供连接字符串,并且您可以改变它:

will get you the connection string, and you can change it with:

Properties.Settings.Default.MyDBConnection = connectionString;
Properties.Settings.Default.Save();


Open the App.config put the below code
<pre lang="c#"><?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>

    <add name="Constr" connectionString="Server=.\SQLEXPRESS; Integrated Security=true;Initial Catalog=IMS"/>
  </connectionStrings>
</configuration></pre>

IDispose

<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
   public interface IDispose
    {
       void Dispose();
    }
}
</pre>





基类





Base Class

public abstract class BaseClass :IDispose
    {
       public SqlConnection sqlcon;
       public SqlCommand sqlcm;
       protected SqlDataReader sqlDr;

       public void Dispose()
       {
           sqlcon.Close();
       }
    }





方法类







Method Class


public class Method :BaseClass
    {
        public Method()
        {
            string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            sqlcon=new SqlConnection(Connectionstring);
            sqlcon.Open();

        }
        public Method(bool isMaster)
        {
            string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
            sqlcon = new SqlConnection(Connectionstring);
            sqlcon.Open();
        }
        public Method(string ConnectionString)
        {
            sqlcon = new SqlConnection(ConnectionString);
            sqlcon.Open();
        }
        public SqlDataReader ExecuteReader()
        {
            sqlcm.Connection = sqlcon;
            sqlDr = sqlcm.ExecuteReader();
            return sqlDr;
        }
        public DataTable GetDataTable()
        {
            DataTable Dt;
            DataSet Ds = FillData();
            Dt = Ds.Tables[0];
            return Dt;
        }










public class SqlDataBase : Method
    {
       public SqlDataBase()
       {
           string Connectionstring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
           sqlcon = new SqlConnection(Connectionstring);
           sqlcon.Open();
       }

       public SqlDataBase(string ConnectionString)
       {
           ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
           sqlcon = new SqlConnection(ConnectionString);
           sqlcon.Open();
       }

    }





访问连接字符串





Accessing Connection string

public static bool Insert(PersonelData person)
     {
         const string insertdetails = "exec[INSERT_PERSONEL]@sid,@FName,@MName,@LName";
         DAL.Method method = new DAL.SqlDataBase();
         method.SelectQuery(insertdetails);
         method.AddParameter("@sid", person.SID);
         method.AddParameter("@FName", person.FName);
         method.AddParameter("@MName",person.MName );
         method.AddParameter("@LName",person.LName);
         int ins = method.ExecuteQuery();

         if (ins > 0)
         {
             return true;
         }

         return false;
       }


这篇关于如何使用winform应用程序从app.config访问connectionString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:35