简体   繁体   English

无效的对象名称“ DB”

[英]Invalid object name 'DB'

I am a beginner and would like to implement basic login page using C#.net - web application. 我是初学者,想使用C#.net-Web应用程序实现基本的登录页面。 I have created database successfully and can test connection worked as well. 我已经成功创建了数据库,并且可以测试连接是否正常工作。 I get the below error message when the control hits ExecuteReader() command. 当控件单击ExecuteReader()命令时,出现以下错误消息。

Error: 错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

Additional information: Invalid object name 'UserDB'. 附加信息:无效的对象名称'UserDB'。

Program: 程序:

string LoginDbCS =ConfigurationManager.ConnectionStrings["UserDB"].ConnectionString;
string ID = TextBoxUserId.Text;
SqlConnection Conn = new SqlConnection(LoginDbCS);
{
    string sCmdStr = "select Password from UserDB where UserID=@ID";
    SqlCommand sCmd = new SqlCommand(sCmdStr, Conn);

    SqlParameter param = new SqlParameter();
    param.ParameterName = "@ID";
    param.Value = TextBoxUserId.Text;

    sCmd.Parameters.Add(param);
    Conn.Open();

    SqlDataReader sReader = sCmd.ExecuteReader();
    sReader.Read();

    if ((string)sReader["Password"]==TextBoxPassword.Text)
    {
        Label5.Text = "Login Successful";
    }
    else
    {
        Label5.Text = "No Records found";
    }

    sReader.Close();
    Conn.Close();
}

} }

Looking forward for suggestions to fix this crash. 期待获得解决此崩溃的建议。

Well, Your question was answered in the comments by sohaiby , but there are some deeper issues with your code, and you should fix those. 好的,您的问题已在sohaiby的注释中得到了回答 ,但是您的代码存在一些更深层次的问题,因此您应该解决这些问题。
I believe that it's easier to learn good coding standards as a beginner then as someone that has experience with bad coding standards, since people often go to what is familiar when thing go bad. 我相信,作为一个初学者,要学习良好的编码标准要比有一个糟糕的编码标准的人学习起来容易,因为当事情变糟时,人们通常会去熟悉的事物。

So, here are some bad practices that you should replace ASAP: 因此,这是您应尽快替换的一些不良做法:

  • Using reserved words as objects names. 使用保留字作为对象名称。 See my answer to this question for the specifics of the method I use to name sql objects. 有关我用来命名sql对象的方法的详细信息, 请参见我对该问题的回答 Other people might use other methods. 其他人可能会使用其他方法。 you don't have to adopt mine, but it's the best I know of. 您不必采用我的,但这是我所知道的最好的。
  • Retrieving all the passwords from the database and check for the correct password in code. 从数据库中检索所有密码,并在代码中检查正确的密码。 You already know what password the user entered. 您已经知道用户输入的密码。 just add it as another parameter to your sql statement and add a condition: 只需将其作为另一个参数添加到您的sql语句并添加一个条件:
    string sCmdStr = "select * from UserDB where UserID=@ID AND Password = @Password";
  • Using a reader for queries where you don't actually need the data. 使用阅读器查询实际上不需要数据的地方。
    In a login page, usually you only want to know if the user have entered the correct user name and password. 通常,在登录页面中,您只想知道用户是否输入了正确的用户名和密码。 There is no need for actual data being returned from the database (unless you want to get some details about the user, such as first / last name, last login date, etc'). 无需从数据库返回实际数据(除非您想获得有关用户的一些详细信息,例如名字/姓氏,姓氏登录日期等)。 In these cases you can probably use ExecuteScalar to get a single value from the database. 在这些情况下,您可能可以使用ExecuteScalar从数据库中获取单个值。
    If the query returns no results, the ExecuteScalar will return null. 如果查询没有返回结果,则ExecuteScalar将返回null。 read how to handle this here. 在这里阅读如何处理。 .
  • Use an IDisposable ( SqlConnection in your case) without a Using statement. 使用IDisposable(在您的情况下为SqlConnection )而不Using语句。

I hope this will help you learn better coding standards and improve your coding skills. 我希望这将帮助您学习更好的编码标准并提高您的编码技能。

Happy programming! 编程愉快!

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

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