본문으로 바로가기

(vb.net) MS-SQL 연결

category vb.net 2014. 10. 30. 14:47

Imports System.Data.SqlClient
Imports System.Data
Public Class Form1
    Dim conn As SqlConnection

    Dim str As String = String.Format("Data Source=서버이름; Initial Catalog=DB이름;Integrated Security=False; uid=아이디; pwd=패스워드")

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim csds As New DataSet
        Dim csda As New SqlDataAdapter
        Dim itmCur As ListViewItem
        Try
            conn = New SqlConnection(str)
            conn.Open()
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try
        
        Dim strsql As String = "select * from Test"
        csda.SelectCommand = New SqlCommand(strsql, conn)
        csda.Fill(csds)

        For Each ds As DataRow In csds.Tables(0).Rows
            itmCur = New ListViewItem
            itmCur.Text = ds.Item("id").ToString()
            itmCur.SubItems.Add(ds.Item("content").ToString())
            ListView1.Items.Add(itmCur)
        Next

    End Sub
End Class