2016-12-16 17 views
1

ログインアプリケーションにアカウントを追加するためのフォームを作成しています。 あなたが助けてくれれば嬉しいです。構文エラー - INSERT INTOステートメント

私はわずか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); 
     } 
    } 
} 
+3

「VALUSES」は、「VALUES」である必要があります – Mairaj

+0

サイドノート:1.値にパラメータを使用します。 2.クラススコープの接続を使用しないで、必要なときに作成して閉じます。 3. 'OleDbCommand'のような' IDisposable'を実装している他の型だけでなく、それらを作成するときに接続を 'using'ブロックで囲みます。 4.接続文字列を 'app.config'に入れ、必要に応じてそこから取得します。 – Igor

+1

SQLを避けようとしています。現在、SQLインジェクションやその他の不快な事態を避けるために、パラメータ化されたクエリを使用してください。 http://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements – Steve

答えて

1

"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); 
... 
+0

thx !!編集しました... – apomene

+0

[パラメータを使用する理由](http://www.xkcd.com/327/) – stuartd

0

あなたは誤植エラー挿入クエリでその値がないVALUSESを作ります

command.CommandText = "INSERT INTO UserDatabase ([Username], [Password], FirstName, LastName) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"; 
関連する問題