简体   繁体   English

在app.config C#中使用连接字符串

[英]Use Connection string in app.config C#

I am using Visual Studio 2005 C#. 我正在使用Visual Studio 2005 C#。 Designing a mobile App for handheld scanners. 设计用于手持式扫描仪的移动应用程序。 At the moment I'm declaring a connection string on every form I have. 目前,我要在我拥有的每种表单上声明一个连接字符串。 I do not want to do this. 我不想这样做。 I know you can do it app.config file. 我知道您可以执行app.config文件。

I am struggling to understand how the app.config file works. 我正在努力了解app.config文件的工作方式。 I have read forums and stackoverflow posts, but I just can't find it. 我已经阅读过论坛和stackoverflow帖子,但找不到。 Can someone please help me with this? 有人可以帮我吗?

Example: app.config file in C# for SQL Server 2005 in VS 2008 示例: VS 2008中SQL Server 2005的C#中的app.config文件

But where is the app.config file? 但是app.config文件在哪里?

Using: Visual Studio 2005 .net 2.0 使用:Visual Studio 2005 .net 2.0

Screenshots: 屏幕截图:

在此处输入图片说明

在此处输入图片说明

app.config file : app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyString" 
         connectionString="User Id=christob;Password=CHRISTOB;Host=poseidon;Pooling=true;Min Pool Size=0;Max     Pool Size=100;Connection Lifetime=0;Port=1523;Sid=GLODCQA"
         providerName="CoreLab.Oracle" />
  </connectionStrings>
</configuration>



using System.Configuration;

private void btnExit_Click_1(object sender, EventArgs e)
    {
        string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["MyString"].ConnectionString;
etc........
    }

Error: 错误:

Error 1 Deployment and/or registration failed with error: 0x8973190e. 错误1部署和/或注册失败,错误:0x8973190e。 Error writing file '%csidl_program_files%\\hhrcv_app\\system.dll'. 写入文件'%csidl_program_files%\\ hhrcv_app \\ system.dll'时出错。 Error 0x80070070: There is not enough space on the disk. 错误0x80070070:磁盘上没有足够的空间。 Device Connectivity Component 设备连接组件

Why do I get this error? 为什么会出现此错误? I only get it when I use systems.configuration; 我只有在使用systems.configuration时才能得到它; But i have to? 但我不得不?

It's a file you can add to your project. 您可以将其添加到项目中。

Add => New Item => Visual C# Items => General => Application Configuration File 添加=>新项=> Visual C#项=>常规=>应用程序配置文件

If you don't have a template for it, you can create the file manually. 如果没有模板,则可以手动创建文件。 The default App.config file that Visual Studio generates contains the following text: Visual Studio生成的默认App.config文件包含以下文本:

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

Create a text file called App.config in your solution directory with the above text in it, then add it to your project as an existing item. 在解决方案目录中创建一个名为App.config的文本文件,其中包含上述文本,然后将其作为现有项添加到您的项目中。

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

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