简体   繁体   English

VB.NET应用程序中的连接字符串

[英]Connection String in a VB.NET Application

I used to work only with asp.net websites before. 我以前只在asp.net网站上工作。 I know that in ASP.NET website, the connection string is found in the web.config file. 我知道在ASP.NET网站中,连接字符串位于web.config文件中。

My problem is now I have started working with VB.NET applications which needed to be interfaced to a database. 我的问题是,现在我已经开始使用需要连接到数据库的VB.NET应用程序。 How is a connection string created and where should I put it? 如何创建连接字符串,我应该在哪里放置它?

Thanks! 谢谢!

Here's the whole app.config file: 这是整个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>

Ok here is an axample : 1- Your app.config should look like this : 好的,这里是一个示例: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- and in your code you can access the connectionsttring this way : 2-并且在您的代码中,您可以通过以下方式访问连接:

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

go try It ;) 去试试吧;)

The other answers tell you where to put the connection string: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx 其他答案告诉您将连接字符串放在何处: http : //msdn.microsoft.com/zh-cn/library/ms254494(v=vs.80).aspx

This is the easiest way to create a Connection String. 这是创建连接字符串的最简单方法。

a) Create a Text file, rename the extension from TXT to UDL, press enter. a)创建一个文本文件,将扩展名从TXT重命名为UDL,然后按Enter。

b) Double click the UDL file and choose OLEDB Provider For SQL Server > Next > type the Database Server name > Select the database and click Test Connection. b)双击UDL文件,然后为SQL Server选择OLEDB Provider>下一步>键入数据库服务器名称>选择数据库,然后单击测试连接。

在此处输入图片说明

c) When the Test passes, close the UDL file and open it with notepad, the bold lines are the connection string: c)当测试通过时,关闭UDL文件并用记事本打开它,粗线是连接字符串:

[oledb] ; [oledb]; Everything after this line is an OLE DB initstring 该行之后的所有内容都是OLE DB初始化字符串
Provider=SQLOLEDB.1; Provider = SQLOLEDB.1; Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YourDatabase;Data Source=SQLEXPRESS 集成安全性= SSPI;持久性安全信息= False;初始目录=您的数据库;数据源= SQLEXPRESS

Non ASP.NET application can also use config file, it is just not named web.config . 非ASP.NET应用程序也可以使用配置文件,它只是没有命名为web.config This file has exactly the same structure you know from ASP.NET including ConnectionStrings section. 该文件的结构与您从ASP.NET知道的完全相同,包括ConnectionStrings部分。 You can copy content from web.config and paste sections you need into app.config . 您可以从web.config复制内容,然后将需要的部分粘贴到app.config In project it will show as app.config but in the bin folder it is named after the executable file name (with exe extension) plus ".config". 在项目中,它将显示为app.config但在bin文件夹中,它以可执行文件名(带有exe扩展名)和“ .config”命名。

To create this file, go to project's Add -> New Item and select Application Configuration File. 要创建此文件,请转到项目的“ Add -> New Item然后选择“应用程序配置文件”。 To get access to your connection strings create/copy <ConnectionString> section into <configuration> section and then use this code: 要访问您的连接字符串,请在<configuration>部分中创建/复制<ConnectionString> <configuration>部分,然后使用以下代码:

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

You need to add reference to `System.Configuration' assembly. 您需要添加对“ System.Configuration”程序集的引用。

If you are working on a webapp project It's the same ! 如果您正在处理webapp项目,那就一样! you can put that in the web.config file , and if you project is win app you can add an app.config file to your project and pu the connection string in there ! 您可以将其放在web.config文件中,如果您的项目是win app,则可以将app.config文件添加到您的项目中,然后在其中输入连接字符串!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM