对象类型不存在映射

时间:2016-05-17 18:43:43

标签: c# mysql sql-server-2008

    private void button5_Click(object sender, EventArgs e)
    {


        DataGridViewRow updatedrow = dataGridView1.Rows[chooseAnyRow];

        updatedrow.Cells[0].Value = SALUTATION.Text;
        updatedrow.Cells[1].Value = NAME.Text;
        updatedrow.Cells[2].Value = SEX.Text;
        updatedrow.Cells[3].Value = ETHNICITY.Text;
        updatedrow.Cells[4].Value = MARITALSTATUS.Text;
        updatedrow.Cells[5].Value = ICNUMBER.Text;
        updatedrow.Cells[6].Value = HPNUMBER.Text;
        updatedrow.Cells[7].Value = DOB.Text;
        updatedrow.Cells[8].Value = ADDRESS.Text;
        updatedrow.Cells[9].Value = STATE.Text;
        updatedrow.Cells[10].Value = CITY.Text;
        updatedrow.Cells[11].Value = POSTCODE.Text;
        updatedrow.Cells[12].Value = pictureBox1.Image;




        con = new SqlConnection(@"Data Source=dasranrajlui\sqlexpress;Initial Catalog=SESoriginal;Integrated Security=True");
        con.Open();
        com = con.CreateCommand();
        com.CommandType = CommandType.Text;
        com.CommandText = " update VoterRegistration set SALUTATION = '" + SALUTATION.Text +
                           "', NAME = '" + NAME.Text +
                           "', SEX = '" + SEX.Text +
                           "', ETHNICITY = '" + ETHNICITY.Text +
                           "', MARITALSTATUS = '" + MARITALSTATUS.Text +
                           "', IC_NUMBER = " + ICNUMBER.Text +
                           ",  HP_NUMBER = " + HPNUMBER.Text +
                           ",  DOB = '" + DOB.Text +
                           "', ADDRESS = '" + ADDRESS.Text +
                           "', STATE = '" + STATE.Text +
                           "', CITY = '" + CITY.Text +
                           "', POSTCODE = '" + POSTCODE.Text + 
                           "', VOTER_PIC = @VOTER_PIC where IC_NUMBER = " + ICNUMBER.Text;


        com.CommandType = CommandType.Text;
        com.Parameters.AddWithValue("@VOTER_PIC", pictureBox1.Image);
        com.Parameters.AddWithValue("@Salutation", SALUTATION.Text);
        com.Parameters.AddWithValue("@Name", NAME.Text);
        com.Parameters.AddWithValue("@Sex", SEX.Text);
        com.Parameters.AddWithValue("@Ethnicity", ETHNICITY.Text);
        com.Parameters.AddWithValue("@MaritalStatus", MARITALSTATUS.Text);
        com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);
        com.Parameters.AddWithValue("@HPNumber", HPNUMBER.Text);
        com.Parameters.AddWithValue("@Dob", DOB.Text);
        com.Parameters.AddWithValue("@Address", ADDRESS.Text);
        com.Parameters.AddWithValue("@State", STATE.Text);
        com.Parameters.AddWithValue("@City", CITY.Text);
        com.Parameters.AddWithValue("@PostCode", POSTCODE.Text);

        if (pictureBox1.Image != null)
        {

            ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            byte[] photo_aray = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo_aray, 0, photo_aray.Length);
            com.Parameters.AddWithValue("@VOTER_PIC", photo_aray);
        }

        try
        {
            com.ExecuteNonQuery();
            MessageBox.Show("updated...");

            SALUTATION.Text = null;
            NAME.Text = null;
            SEX.Text = null;
            ETHNICITY.Text = null;
            MARITALSTATUS.Text = null;
            ICNUMBER.Text = null;
            HPNUMBER.Text = null;
            DOB.Text = null;
            ADDRESS.Text = null;
            STATE.Text = null;
            CITY.Text = null;
            POSTCODE.Text = null;

        }

        catch (Exception EX)
        {
            MessageBox.Show(EX + "NOT Updated");
        }

        finally
        {
            con.Close();
        }

}

错误显示:

  

对象类型

不存在任何映射

是因为我的转换图像错了吗? 或者是否有另一种方法将图像更新为我的SQL? 问题是我需要更新我可以显示和更新的图像中的值,这些值可以保存到我的数据库中。

3 个答案:

答案 0 :(得分:2)

您的代码中会出现更多错误;更正后的表格如下

  1. 您正在以错误的方式使用参数化查询。您需要使用@parameterName代替传递相应的值。您的查询将打开sql Injection的大门。
  2. 导致当前错误的,之后的POSTCODE =..,您无需在最后一个列名称之后放置,
  3.   

    最后,您需要添加com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);两次,因为命令需要12个参数

    代码如下:

    com.CommandText = " update VoterRegistration set SALUTATION =@Salutation" +
                  ", NAME = @Name" +
                  ", SEX = @Sex" +
                  ", ETHNICITY = @Ethnicity" +
                  ", MARITALSTATUS = @MaritalStatus" +
                  ", IC_NUMBER = @ICNumber" +
                  ",  HP_NUMBER = @HPNumber" +
                  ",  DOB = @Dob" +
                  ", ADDRESS = @Address" +
                  ", STATE = @State" +
                  ", CITY = @City" +
                  ", POSTCODE = @PostCode where IC_NUMBER =@ICNumber";
    
    
    com.CommandType = CommandType.Text;
    
    com.Parameters.AddWithValue("@Salutation", SALUTATION.Text);
    com.Parameters.AddWithValue("@Name", NAME.Text);
    com.Parameters.AddWithValue("@Sex", SEX.Text);
    com.Parameters.AddWithValue("@Ethnicity", ETHNICITY.Text);
    com.Parameters.AddWithValue("@MaritalStatus", MARITALSTATUS.Text);
    com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);
    com.Parameters.AddWithValue("@HPNumber", HPNUMBER.Text);
    com.Parameters.AddWithValue("@Dob", DOB.Text);
    com.Parameters.AddWithValue("@Address", ADDRESS.Text);
    com.Parameters.AddWithValue("@State", STATE.Text);
    com.Parameters.AddWithValue("@City", CITY.Text);
    com.Parameters.AddWithValue("@PostCode", POSTCODE.Text);
    com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);
    

答案 1 :(得分:1)

使用您创建的参数并删除额外的逗号

   con = new SqlConnection(@"Data Source=dasranrajlui\sqlexpress;Initial Catalog=SESoriginal;Integrated Security=True");
    con.Open();
    com = con.CreateCommand();
    com.CommandType = CommandType.Text;
    com.CommandText = " update VoterRegistration set 
    SALUTATION @Salutation, 
    NAME = @Name, 
    SEX = @Sex, 
    ETHNICITY =@Ethnicity, 
    MARITALSTATUS = @MaritalStatus,
    IC_NUMBER = @ICNumber,  
    HP_NUMBER = @HPNumber,  
    DOB = @Dob, 
    ADDRESS = @Address,
    STATE = @State,
    CITY = @City, 
    POSTCODE = @PostCode 
    where IC_NUMBER = @ICNumber";

    com.CommandType = CommandType.Text;

    com.Parameters.AddWithValue("@Salutation", SALUTATION.Text);
    com.Parameters.AddWithValue("@Name", NAME.Text);
    com.Parameters.AddWithValue("@Sex", SEX.Text);
    com.Parameters.AddWithValue("@Ethnicity", ETHNICITY.Text);
    com.Parameters.AddWithValue("@MaritalStatus", MARITALSTATUS.Text);
    com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);
    com.Parameters.AddWithValue("@HPNumber", HPNUMBER.Text);
    com.Parameters.AddWithValue("@Dob", DOB.Text);
    com.Parameters.AddWithValue("@Address", ADDRESS.Text);
    com.Parameters.AddWithValue("@State", STATE.Text);
    com.Parameters.AddWithValue("@City", CITY.Text);
    com.Parameters.AddWithValue("@PostCode", POSTCODE.Text);
   com.Parameters.AddWithValue("@ICNumber", ICNUMBER.Text);

答案 2 :(得分:0)

我建议您在,之前删除where并在ICNUMBER.text附近添加引用

  "', POSTCODE = '" + POSTCODE.Text + "' where IC_NUMBER = '" + ICNUMBER.Text  +"'";