如何将数据从数据库传输到文本框?
我不能将数据从数据库传输到文本框。请帮帮我。
I can not do transferring data to textboxes from database. Help me please.
="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public Form1 frm1;
public Form2 frm2;
public Form3 frm3;
public Form4 frm4;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
try
{
int a = 0;
string wanted, id, username, password, email, secureword, name, surname, age, gender, country;
MySqlConnection conn = new MySqlConnection("server=localhost;database=naperva;user=root;password=");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE id, username, password, email, secureword, name, surname, age, gender, country", conn);
MySqlDataReader read = null;
conn.Open();
read = cmd.ExecuteReader();
wanted = frm1.loginUsername_txt.Text;
while (read.Read())
{
id = read["id"].ToString();
username = read["username"].ToString();
password = read["password"].ToString();
email = read["email"].ToString();
secureword = read["secureword"].ToString();
name = read["name"].ToString();
surname = read["surname"].ToString();
age = read["age"].ToString();
gender = read["gender"].ToString();
country = read["country"].ToString();
if (wanted == username)
{
id = myprofileID_txt.Text;
username = myprofileUsername_txt.Text;
password = myprofilePassword_txt.Text;
email = myprofileEmail_txt.Text;
secureword = myprofileSecureWord_Txt.Text;
name = myprofileName_txt.Text;
surname = myprofileSurname_txt.Text;
gender = myprofileGender_comboBox.Text;
age = myprofileAge_txt.Text;
country = myprofileCountry_txt.Text;
a = 1;
}
if (a != 1)
{
MessageBox.Show("Not found");
read.Close();
conn.Close();
}
}
}
catch (Exception)
{
MessageBox.Show("DATABASE ERROR");
}
}
}
}
你几乎拥有它。这个代码是你从某个地方复制的吗?
要放入文本框,你可以:
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;
}