且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据从数据库传输到文本框?

更新时间:2023-02-10 15:17:33

你几乎拥有它。这个代码是你从某个地方复制的吗?



要放入文本框,你可以:

You almost have it. Is this code you copied from somewhere?

To put in textboxes you do:
myprofileCountry_txt.Text = country;


我写了这段代码。我收到了错误行:

I wrote this codes. I received the error line:
read = cmd.ExecuteReader();





共有4种表格。全部互连。



There are a total of 4 forms. All interconnected.

public Form1()
        {
            InitializeComponent();

            frm2 = new Form2();
            frm3 = new Form3();
            frm4 = new Form4();
            frm2.frm1 = this;
            frm3.frm1 = this;
            frm4.frm1 = this;

        }





我用各种形式写的相同:



I wrote the same in all forms:

public Form1 frm1;
        public Form2 frm2;
        public Form3 frm3;
        public Form4 frm4;





有报名表和将成员添加到数据库表单。单击一个按钮打开Form4(我的个人资料)。在Form4中,在写入数据时注册。对不起,发给我不明白的代码。抱歉我的英语:)



There are entry form and add members to the database form. When the click a button open the Form4(My Profile). In the Form4, sign up, while writing data. Sorry, send you the code I did not understand. And sorry for my English :)


问题与您的sql语句有关,更改

issue is with your sql statement, change
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE id, username, password, email, secureword, name, surname, age, gender, country", conn);



to


to

wanted = frm1.loginUsername_txt.Text;
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE username ='"+wanted +"'", conn);



你***检查一下sql的语法 [ ^ ]并且还将字符串与sql语句连接不安全,它将打开sql注入atacks。我会使用参数,如果它只是用于验证用户是否存在;示例代码:




you better check the sql where syntax [^] and also concatenating strings not safe way with sql statements, it will open sql injection atacks. I would use parameters and if it it is only for validating user exist or not; sample code:

public bool ValidateUser(string username, string password)
{
    int count = 0;
    string query = "SELECT count(*) FROM users WHERE username = @user AND password = @pass";
    using(MySqlConnection conn = new MySqlConnection("server=localhost;database=naperva;user=root;password="))
    using (MySqlCommand cmd = new MySqlCommand(query, conn))
    {
        cmd.Parameters.Add(new MySqlParameter("@user", username));
        cmd.Parameters.Add(new MySqlParameter("@pass ", password));

        connector.Open();
        count = int.Parse(cmd.ExecuteScalar().ToString());
    }

    return count > 0;
}