为什么我的代码扔在SQL Server中的语法错误?

时间:2019-01-31 20:11:58

标签: c# sql-server

  

类型的未处理的异常 'System.Data.SqlClient.SqlException' 出现在system.data.dll

     

额外的资料:邻近 '名称' 的语法不正确

问题是在更新上表数据库

public partial class Form1 : Form
{
    SqlConnection Conn = new SqlConnection(@"Data Source=DESKTOP-R0N4ID3;Initial Catalog=Userinfo;Integrated Security=True");
    string Gender;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'userinfoDataSet.tableUserinfo' table. You can move, or remove it, as needed.
        this.tableUserinfoTableAdapter.Fill(this.userinfoDataSet.tableUserinfo);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Conn.Open();

        String Query_Insert = "insert into tableUserinfo" +
                               "(Name,Email,Salary,Gender,Speciality)" +
                               "values ('" + textName.Text + "','" + textEmail.Text + "'," + textSalary.Text + ",'" + Gender + "','" + listspeciality.SelectedItem+ "')";
        SqlCommand comm = new SqlCommand(Query_Insert, Conn);

        comm.ExecuteNonQuery();

        Conn.Close();
    }

    private void radioMeal_CheckedChanged(object sender, EventArgs e)
    {
        Gender = radioMeal.Text;
    }

    private void radioFemale_CheckedChanged(object sender, EventArgs e)
    {
        Gender = radioFemale.Text;
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        String Name = DgvUinfo.CurrentRow.Cells[0].Value.ToString().Trim();
        String Email = DgvUinfo.CurrentRow.Cells[1].Value.ToString().Trim();
        String Salary = DgvUinfo.CurrentRow.Cells[2].Value.ToString().Trim();
        String Gender1 = DgvUinfo.CurrentRow.Cells[3].Value.ToString().Trim();
        String Speciality =  DgvUinfo.CurrentRow.Cells[4].Value.ToString().Trim();

        //massage value
        if (DgvUinfo.Columns[e.ColumnIndex].Name == "ColEdit")
        {
            DialogResult ResultMesupd = MessageBox.Show("Are you sure want update this record ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (ResultMesupd == DialogResult.Yes)
            {
                Conn.Open();
                String Query_Update = "update tableUserinfo"
                                      + "set Name ='" + DgvUinfo.CurrentRow.Cells[0].Value.ToString().Trim()
                                      + "', Email = '" + DgvUinfo.CurrentRow.Cells[1].Value.ToString().Trim()
                                      + "', Salary = " + DgvUinfo.CurrentRow.Cells[2].Value.ToString().Trim()
                                      + ", Gender = '" + DgvUinfo.CurrentRow.Cells[3].Value.ToString().Trim()
                                   + "', Speciality = '" + DgvUinfo.CurrentRow.Cells[4].Value.ToString().Trim() + "'"
                                   + " where Name='" + Name
                                   + "' and Email='" + Email
                                   + "' and Salary=" + Salary
                                   + " and Gender='" + Gender1
                                   + "' and Speciality='" + Speciality + "'";

                SqlCommand comm = new SqlCommand(Query_Update, Conn);
                comm.ExecuteNonQuery();
                Conn.Close();
            }
        }
    }
}

0 个答案:

没有答案
相关问题