2012-05-02 42 views
1

我以前只能用asp.net网站工作。我知道在ASP.NET网站中,连接字符串可以在web.config文件中找到。VB.NET应用程序中的连接字符串

我的问题是现在我已经开始使用需要连接到数据库的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> 

回答

2

确定这里是一个axample: 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 - 和你的代码,你可以访问connectionsttring这样:

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

去试试吧;)

0

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

要创建此文件,请转到项目的Add -> New Item并选择应用程序配置文件。为了让你的连接字符串访问创建/复​​制<ConnectionString>节到<configuration>部分,然后使用此代码:

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

您需要添加参考`System.Configuration”组装。

+0

谢谢但我试图创建表适配器时,连接字符串不显示。我将连接字符串放在中 –

+0

只需从web.confg中复制并粘贴整个节。 –

+0

我应该在哪个部分的app.config文件中粘贴它?有等... –

0

如果你正在开发一个webapp项目,它是一样的!你可以把它放在web.config文件中,如果你的项目是win应用程序,你可以添加一个app.config文件到你的项目中并在那里输入连接字符串!

+0

我所做的是我将我的web.config文件中的所有内容都复制到了app.config文件中。但是,当我尝试添加表格适配器时,它看不到连接字符串。什么可能是错误? –

+0

我发布了一个关于如何在app.config文件中编写连接字符串的示例源代码,以及如何在代码中使用它!你用那个吗?! – Karamafrooz

2

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

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

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

b)双击UDL文件并选择OLEDB Provider for SQL Server> Next>键入数据库服务器名称>选择数据库并单击Test Connection。

enter image description here

c)如果测试通过,关闭UDL文件和记事本打开它,粗线是连接字符串:

[OLEDB] ;此行后的所有内容都是OLE DB initstring
Provider = SQLOLEDB。1; 集成安全= SSPI;坚持安全信息=假;初始目录= YourDatabase;数据源= SQLEXPRESS

+0

这么好,很容易... +1! –

相关问题