0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQL Updateクエリ

Last updated at Posted at 2023-08-17

サンプルです。
例外や改修はご自分でお願いします。

using System;
using System.Data.OleDb;
using System.Windows;

namespace WPF_ACCESS {

    public partial class MainWindow : Window {

        String constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB\SYSTEM.accdb";
        public OleDbConnection conn;
        
        public MainWindow() {
            InitializeComponent();

            string[] ax = new string[100];
            int c = 0;

            //////////////////////////////////////////////////////////////////////////////////////////////////
            // DBの項目順番通りに記載すること
            ax[c++] = "CODE";
            ax[c++] = "NAME1";
            ax[c++] = "NAME2";
            
            c--;

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // UPDATEクエリ作成
            ////////////////////////////////////////////////////////////////////////////////////////////////
            string sql = "UPDATE T_MAIN SET ";
            for (int v = 0; v <= c; v++) sql += ax[v] + "=@" + ax[v] + ",";

            string vr = sql.Substring(0, sql.Length - 1); // 最後の , 取る
            vr += " WHERE ID=5755";

            //MessageBox.Show(vr);

            using( var conn = new OleDbConnection {
                ConnectionString = constr,
            })
            using( var comm = new OleDbCommand {
                CommandText = vr,
                Connection = conn,
            })
            {
                conn.Open();

                try {
                    c = 0;
                    comm.Parameters.AddWithValue("@" + ax[c++], "789");
                    comm.Parameters.AddWithValue("@" + ax[c++], "456");
                    comm.Parameters.AddWithValue("@" + ax[c++], "789");

                    comm.ExecuteNonQuery();
                    conn.Close();
                }
                catch {
                    MessageBox.Show ("T_MAIN UPDATE ERR");
                }
            }

        }
        

    }


}
0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?