C#构造函数有2个参数,但声称它没有一个接受两个参数的构造函数 [英] C# Constructor has 2 arguments, but claims it does not have a constructor that takes two arguments

查看:351
本文介绍了C#构造函数有2个参数,但声称它没有一个接受两个参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是我的问题。

So here is my problem. I have a class called Login that will be used for logging in and creating new log in accounts.

我创建了一个不带参数的Login构造函数。

I've created a Login constructor that takes no arguments

public Login()
{
    _gloID = 0;
    _Username = null;
    _Password = null;
    _Note = null;
    _Active = false;
    _Status = null;
    _gvoID = 0;
    _DateModified = new DateTime(1901, 1, 1);
    _ModifiedBy = 0;
}



我还创建了一个接受两个参数的Login构造函数。
此构造函数接受用户名和密码,然后从数据库收集其余信息。

I've also created a Login constructor that takes two arguments. This constructor takes the username and password and then gathers the rest of the information from the database.

public Login(string username, string password)
{
    // Declarations
    uint gloid = 0, gvoid = 0, modifiedby = 0;
    string note = null, status = null;
    bool active = false;
    DateTime datemodified = new DateTime(1901, 1, 1);
    // Command
    string query = string.Format("SELECT glo_id, glo_note, glo_active, glo_status, gvo_id, date_modified, modified_by FROM gfrc_login" +
                                    " WHERE glo_username = '{0}' AND glo_password = '{1}'", username, password);

    try
    {
        using (conn)
        {
            conn.Open();
            cmd = new OleDbCommand(query, conn);
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                gloid = Convert.ToUInt32(rdr.GetString(0));
                note = rdr.GetString(1).ToString();
                active = Convert.ToBoolean(rdr.GetString(2));
                status = rdr.GetString(3).ToString();
                gvoid = Convert.ToUInt32(rdr.GetString(4));
                datemodified = Convert.ToDateTime(rdr.GetString(5));
                modifiedby = Convert.ToUInt32(rdr.GetString(6));
            }
        }
    }
    finally
    {
        if (rdr != null)
            rdr.Close();
    }
    if (conn != null)
    {
        conn.Close();
    }

    _gloID = gloid;
    _Username = username;
    _Password = password;
    _Note = note;
    _Active = active;
    _Status = status;
    _gvoID = gvoid;
    _DateModified = datemodified;
    _ModifiedBy = modifiedby;
}

请注意,所有的数据库连接变量都已在类的开头。

Note that all the database connection variables have been declared at the beginning of the class.

现在,当我尝试运行以下代码时,我得到一个错误说:'登录'不包含有两个参数的构造函数。

Now when I try to run the following I get an error saying: 'Login' does not contain a constructor that takes 2 arguments.

protected void Login_Authenticate(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string password = CalculateMD5(txtPassword.Text);
    Login login = new Login(username, password);
}



编辑:FYI,我有措施防止SQL注入其余的代码。

FYI, I have measures preventing SQL injections in the rest of my code.

推荐答案

你可能是指两个不同的 Login 类。尝试指定全名(使用命名空间),看看会发生什么。

You are probably referring to two different Login classes. Try specifying the full name (with the namespace) and see what happens.

这篇关于C#构造函数有2个参数,但声称它没有一个接受两个参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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