winforms 无法确定我的insert into语句有什么问题,该语句用于将数据从windows窗体文本框插入到ms access数据库的行中

hfsqlsce  于 2022-11-30  发布在  Windows
关注(0)|答案(1)|浏览(112)
public partial class Form1 : Form
{
    static OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=chessclub.accdb");

    static OleDbCommand cmd = con.CreateCommand();
    static OleDbDataReader reader;
    int count = 0;
    
    public Form1()
    {
        InitializeComponent();
    }

    private void btncreate_Click(object sender, EventArgs e)
    {
        if (con.State.Equals(System.Data.ConnectionState.Open))
            con.Close();

        con.Open();

        OleDbCommand cmd = con.CreateCommand();
        cmd.CommandText = "INSERT INTO chess1db( names , schoolid , major , gender) + VALUES ( '" + txtname.Text + "','" + txtid.Text + "','" + txtmajor.Text + "','" + txtgndr.Text + "')";
        cmd.Connection = con;

        cmd.ExecuteNonQuery();

        MessageBox.Show("Record submitted ");
        con.Close();
    }
}

每次我按下发送数据到数据库的按钮时,它都告诉我INSERT INTO语句中有语法错误。这是什么问题?

64jmpszr

64jmpszr1#

这是个加号,拿掉它

cmd.CommandText = "INSERT INTO chess1db( names , schoolid , major , gender)  VALUES ('" + txtname.Text + "','" + txtid.Text + "','" + txtmajor.Text + "','" + txtgndr.Text + "')";

但请务必使用 * 参数 *。

相关问题