【问题标题】:Connection String in a VB.NET ApplicationVB.NET 应用程序中的连接字符串
【发布时间】:2012-05-04 00:48:47
【问题描述】:

我以前只使用过asp.net 网站。我知道在 ASP.NET 网站中,连接字符串是在 web.config 文件中找到的。

我的问题是现在我已经开始使用需要连接到数据库的 VB.NET 应用程序。连接字符串是如何创建的,应该放在哪里?

谢谢!

这是整个 app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <connectionStrings>
    <add name="dbAsthmaConnectionString" connectionString="Data Source=9300-00\SQLEXPRESS;Initial Catalog=dbStore;Persist Security Info=True;User ID=johnsmith;Password=1234" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
         <listeners>
           <add name="FileLog"/>
             <!-- Uncomment the below section to write to the Application Event Log -->
             <!--<add name="EventLog"/>-->
         </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
           initializeData="FileLogWriter"/>
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
</configuration>

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    好的,这是一个例子: 1- 你的 app.config 应该是这样的:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="Amlakconn" connectionString ="Data Source=KHASHAYAR-PC\SQLEXPRESS;Initial Catalog=Amlak;Integrated Security=True"/>
      </connectionStrings>
    </configuration>
    

    2- 在您的代码中,您可以通过这种方式访问​​连接字符串:

    private string conn = ConfigurationManager.ConnectionStrings["Amlakconn"].ConnectionString;
    

    去试试吧;)

    【讨论】:

      【解决方案2】:

      其他答案告诉您将连接字符串放在哪里:http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx

      这是创建连接字符串的最简单方法。

      a) 创建一个文本文件,将扩展名从 TXT 重命名为 UDL,按回车键。

      b) 双击 UDL 文件并选择 OLEDB Provider For SQL Server > 下一步 > 键入数据库服务器名称 > 选择数据库并单击测试连接。

      c) 测试通过后,关闭UDL文件,用记事本打开,粗线为连接字符串:

      [oledb] ;此行之后的所有内容都是 OLE DB 初始化字符串
      Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YourDatabase;Data Source=SQLEXPRESS

      【讨论】:

        【解决方案3】:

        如果你在做一个 webapp 项目,也是一样的!你可以把它放在 web.config 文件中,如果你的项目是 win 应用程序,你可以在你的项目中添加一个 app.config 文件并将连接字符串放入其中!

        【讨论】:

        • 我所做的是将 web.config 文件中的所有内容都复制到了 app.config 文件中。但是,当我尝试添加表适配器时,它看不到连接字符串。可能是什么错误?
        • 我向您发布了一个示例源代码,关于如何在 app.config 文件中编写连接字符串以及如何在后面的代码中使用它!你用过吗?!
        【解决方案4】:

        非 ASP.NET 应用程序也可以使用配置文件,只是没有命名为web.config。该文件的结构与您从 ASP.NET 中知道的完全相同,包括 ConnectionStrings 部分。您可以从web.config 复制内容并将您需要的部分粘贴到app.config。在项目中,它将显示为app.config,但在 bin 文件夹中,它以可执行文件名(带有 exe 扩展名)加上“.config”命名。

        要创建此文件,请转到项目的Add -&gt; New Item 并选择应用程序配置文件。要访问您的连接字符串,请创建/复制 &lt;ConnectionString&gt; 部分到 &lt;configuration&gt; 部分,然后使用此代码:

        string conStr = ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString;
        SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conStr);
        

        您需要添加对“System.Configuration”程序集的引用。

        【讨论】:

        • 谢谢,但是当我尝试创建表适配器时,连接字符串没有出现。我将连接字符串放在
        • 只需从您的 web.confg 复制并粘贴整个 部分。
        • 我应该将它粘贴到 app.config 文件的哪个部分?有等……
        • 更新了我的答案,详细说明了如何操作。
        • 嗨,我的连接字符串如下所示,这是我在 中粘贴的内容:
        猜你喜欢
        • 1970-01-01
        • 2011-02-12
        • 2012-08-11
        • 2017-11-09
        • 2012-05-14
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 1970-01-01
        相关资源
        最近更新 更多