我在c#访问数据中更新查询时遇到问题,请帮我说数据类型不匹配 [英] i have a problem while update query in c# access data , please help me it says data type mismatch

查看:66
本文介绍了我在c#访问数据中更新查询时遇到问题,请帮我说数据类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 " public static int updatestrecord(String St_name,String St_fname,String Address,int Contact_no,int Student_id)

        {

            String query =" update St_record set St_name ='" + St_name +"',St_fname ='" + St_fname +"',Address ='" + Address +"',Contact_no ='" + Contact_no +"'where
Student_id ='" ; + Student_id +"'";

            OleDbCommand com = new OleDbCommand(query,conn);

            int rows = com.ExecuteNonQuery();

           返回行;

        }"这是更新按钮的数据库类代码"。

 "public static int updatestrecord(String St_name, String St_fname, String Address, int Contact_no, int Student_id)
        {
            String query= "update St_record set St_name='" + St_name+ "',St_fname='"+St_fname+"',Address='"+Address+"',Contact_no='"+Contact_no+"' where Student_id='"+Student_id+"'";
            OleDbCommand com= new OleDbCommand(query,conn);
            int rows = com.ExecuteNonQuery();
            return rows;
        }"this is a database class code for update button ".

这是一个更新的按钮代码"

and thats a button code of update "

推荐答案

您好,

我建议使用参数而不是参数的字符串连接。的? mark表示参数,并在添加参数时注意它们是Access的序号位置。

I would recommend using parameters rather than string concatenation for parameters. The ? mark signifies a parameter and note when adding parameters they are ordinal position for Access.

public static int updatestrecord(string St_name, string St_fname, string Address, int Contact_no, int Student_id)
{
    var query = "UPDATE St_record " +
                "SET St_name=?,St_fname=?,Address=?,Contact_no=? " +
                "WHERE Student_id=?";

    var com = new OleDbCommand(query, conn);

    com.Parameters.AddWithValue("?", St_name);
    com.Parameters.AddWithValue("?", St_fname);
    com.Parameters.AddWithValue("?", Address);
    com.Parameters.AddWithValue("?", Contact_no);
    com.Parameters.AddWithValue("?", Student_id);


    return com.ExecuteNonQuery();
}
private void update_Click(object sender, EventArgs e)
{
    int Student_id = int.Parse(textBox1.Text);
    string St_name = textBox2.Text;
    string St_fname = textBox3.Text;
    string Address = textBox4.Text;
    int Contact_no = int.Parse(textBox5.Text);

    int row = Databaseconn.updatestrecord(St_name, St_fname, Address, Contact_no, Student_id);
    MessageBox.Show(row > 0 ? "record updated" : "error");
}
}


这篇关于我在c#访问数据中更新查询时遇到问题,请帮我说数据类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆