【发布时间】:2015-04-23 22:59:27
【问题描述】:
在我的 C# 应用程序中,我正在尝试连接到 DataConnection。 该应用程序只是一个简单的登录表单,但一旦用户单击登录按钮,它就会引发引用连接字符串的异常错误。有人可以帮忙吗?
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\Documents\Visual Studio 2013\Projects\EnviroWaste 作业记录器\EnviroWaste 作业 Logger\bin\Debug\EnviroWaste Job Logger.exe'。已加载符号。
System.Configuration.dll 中发生了“System.Configuration.ConfigurationErrorsException”类型的第一次机会异常
配置系统初始化失败
System.Data.dll 中发生了“System.TypeInitializationException”类型的第一次机会异常
连接字符串如下所示:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integrated Security=True"
【问题讨论】:
-
你只有部分错误,如果我们知道错误所在的行和抛出的异常会有所帮助。
-
当您收到异常时,您应该做的第一件事是阅读错误消息。
-
另外,附注...您正在以纯文本形式存储用户密码。 永远不要这样做。这对您的用户非常不负责任。密码应该被散列并且永远不能被检索,即使是你程序员也不行。
-
app.config/web.config 中的连接字符串是什么样的?
-
@MattList:恐怕这条消息可能意味着很多事情。但好消息是消息(“配置系统初始化失败”)为您提供了在 Google 上搜索的内容。看起来 Stack Overflow 上还有很多与此相关的其他问题,您也许可以找到一些东西。
标签: c# connection-string