1

Access DB のテーブルを 2 行チェックし、ユーザーが textBox を介して ComplexityFactor 列を変更できるようにするプログラムがあります。アプリケーションのロード時に行が存在しない場合は、2 つの行が作成されます (OHorUG フィールドを除いて、すべてのフィールドは同じです)。選択した行に基づいて、ユーザーは選択した行の ComplexityFactor フィールドを変更できます。行を正常に作成して読み取ることができましたが、1 フィールドを更新できません。

更新コードは次のとおりです。

private void button1_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count != 0)
        {
            ListViewItem selected = listView1.SelectedItems[0];
            OleDbCommand updateCmd = new OleDbCommand();
            myConnection.Open();

            updateCmd.CommandType = CommandType.Text;
            updateCmd.CommandText = "UPDATE Contractors SET ComplexityFactor = @ComplexityFactor" +
               " WHERE Division = @Division AND ComplexityFactorCode = @ComplexityFactorCode AND ContractNumber = @ContractNumber AND ContractorCode = @ContractorCode AND ContractorName = @ContractorName AND OHorUG = @OHorUG)";

            updateCmd.Parameters.AddWithValue("@ComplexityFactor", textBox1.Text.ToString());//FYI this field is a Number in Access
            updateCmd.Parameters.AddWithValue("@Division", "SV");
            updateCmd.Parameters.AddWithValue("@ComplexityFactorCode", "X01");
            updateCmd.Parameters.AddWithValue("@ContractNumber", "0");
            updateCmd.Parameters.AddWithValue("@ContractorCode", "SSI");
            updateCmd.Parameters.AddWithValue("@ContractorName", "SunStream Inc");
            updateCmd.Parameters.AddWithValue("@OHorUG", selected.SubItems[6].Text.ToString());
            updateCmd.Connection = myConnection;
            updateCmd.ExecuteNonQuery();
            myConnection.Close();
        }
        else
        {
            MessageBox.Show("Please select a row to update...");


        }

例外を追加するのを忘れました:

System.Data.OleDb.OleDbException was unhandled
  Message=Extra ) in query expression 'Division = @Division AND ComplexityFactorCode = @ComplexityFactorCode AND ContractNumber = @ContractNumber AND ContractorCode = @ContractorCode AND ContractorName = @ContractorName AND OHorUG = @OHorUG)'.
  Source=Microsoft JET Database Engine
  ErrorCode=-2147217900
  StackTrace:
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\s153720\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 199
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication1.Program.Main() in c:\Users\s153720\AppData\Local\Temporary Projects\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:     
4

2 に答える 2

1

)updateステートメントの最後にある閉じ括弧を削除してみてください

updateCmd.CommandText = "UPDATE Contractors SET ComplexityFactor = @ComplexityFactor" +
" WHERE Division = @Division AND ComplexityFactorCode = @ComplexityFactorCode AND ContractNumber = @ContractNumber AND ContractorCode = @ContractorCode AND ContractorName = @ContractorName AND OHorUG = @OHorUG";
于 2012-09-12T20:20:09.517 に答える
0

あなたは試してみたいかもしれません:

  1. 文字列を数値 cint または cdec に変換します
  2. SQL インジェクション攻撃を防ぐ限り、番号を SQL に直接渡します。
于 2012-09-12T20:20:23.037 に答える