[VB.NET]Oracleでテーブルにデータを削除(delete)する

2021年10月4日

書式
DELETE FROM テーブル名 WHERE カラム名 = :変数名
OracleCommand型インスタンス名.Parameters.Add(New OracleParameter(カラム名, OracleDbType.データ型)).Value = xxx

OracleCommand
基本コンストラクターは、すべてのフィールドを既定値に初期化します。 次の表に、のインスタンスのプロパティの初期値を示し OracleCommand ます。
注釈
プロパティ 初期値
CommandText 空の文字列 (“")
CommandType Text
Connection null

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Imports Oracle.ManagedDataAccess.Client
Module Module1
Sub Main()
'SQL構文(delete操作) バインド変数 :変数名
Dim strSql As String = "DELETE FROM USERTBL WHERE UID = :UID"
'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(strSql)
'Connectionプロパティにconnを指定
cmd.Connection = Conn
cmd.CommandType = CommandType.Text
'バインド名
cmd.BindByName = True
'Parametersプロパティに値UIDを設定
cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 456
'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構文(delete操作) バインド変数 :変数名 Dim strSql As String = "DELETE FROM USERTBL WHERE UID = :UID" '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(strSql) 'Connectionプロパティにconnを指定 cmd.Connection = Conn cmd.CommandType = CommandType.Text 'バインド名 cmd.BindByName = True 'Parametersプロパティに値UIDを設定 cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 456 '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構文(delete操作) バインド変数 :変数名
    Dim strSql As String = "DELETE FROM USERTBL WHERE UID = :UID"
    
      '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(strSql)
                    'Connectionプロパティにconnを指定
              cmd.Connection = Conn
              cmd.CommandType = CommandType.Text
                    'バインド名
              cmd.BindByName = True
                    
                    'Parametersプロパティに値UIDを設定
              cmd.Parameters.Add(New OracleParameter("UID", OracleDbType.Int32)).Value = 456
                    
                    '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

 

Software

Posted by arkgame