1
1

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 Insertクエリ

Last updated at Posted at 2023-08-21

項目数が多いときダラダラSQL構文を記述する必要があるので
それを簡潔にするためのものです。
WPF、WinForms .NET FRAMEWORK

あくまでサンプルです。例外処理などはご自身でお願いします。


using System;
using System.Collections.Generic;
using System.Windows;
using System.Data.OleDb;

namespace WPF_INSERT {

    public partial class MainWindow : Window {

        public MainWindow() {
            InitializeComponent();

            String ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB\TEST.ACCDB";
            OleDbConnection conn;
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            
            InsertSQL(connection);
        }

        private void InsertSQL(OleDbConnection connection){

            // new List<string>() に変えたり。
            string[] ax = new string[100];
            int c = 0;

            // DBの項目順番通りに記述
            ax[c++] = "A1";
            ax[c++] = "A2";
            ax[c++] = "A3";
                 
            string re = "";
            string rx = "";
      
            for (int v = 0; v < c; v++) {
                re = re + ax[v] + ",";
                rx = rx + "@" + ax[v] + ",";
            }

            // 最後の余計な , 取る
            string s1 = re.Substring(0, re.Length - 1); 
            string s2 = rx.Substring(0, rx.Length - 1);

            string sqlstr = "INSERT INTO T_1 (" + s1 + ") VALUES (" + s2 + ")";
          
            try {
                OleDbCommand command = new OleDbCommand(sqlstr, connection);
                List<OleDbParameter> sqlParameters = new List<OleDbParameter>();

                // ここも順番通りに記述
                sqlParameters.AddSqlParameter(new OleDbParameter("@" + ax[c++], OleDbType.VarChar)).Value = "A1";
                sqlParameters.AddSqlParameter(new OleDbParameter("@" + ax[c++], OleDbType.VarChar)).Value = "A2";
                sqlParameters.AddSqlParameter(new OleDbParameter("@" + ax[c++], OleDbType.VarChar)).Value = "A3";

                command.Parameters.AddParams(sqlParameters);
                command.ExecuteNonQuery();
                connection.Close();
                
            }
            catch (Exception e) {
                MessageBox.Show ("エラー" + e.ToString());    
            }
        }
    }

    // http://www.curict.com/item/cd/cdae4c2.html 参考URL
    public static class SqlParameterExtension {
        public static OleDbParameter AddSqlParameter(this List<OleDbParameter> list, OleDbParameter parameter) {
            list.Add(parameter);
            return parameter;
        }

        public static void AddParams(this OleDbParameterCollection collection, List<OleDbParameter> list) {
            foreach (OleDbParameter parameter in list) {
                collection.Add(parameter);
            }
        }
    }
}

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?