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?

【C# ASP.NET】PostgreSQL及びSQL Serverを使ったINSERT文の実装方法

Posted at

PostgreSQL及びSQL serverを使ったINSERTクエリについて実装の違いを記載します。

PostgreSQLでの実装例

sample.cs
using Npgsql;

public class TransactionExample
{
    public static void InsertWithTransaction(string connectionString, string tableName, string data)
    {
        using (var connection = new NpgsqlConnection(connectionString))
        {
            connection.Open();
            using (var transaction = connection.BeginTransaction())
            {
                try
                {
                    using (var command = new NpgsqlCommand())
                    {
                        command.Connection = connection;
                        command.Transaction = transaction;
                        command.CommandText = $"INSERT INTO {tableName} (column_name) VALUES (@data)"; // テーブル名とカラム名を適切に設定
                        command.Parameters.AddWithValue("@data", data);
                        command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                    Console.WriteLine("トランザクションがコミットされました。");
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Console.WriteLine($"エラーが発生しました: {ex.Message}");
                    Console.WriteLine("トランザクションがロールバックされました。");
                }
            }
        }
    }

    public static void Main(string[] args)
    {
        // PostgreSQLへの接続文字列
        string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;";
        string tableName = "my_table"; // 実際のテーブル名に置き換えてください
        string data = "test data"; // 実際のデータに置き換えてください

        InsertWithTransaction(connectionString, tableName, data);
    }
}

つづいてSQL Serverでの実装例

sample.cs
using System;
using System.Data;
using System.Data.SqlClient;

public class TransactionExample
{
    public static void Main(string[] args)
    {
        string connectionString = "あなたの接続文字列";
        string insertQuery = "INSERT INTO Products (ProductName, Category, Price) VALUES (@ProductName, @Category, @Price)";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            SqlTransaction transaction = connection.BeginTransaction();
            SqlCommand command = connection.CreateCommand();
            command.Transaction = transaction;

            try
            {
                // 最初の挿入
                command.CommandText = insertQuery;
                command.Parameters.Clear();
                command.Parameters.Add("@ProductName", SqlDbType.NVarChar, 50).Value = "りんご";
                command.Parameters.Add("@Category", SqlDbType.NVarChar, 50).Value = "果物";
                command.Parameters.Add("@Price", SqlDbType.Money).Value = 200;
                command.ExecuteNonQuery();

                // 2回目の挿入
                command.CommandText = insertQuery;
                command.Parameters.Clear();
                command.Parameters.Add("@ProductName", SqlDbType.NVarChar, 50).Value = "バナナ";
                command.Parameters.Add("@Category", SqlDbType.NVarChar, 50).Value = "果物";
                command.Parameters.Add("@Price", SqlDbType.Money).Value = 150;
                command.ExecuteNonQuery();

                transaction.Commit();
                Console.WriteLine("トランザクションをコミットしました。");
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Console.WriteLine("トランザクションをロールバックしました。エラー: " + ex.Message);
            }
        }
    }
}
0
0
0

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?