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

编程入门 行业动态 更新时间:2024-10-22 13:58:16
本文介绍了如何使用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'

推荐答案

不要。这样做很简单! 在VS中打开您的解决方案,然后查看解决方案资源管理器窗格。 打开您的项目分支和Properties子分支。 双击settings.settings 在出现的对话框中,键入设置名称:MyDBConnection 将Type设置为String,以及用户的范围。将默认设置为工作配置字符串。 保存并关闭对话框。 在您的代码中: Don't. Do it the easy way! Open your solution in VS, and look in the Solution Explorer pane. Open your project branch, and the Properties sub branch. Double click "settings.settings" In the dialog that appears, type the name of the setting: "MyDBConnection" Set the Type to String, and the Scope to "User". Set teh default to a working config string. Save and close the dialog. In your code: 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();

如果您使用的是3层架构,请将以下代码放入DataAccessLayer(DAL)中)。 Hello if you are using 3 tier Architecture,put the below code inside your DataAccessLayer(DAL). 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?

本文发布于:2023-11-03 15:09:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555427.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   应用程序   winform   connectionString   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!