[VB.Net] PostgreSQL 데이터 조회하기

View Comments

1. 테이블에서 데이터 조회
      Dim intCount As Integer
 
        Try
            If DB_CONNECT() Then
                strCmd.Connection = gOleDBConn
                strCmd.CommandType = CommandType.Text
                strCmd.CommandText = "SELECT * FROM code"
 
                objDA.SelectCommand = strCmd
                objDA.Fill(objDS, "code")
 
                With objDS.Tables("code")
                    For intCount = 0 To .Rows.Count - 1
                        cbRelate.Items.Add(.Rows(intCount).Item("c_code").ToString & " : " & .Rows(intCount).Item("c_value").ToString)
                    Next
                End With
 
                Call DB_DISCONNECT()
            Else
                MessageBox.Show("DB 연결을 확인하십시오!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
            Call DB_DISCONNECT()
        End Try

댓글0 Comments (+add yours?)

트랙백0 Tracbacks (+view to the desc.)

[VB.Net] PostgreSQL OleDB 연결하기

View Comments

1. OleDB 연결을 위한 ConnectString
   - "Provider=PostgreSQL OLE DB Provider;Data Source=DB서버 주소;location=DB명;User ID=아이디;password=비밀번호"
 
2. DB 연결 함수
   Private gOleDBConn As OleDb.OleDbConnection
   Public objDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
   Public objDS As DataSet = New DataSet()
   Public strCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand()
 
   Public Function DB_CONNECT() As Boolean
        Dim strConn As String = "Provider=PostgreSQL OLE DB Provider;Data Source=DB서버 주소;location=DB명;User ID=아이디;password=비밀번호"
 
        Try
            If gOleDBConn Is Nothing Then
                gOleDBConn = New OleDb.OleDbConnection(strConn)
                gOleDBConn.Open()
            Else
                If gOleDBConn.State = ConnectionState.Open Then
                ElseIf gOleDBConn.State = ConnectionState.Closed Then
                    gOleDBConn = New OleDb.OleDbConnection(strConn)
 
                    gOleDBConn.Open()
                End If
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
 
            If gOleDBConn.State = ConnectionState.Closed Then
                DB_CONNECT = False
                MsgBox("데이터베이스 접속 실패!", MsgBoxStyle.Critical, "Error")
            Else
                DB_CONNECT = True
            End If
        End Try
 
        DB_CONNECT = True
    End Function
 
3. DB 연결 해제 함수
   Public Function DB_DISCONNECT() As Boolean
        Try
            If gOleDBConn.State = ConnectionState.Open Then
                objDA.Dispose()
                objDS.Dispose()
                strCmd.Dispose()
                gOleDBConn.Close()
            ElseIf gOleDBConn.State = ConnectionState.Closed Then
                MsgBox("데이터 베이스가 이미 연결 해제 상태입니다!", MsgBoxStyle.Critical, "Error")
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
 
            If gOleDBConn.State = ConnectionState.Open Then
                objDA.Dispose()
                objDS.Dispose()
                strCmd.Dispose()
                gOleDBConn.Close()
            End If
        End Try
 
        DB_DISCONNECT = True
    End Function

댓글0 Comments (+add yours?)

트랙백0 Tracbacks (+view to the desc.)

[VB.Net] PostgreSQL 데이터 입력하기

View Comments

1. 신규 데이터 입력하기
        Dim intCount As Integer
 
        Try
            If DB_CONNECT() Then
                strCmd.Connection = gOleDBConn
                strCmd.CommandType = CommandType.Text
                strCmd.CommandText = "INSERT INTO person VALUES('" & cbRelate.Text.Substring(0, 2) & "', '" & txtName.Text & "', '" & txtBirthday.Text & "', '" & txtHomeAddr.Text & "', '" & txtHomeTel.Text & "', '" & txtComName.Text & "', '" & txtComAddr.Text & "', '" & txtComTel.Text & "', '" & txtCellPhone.Text & "', '" & txtMemo.Text & "')"
 
                objDA.InsertCommand = strCmd
                intCount = objDA.InsertCommand.ExecuteNonQuery()
 
                If intCount = -1 Then
                    MessageBox.Show("데이터 입력 성공", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("데이터 입력 실패!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
 
                Call DB_DISCONNECT()
            Else
                MessageBox.Show("DB 연결을 확인하십시오!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
 
            Call DB_DISCONNECT()
        End Try

댓글0 Comments (+add yours?)

트랙백0 Tracbacks (+view to the desc.)

Newer Entries Older Entries