语法错误 - INSERT INTO语句

时间:2016-12-16 12:57:02

标签: c# database

我正在制作一个表单来为我的登录应用程序添加帐户 如果你可以提供帮助那就太棒了。

我只有11岁,所以这可能是一个愚蠢的问题!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Login_Viper_Safe
{
    public partial class Form3 : Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public Form3()
        {
            InitializeComponent();
            connection.ConnectionString =   @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\SillyTen9\Documents\UserDatabase.accdb; Persist Security Info=False;";
    }

    private void Form3_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "INSERT INTO UserDatabase ([Username], [Password], FirstName, LastName) VALUSES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            command.ExecuteNonQuery();
            MessageBox.Show("Signed Up!");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你的值有误(额外的s):

 "INSERT INTO UserDatabase ([Username], [Password], FirstName, LastName) VALUES( ...

使用参数也很重要:

command.CommandText = "INSERT INTO UserDatabase ([Username], [Password], FirstName, LastName) VALUES (@param1,@param2,@param3,@param4)";
command.Parameters.AddWithValue("@param1",textBox1.Text);
command.Parameters.AddWithValue("@param2",textBox2.Text);
command.Parameters.AddWithValue("@param3",textBox3.Text);
command.Parameters.AddWithValue("@param4",textBox4.Text);
...

答案 1 :(得分:0)

您在输入查询

中将拼写错误设为值而不是VALUSES
command.CommandText = "INSERT INTO UserDatabase ([Username], [Password], FirstName, LastName) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";