有些事情出错我在MysqlDB中编辑用户信息&& C#

时间:2017-09-24 02:24:19

标签: c# mysql

嘿,伙计们正在一个简单的教育中心经理工作

所以当我开始编辑任何用户信息时,所有保存在数据库中的用户(MySQL数据库)都会更改为相同的名称,年龄,时间,类除了地址

喜欢这个屏幕截图

Screen shot 1

Screen shot 2 the result

这是我的代码

Form1.cs的

 public partial class Form1 : Form
{
    DateTime datetime = DateTime.Now;
    Students st = new Students();

    public Form1()
    {
        InitializeComponent();
        FILLDGV();
    }


   public void FILLDGV() {

        dataGridView1.DataSource = st.ShowData();

        dataGridView1.Columns[0].HeaderText = "ID";
        dataGridView1.Columns[1].HeaderText = "Name";
        dataGridView1.Columns[2].HeaderText = "Age";
        dataGridView1.Columns[3].HeaderText = "Address";
        dataGridView1.Columns[4].HeaderText = "Class";
        dataGridView1.Columns[5].HeaderText = "Inserted Time";


    }

    private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    } 
    private void button3_Click(object sender, EventArgs e) { //Button to Insert Data
        try
        {

            st.AddStudent(textBox1.Text,Convert.ToInt32(textBox2.Text), textBox3.Text, textBox4.Text, datetime.ToString());

            FILLDGV();
            MessageBox.Show("Data Inserted Successfully");

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();

        }
        catch (Exception) { MessageBox.Show("Oops , Something went Wrong", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error ); }

    }

    private void button2_Click(object sender, EventArgs e) //button to Edit Data
    {

        try
        {
            st.EditStudent(textBox1.Text, Convert.ToInt32(textBox2.Text), textBox3.Text, textBox4.Text,dataGridView1.CurrentRow.Cells[5].Value.ToString(), Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));


            FILLDGV();
            MessageBox.Show("Edit Successfully");

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();
        }
        catch (Exception)
        {
            MessageBox.Show("Oops , Something went Wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


    private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e) // data grid view to Display data
    {
        textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        textBox4.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();

    }

    private void تسجيلخروجToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

    private void button4_Click(object sender, EventArgs e) // this Button to Delete Data 
    {

        try
        {



            st.DelStudent(Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));
            FILLDGV();
            MessageBox.Show("Delete Successfully");

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox1.Focus();


        }
        catch (Exception) { MessageBox.Show("Oops , Something went Wrong", "Error", MessageBoxButtons.OK,MessageBoxIcon.Information); }
    }
}

Student.cs

 class Students
{

    MySqlConnection con = new MySqlConnection(@"Server=localhost;Database=stu_info;uid=root;pwd=root;");


    public DataTable ShowData()
    {

        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM firstc", con);
        da.Fill(dt);
        return dt;


    }

    public void AddStudent(string name, int age, string address, string sclass,string date)
    {


            MySqlCommand cmd = new MySqlCommand("insert into firstc(sname,sage,saddress,sclass,DateT) values(@sname,@sage,@saddress,@sclass,@DateT)", con);

            cmd.Parameters.AddWithValue("@sname", name);
            cmd.Parameters.AddWithValue("@sage", age);
            cmd.Parameters.AddWithValue("@saddress", address);
            cmd.Parameters.AddWithValue("@sclass", sclass);
            cmd.Parameters.AddWithValue("@DateT",date);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

    }

    public void EditStudent(string name, int age, string address, string sclass,string date, int id)
    {

        MySqlCommand cmd = new MySqlCommand("update firstc set sname=@sname,sage=@sage,sclass=@sclass where @sid", con);

        cmd.Parameters.AddWithValue("@sname", name);
        cmd.Parameters.AddWithValue("@sage", age);
        cmd.Parameters.AddWithValue("@saddress", address);
        cmd.Parameters.AddWithValue("@sclass", sclass);
        cmd.Parameters.AddWithValue("@DateT", date);
        cmd.Parameters.AddWithValue("@sid", id);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }

    public void DelStudent(int id)
    {

        MySqlCommand cmd = new MySqlCommand("Delete from firstc where firstc.sid=@sid", con);


        cmd.Parameters.AddWithValue("@sid", id);

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

1 个答案:

答案 0 :(得分:0)

更新语句的where子句不正确,因为它不包含任何字段名称,只包含值。这样所有记录都会更新。根据{{​​1}}语句,您需要将delete添加到firstc.sid=子句中:

where