2

ログインして新しいアカウントを登録できるac#アプリケーションを作成します。しかし、新しいアカウントのボタンをクリックし、必須フィールドに入力すると、次のエラーが発生します。

vcom.ExecuteNonQuery(); ->OleDBExceptionは処理されませんでした。INSERT命令にsyntaxerrorが含まれています。

以下のコードを参照してください。


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

namespace Eindopdracht
{
    public partial class maak_account : Form
    {

        OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb");

        public maak_account()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb");
            vcon.Open();

            string test = string.Format("insert into inlog (PASSWORD, Username, leeftijd, gewicht) VALUES ('" + textBox2.Text + "','" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
            OleDbCommand vcom = new OleDbCommand(test, vcon);
            vcom.ExecuteNonQuery();
            MessageBox.Show("Uw gegevens zijn opgeslagen");
            vcom.Dispose();
        }
    }
}
4

2 に答える 2

2

構文エラーの理由は、Passwordたまたま列の名前である、が予約済みのキーワードであるためです。

insert into inlog ([PASSWORD], Username, leeftijd, gewicht)

MSAccessDocsから

予約語がすでに使用されている場合は、単語の各出現箇所を括弧([])で囲むことにより、エラーメッセージを回避できます。ただし、最善の解決策は、名前を予約されていない単語に変更することです。

コードをさらに改善するには、

  • ステートメントを使用usingしてオブジェクトを適切に破棄します
  • try-catch例外を適切に処理するために使用する
  • SQLインジェクションを回避するために値をパラメーター化

例、

string connStr = @"provider= microsoft.jet.oledb.4.0;data source=sample.mdb";
string test = "insert into inlog ([PASSWORD], Username, leeftijd, gewicht) VALUES (?, ?, ?, ?)";

using(OleDbConnection vcon = new OleDbConnection(connStr))
{
    using(OleDbCommand vcom = new OleDbCommand(test, vcon))
    {
        vcom.Parameters.AddWithValue("PASSWORD", textBox2.Text);
        vcom.Parameters.AddWithValue("Username", textBox1.Text);
        vcom.Parameters.AddWithValue("leeftijd", textBox3.Text);
        vcom.Parameters.AddWithValue("gewicht", textBox4.Text);
        try
        {
            vcon.Open();
            com.ExecuteNonQuery();
        }
        catch(OleDbException ex)
        {
            // do something with the exception
        }
    }
}
于 2013-03-27T10:47:12.463 に答える
0

try / catchブロックを使用して例外をキャッチし、エラーメッセージを確認します。また、finallyブロックでリソースを破棄します。例外がスローされた場合でも、finallyブロックは常に実行されます。

try
{
    OleDbConnection vcon = new OleDbConnection(@"provider= microsoft.jet.oledb.4.0;data source=sample.mdb");
    vcon.Open();

    string test = string.Format("insert into inlog (PASSWORD, Username, leeftijd, gewicht) VALUES ('" + textBox2.Text + "','" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
    OleDbCommand vcom = new OleDbCommand(test, vcon);
    vcom.ExecuteNonQuery();
    MessageBox.Show("Uw gegevens zijn opgeslagen");
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    vcom.Dispose();
    vcon.Close();
    vcon.Dispose();
}
于 2013-03-27T10:50:30.537 に答える