如何从vb.net项目中的app.config文件存储和检索ConnectionString [英] How To Store and retrive ConnectionString from app.config file in vb.net project

查看:68
本文介绍了如何从vb.net项目中的app.config文件存储和检索ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在尝试从app.config中获取连接字符串,我创建了一个连接类并声明了一个字符串,该字符串将存储从app.config文件中获取的连接字符串

Hello
I am trying to fetch connection string from app.config, i have created a connection class and declareing a string which will stores connection string fetched from app.config file

 Public Class ConnectionClassDim 

ConString As String = ConfigurationManager.ConnectionStrings ("ConString").ConnectionString
''Here It will not reading Connectionstring from App.Config What to do for it?
    

    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim da As SqlDataAdapter
   
    Public Function GetData(ByVal Query As String) As DataTable

        Dim dt As New DataTable
        Try
            con = New SqlConnection(ConString)
            con.Open()
            da = New SqlDataAdapter(Query, con)

            da.Fill(dt)
            Return dt
        Catch ex As Exception
            Return dt
        End Try
    End Function
End Class



app.config就像



the app.config is like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>
    <add name="ConString" connectionString="Data Source=DEV150;Initial Catalog=TrailDb;Integrated Security=false;User Id=sa;Password=sadev150;"

            providerName="System.Data.SqlClient"/>
  </connectionStrings>


</configuration>

推荐答案

您可以使用
System.Configuration.COnfigurationSettings.AppSettings ["Key"]
You can use
System.Configuration.COnfigurationSettings.AppSettings["Key"]



Hi
请尝试这个


在您的App.Config文件中,

Hi
Please Try this


In Your App.Config file looks like

 <configuration>
  <connectionstrings>
    <add name="DBConnectionString">
      connectionString="Data Source=Manoj;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=admin123"
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>
</configuration>




请添加参考"System.Configuration.dll"

在您的代码中




Please Add Reference "System.Configuration.dll"

IN your code

// Add NameSpaces for
using System.Configuration;

string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings[1].ToString();


将读取内容移至页面加载功能-这样,每次需要时都会完成读取.
Move the read into your page load function - that way it will be done each time you need it.


这篇关于如何从vb.net项目中的app.config文件存储和检索ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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