繁体   English   中英

无法弄清楚我的 insert into 语句将 windows forms 文本框的数据插入 ms access 数据库中的一行有什么问题

[英]Can't figure out what's wrong with my insert into statement to insert data from a windows forms textbox to a row in ms access database

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语句中存在语法错误。 它出什么问题了?

这是加号。 去掉它:

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

但是不要使用参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM