如何在Windows应用程序中编写连接字符串 [英] how to write connection string in windows application

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

问题描述

在Web应用程序中,我们用来编写web.config文件以访问数据库连接,就像我在Windows应用程序中所希望的一样.

In web applications we use to write web.config file to access database connectivity like same way i want in windows application

推荐答案

<connectionstrings>
     <add name="MyDBConnectionString" providername="System.Data.SqlClient">
           connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
  </add></connectionstrings>




然后在c#




Then in c#

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

>


您需要在项目中具有app.config,其余部分与web.config中的相同.
You need to have app.config in the project, and rest of the part remains same as in web.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DBEntities" connectionString="metadata=res://*/TestModel.csdl|res://*/TestModel.ssdl|res://*/TestModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=127.0.0.1\Dev;Initial Catalog=TestDB;User ID=TestUser;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


在Windows应用程序中,我们有app.config文件,将以下代码添加到该xml文件中
<connectionstrings>
      <add name="MyConnectionString" providername="System.Data.SqlClient">
            connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
  </add></connectionstrings>  


要从form.cs访问它,请编写与Web应用程序.aspx.cs页面相同的代码,如下所示


To access it from form.cs write the same code as web applications .aspx.cs page as following

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

>


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

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