C# で SQL Server に対して変更処理(INSERT, UPDATE, DELETE)を実行する方法をまとめてみました。
目次
- 1行だけ実行
- トランザクション処理
- [おまけ] DB変更を行うSQL
- INSERT
- UPDATE
- DELETE
1行だけ実行
単一テーブルにしか影響しないようなSQLは1行だけ実行することになると思います。
このようなSQLを実行する場合、トランザクションを考慮せずそのまま ExecuteNonQuery()
を実行する方法が簡単です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | using System; using System.Configuration; using System.Data.SqlClient; public void Insert1( string id, string password, string role) { // 接続文字列の取得 var connectionString = ConfigurationManager.ConnectionStrings[ "sqlsvr" ].ConnectionString; using (var connection = new SqlConnection(connectionString)) using (var command = connection.CreateCommand()) { try { // データベースの接続開始 connection.Open(); // SQLの準備 command.CommandText = @"INSERT INTO T_USER (ID, PASSWORD, ROLE_NAME) VALUES (@ID, @PASSWORD, @ROLE_NAME)" ; command.Parameters.Add( new SqlParameter( "@ID" , id)); command.Parameters.Add( new SqlParameter( "@PASSWORD" , password)); command.Parameters.Add( new SqlParameter( "@ROLE_NAME" , role)); // SQLの実行 command.ExecuteNonQuery(); } catch (Exception exception) { Console.WriteLine(exception.Message); throw ; } finally { // データベースの接続終了 connection.Close(); } } } |
トランザクション処理
複数テーブルに書き込みを行う場合、トランザクションを利用した書き込みになるかと思います。 通常はこちらの方が多い実装になるかと思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | using System; using System.Configuration; using System.Data.SqlClient; public void Insert2( string id, string password, string phone, string address) { // 接続文字列の取得 var connectionString = ConfigurationManager.ConnectionStrings[ "sqlsvr" ].ConnectionString; using (var connection = new SqlConnection(connectionString)) { try { // データベースの接続開始 connection.Open(); using (var transaction = connection.BeginTransaction()) using (var command = new SqlCommand() { Connection = connection, Transaction = transaction }) { try { // 親テーブルを挿入するSQLの準備 command.CommandText = @"INSERT INTO T_USER (ID, PASSWORD) VALUES (@ID, @PASSWORD)" ; command.Parameters.Add( new SqlParameter( "@ID" , id)); command.Parameters.Add( new SqlParameter( "@PASSWORD" , password)); // 親テーブルを挿入するSQLの実行 command.ExecuteNonQuery(); // 子テーブルを挿入するSQLの準備 command.CommandText = @"INSERT INTO T_USER_EXT (ID, PHONE, ADDRESS) VALUES (@ID, @PHONE, @ADDRESS)" ; ////command.Parameters.Add(new SqlParameter("@ID", id)); // <- 上で既に @ID を追加済みのため再投入は不要。 command.Parameters.Add( new SqlParameter( "@PHONE" , phone)); command.Parameters.Add( new SqlParameter( "@ADDRESS" , address)); // 子テーブルを挿入するSQLの実行 command.ExecuteNonQuery(); // コミット transaction.Commit(); } catch { // ロールバック transaction.Rollback(); throw ; } } } catch (Exception exception) { Console.WriteLine(exception.Message); throw ; } finally { // データベースの接続終了 connection.Close(); } } } |
[おまけ] DB変更を行うSQL
INSERT
1 2 3 4 5 | INSERT INTO T_USER ( ID, PASSWORD ) VALUES ( @ID, @ PASSWORD ); |
UPDATE
1 2 3 4 5 6 | UPDATE T_USER SET PASSWORD =@ PASSWORD WHERE ID=@ID ; |
DELETE
1 2 3 | DELETE FROM T_USER WHERE ID=@ID |
関連記事
C# を使って SQL Server に接続、操作する方法に関しては以下のような記事もあります。 あわせて参考に読んでいただけると理解が深まるハズ!
- SQL Server に 接続
- SQL Server に対して SELECT文 を 実行
- SQL Server に対して INSERT文 ・ UPDATE文 ・ DELETE文 を 実行←この記事
- SQL Server に対して パラメタライズドクエリ を 実行
最後に… このブログに興味を持っていただけた方は、 ぜひ 「Facebookページ に いいね!」または 「Twitter の フォロー」 お願いします!!