[VB.NET]Oracleでテーブルにデータを挿入(insert)する

2021年10月4日

書式
INSERT INTO テーブル名(カラム1,カラム2) VALUES (:バインド変数1,:バインド変数2)
OracleCommand型インスタンス名.Parameters.Add(New OracleParameter(カラム名, OracleDbType.データ型)).Value = xxx

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Imports Oracle.ManagedDataAccess.Client
Module Module1
Sub Main()
'SQL構文(insert操作) バインド変数 :変数名
Dim Sql As String = "INSERT INTO USERTBL(UID,DEPNAME) VALUES (:UID,:DEPNAME)"
'Using ステートメント OracleConnectionのインスタンスを生成
Using Conn As OracleConnection = New OracleConnection()
'データソース、ユーザー名、パスワードを指定
Conn.ConnectionString ="User Id=xxx;Password=xxx;Data Source=xxx;"
Try
'Oracleに接続
Conn.Open()
'トランザクションを開始
Using transaction As OracleTransaction = Conn.BeginTransaction()
Try
'OracleCommandのインスタンスのプロパティを指定
Using cmd As OracleCommand = New OracleCommand(Sql)
'Connectionプロパティにconnを指定
cmd.Connection = Conn
cmd.CommandType = CommandType.Text
'バインド名
cmd.BindByName = True
'Parametersプロパティに値UIDを設定
cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 45
'Parametersプロパティに値DEPNAMEを設定
cmd.Parameters.Add(New OracleParameter("DEPNAME", OracleDbType.Varchar2)).Value = "企画部"
'SQLの実行
cmd.ExecuteNonQuery()
'コミット
transaction.Commit()
End Using
Catch ex As Exception
'ロールバック
transaction.Rollback()
'エラーメッセージを出力
Console.WriteLine(ex.Message)
End Try
End Using
Catch ex As Exception
'例外メッセージを出力
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
End Module
Imports Oracle.ManagedDataAccess.Client Module Module1 Sub Main() 'SQL構文(insert操作) バインド変数 :変数名 Dim Sql As String = "INSERT INTO USERTBL(UID,DEPNAME) VALUES (:UID,:DEPNAME)" 'Using ステートメント OracleConnectionのインスタンスを生成 Using Conn As OracleConnection = New OracleConnection() 'データソース、ユーザー名、パスワードを指定 Conn.ConnectionString ="User Id=xxx;Password=xxx;Data Source=xxx;" Try 'Oracleに接続 Conn.Open() 'トランザクションを開始 Using transaction As OracleTransaction = Conn.BeginTransaction() Try 'OracleCommandのインスタンスのプロパティを指定 Using cmd As OracleCommand = New OracleCommand(Sql) 'Connectionプロパティにconnを指定 cmd.Connection = Conn cmd.CommandType = CommandType.Text 'バインド名 cmd.BindByName = True 'Parametersプロパティに値UIDを設定 cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 45 'Parametersプロパティに値DEPNAMEを設定 cmd.Parameters.Add(New OracleParameter("DEPNAME", OracleDbType.Varchar2)).Value = "企画部" 'SQLの実行 cmd.ExecuteNonQuery() 'コミット transaction.Commit() End Using Catch ex As Exception 'ロールバック transaction.Rollback() 'エラーメッセージを出力 Console.WriteLine(ex.Message) End Try End Using Catch ex As Exception '例外メッセージを出力 Console.WriteLine(ex.Message) End Try End Using End Sub End Module
Imports Oracle.ManagedDataAccess.Client

Module Module1
  Sub Main()
    'SQL構文(insert操作) バインド変数 :変数名
    Dim Sql As String = "INSERT INTO USERTBL(UID,DEPNAME) VALUES (:UID,:DEPNAME)"
    
     'Using ステートメント OracleConnectionのインスタンスを生成
    Using Conn As OracleConnection = New OracleConnection()
      'データソース、ユーザー名、パスワードを指定
      Conn.ConnectionString ="User Id=xxx;Password=xxx;Data Source=xxx;"
      Try
        'Oracleに接続
        Conn.Open()
            
        'トランザクションを開始
        Using transaction As OracleTransaction = Conn.BeginTransaction()

          Try
             'OracleCommandのインスタンスのプロパティを指定
            Using cmd As OracleCommand = New OracleCommand(Sql)
               'Connectionプロパティにconnを指定
              cmd.Connection = Conn
              cmd.CommandType = CommandType.Text
               'バインド名
              cmd.BindByName = True
                    
              'Parametersプロパティに値UIDを設定
              cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 45
              'Parametersプロパティに値DEPNAMEを設定
              cmd.Parameters.Add(New OracleParameter("DEPNAME", OracleDbType.Varchar2)).Value = "企画部"
              
              'SQLの実行
              cmd.ExecuteNonQuery()
              'コミット
              transaction.Commit()
                    
            End Using
          Catch ex As Exception
            'ロールバック
            transaction.Rollback()
            'エラーメッセージを出力
            Console.WriteLine(ex.Message)
          End Try
        End Using
      Catch ex As Exception
        '例外メッセージを出力
        Console.WriteLine(ex.Message)
      End Try
    End Using
  End Sub
End Module

 

VB.net

Posted by arkgame