通过System.Configuration检索ConnectionString引发异常 [英] Retrieving a ConnectionString through System.Configuration Throws Exception

查看:137
本文介绍了通过System.Configuration检索ConnectionString引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序中,我试图连接到DataConnection。
该应用程序只是一个简单的登录表单,但是一旦用户单击登录按钮,它就会引发引用连接字符串的异常错误。有人可以帮忙吗?

In my C# application, i am trying to connect to a DataConnection. The application is just a simple Login form but as soon as the user clicks the login button, it throws an exception error referencing the Connection String. Can anyone help?

public void userLoginSuccessfull()
{
    try
    {
        //////////////////////////////////
        // This line is throwing the error
        //////////////////////////////////
        string connString = System.Configuration.ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;

        if (txtUsername.Text != "" & txtPassword.Text != "")
        {
            string queryText = @"SELECT Count(*) FROM Users 
                     WHERE Username = @Username AND Password = @Password";

            using (SqlConnection cn = new SqlConnection(connString))
            using (SqlCommand cmd = new SqlCommand(queryText, cn))
            {
                cn.Open();
                cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
                int result = (int)cmd.ExecuteScalar();
                if (result > 0)
                {
                    loadUserForm();
                }
            }
        }
    }
    catch (Exception ee)
    {
            Console.WriteLine(ee.StackTrace);
    }
}

这是错误


'EnviroWaste Job Logger.vshost.exe'(CLR v4.0.30319:EnviroWaste Job Logger.vshost.exe):已加载'C:\Users\listm\ \文档\Visual Studio
2013\项目\EnviroWaste作业记录器\EnviroWaste作业
记录器\bin\调试\EnviroWaste作业记录器.exe'。

'EnviroWaste Job Logger.vshost.exe' (CLR v4.0.30319: EnviroWaste Job Logger.vshost.exe): Loaded 'C:\Users\listm\Documents\Visual Studio 2013\Projects\EnviroWaste Job Logger\EnviroWaste Job Logger\bin\Debug\EnviroWaste Job Logger.exe'. Symbols loaded.

System.Configuration.dll中出现类型为'System.Configuration.ConfigurationErrorsException'的第一次机会异常

A first chance exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll

配置系统初始化失败

System.Data.dll中出现类型为'System.TypeInitializationException'的第一次机会异常

A first chance exception of type 'System.TypeInitializationException' occurred in System.Data.dll

连接字符串如下:

connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integr‌​ated Security=True"


推荐答案

您收到的错误与内容不正确的配置文件有关。您的 app.config (如果是台式机)或 web.config (如果是网络应用程序)很可能出现以下错误:

The error you got relates to a configuration file that has incorrect content. Most likely, your app.config (if desktop) or web.config (if web app) file the following error:


它的 first 元素没有< configSections> 元素。

检查该文件并确保它看起来像这样

Check that file and make sure it looks something like this

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- Section groups, and stuff like userSettings, etc  -->
    </configSections>

    <connectionStrings>
        <add name="connectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integr‌​ated Security=True"/>
    </connectionStrings>

    <!-- Possibly other stuff  -->

</configuration>

这篇关于通过System.Configuration检索ConnectionString引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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