连接字符串应用程序配置文件到cs文件 [英] connection string app config file to cs file

查看:67
本文介绍了连接字符串应用程序配置文件到cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在窗口文件中的cs文件中获取连接字符串。如果我在app配置文件中创建connectionstring。

 <   add     name   =  ROMS.Properties.Settings。 DBRomsConnectionString    connectionstring   =  Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | \ Resesources \ DBRoms.mdb; Persist Security Info = True    providername   =  System.Data.OleDb    /   >  

当我们将项目一个系统移动到另一个系统时,它始终会产生问题所以我想设置一些特定的项目路径,因此它调用consta每次都是nt路径。

解决方案

尝试

http://www.youtube.com/watch?v=L2kjlm0gVJo [ ^ ]

如何从C#中的App.Config获取连接字符串 [ ^ ]

http://www.jsinh.in/2013/ 06 / how-to-get-sql-connection-string-from-app-config-web-config-using-c / [ ^ ]

用哟写下来你的代码

string con = ConfigurationManager.connectionstring [dbcon] .connectionstring();


你可以使用 Environment.SpecialFolder.LocalApplicationData 对于这样的事情:



  string  path = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), this  .AssemblyCompany), .AssemblyProduct); 





一个非常基本的用法:



 使用系统; 
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 Test_Path1
{
class Program
{
public static string AssemblyCompany = Company1;
public static string AssemblyProduct = App1;
静态 void Main( string [] args)
{
string connStr = @ 数据源=。\sqlexpress;初始目录= TEST; integrated security = true;;
string myweb = @ http:/ /google.com;

string allConfig = connStr + Environment.NewLine + myweb;
MyConfig cfg = new MyConfig();
cfg.Save(allConfig);

string [] rows = cfg.Read();

Console.WriteLine(rows [ 0 ]);
Console.WriteLine(rows [ 1 ]);
}
}

class MyConfig
{
public string ConfigPath
{
get
{
return Path.Combine(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Program .AssemblyCompany),Program.AssemblyProduct);
}
}

public MyConfig()
{
if (!Directory.Exists( this .ConfigPath))
Directory.CreateDirectory( .ConfigPath);
}

public void 保存( string config)
{
使用(StreamWriter s = StreamWriter(Path.Combine( this .ConfigPath, MyConfig.txt)))
{
s.Write(config);
}
}

public string []阅读()
{
使用(StreamReader s = new StreamReader(Path。合并( this .ConfigPath, MyConfig.txt)))
{
return s.ReadToEnd()。Split( new string [] {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
}
}

}
}


How can I get a connection string in cs file in window file. If I create connectionstring in app config file .

<add name="ROMS.Properties.Settings.DBRomsConnectionString" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Resources\DBRoms.mdb;Persist Security Info=True" providername="System.Data.OleDb" />

Always it creates problem when we move project one system to another system so I want to set some specific path to project so it call constant path every time.

解决方案

Try
http://www.youtube.com/watch?v=L2kjlm0gVJo[^]
How to get Connection String from App.Config in C#[^]
http://www.jsinh.in/2013/06/how-to-get-sql-connection-string-from-app-config-web-config-using-c/[^]


Write it in your code
string con =ConfigurationManager.connectionstring[dbcon].connectionstring();


you can use "Environment.SpecialFolder.LocalApplicationData" for such things:

string path = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),this.AssemblyCompany), this.AssemblyProduct);



A very basic usage:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_Path1
{
    class Program
    {
        public static string AssemblyCompany = "Company1";
        public static string AssemblyProduct = "App1";
        static void Main(string[] args)
        {
            string connStr = @"Data Source=.\sqlexpress; initial catalog=TEST; integrated security=true;";
            string myweb = @"http://google.com";

            string allConfig = connStr + Environment.NewLine + myweb;
            MyConfig cfg = new MyConfig();
            cfg.Save(allConfig);

            string[] rows = cfg.Read();

            Console.WriteLine(rows[0]);
            Console.WriteLine(rows[1]);
        }
    }

    class MyConfig
    {
        public string ConfigPath
        {
            get
            {
                return Path.Combine(Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    Program.AssemblyCompany), Program.AssemblyProduct);
            }
        }

        public MyConfig()
        {
            if (!Directory.Exists(this.ConfigPath))
                Directory.CreateDirectory(this.ConfigPath);
        }

        public void Save(string config)
        {
            using (StreamWriter s = new StreamWriter(Path.Combine(this.ConfigPath, "MyConfig.txt")))
            {
                s.Write(config);
            }
        }

        public string[] Read()
        {
            using (StreamReader s = new StreamReader(Path.Combine(this.ConfigPath, "MyConfig.txt")))
            {
                return s.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

    }
}


这篇关于连接字符串应用程序配置文件到cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆