วันศุกร์ที่ 17 กรกฎาคม พ.ศ. 2558

WINDOWS MOBILE CE Connect SQL SERVER 2008

Imports System.Data
Imports System
Imports System.Data.SqlClient
Imports System.Windows.Forms.DateTimePicker
Imports System.Windows.Forms


'####### FOR MOBILE #########
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As New DataSet()
        dbCon.Open()

        Dim sql As String
        sql = "SELECT * FROM LOGIN"
        Dim command As New SqlCommand(sql, dbCon)
        dbAdapter.SelectCommand = command
        dbAdapter.Fill(ds)
        dbAdapter.Dispose()
        command.Dispose()

        DataGrid2.DataSource = ds.Tables(0)     '' ใส่ค่าใน GridView1  
        dbCon.Close()

        Console.WriteLine("State: {0}", dbCon.State)
        Console.WriteLine("ConnectionString: {0}", _
        dbCon.ConnectionString)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sql As String
        Dim rs As SqlDataReader                 'การอ่านค่าจาก DataBase
        sql = "SELECT * FROM LOGIN"
        rs = ExcuteReader(sql)
        While rs.Read
            MsgBox("Get data" & rs.Item("id"))
        End While
        rs.Close()
    End Sub
End Class


### Module1

Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlCommand
Imports System.Data.SqlClient.SqlConnection
Imports System.Data.SqlClient.SqlDataAdapter
Imports System.Data.SqlClient.SqlDataReader
Imports System.Data
Imports System

Module Module1
    Public sStrng As String = "Data Source=192.168.101.3\SQLExpress;Initial Catalog=TEST;" _
        & "User ID=sa;" _
        & "Password=seiwa@1234;" _
        & "Persist Security Info=True;"


    Public scmd As SqlCommand
    Public dbCon As New SqlConnection(sStrng)                          'MySqlConnection(sstring)   
    Public dbCmd As New SqlCommand
    Public dbAdapter As New SqlDataAdapter

    Sub OpenDB()
        If dbCon.State = ConnectionState.Closed Then   ' ถ้า Cloas ให้เปิด
            Try
                dbCon.Open()
                'MsgBox("สามารถติดต่อฐานข้อมูล")
            Catch ex As Exception
                MsgBox("ไม่สามารถติดต่อฐานข้อมูล")
            End Try
        End If
    End Sub

    ''' <summary>
    ''' ####### ins_upd_del #######
    ''' </summary>
    ''' <returns></returns>
    ''' <remadsd></remadsd>
    Public Function ExcSQL(ByVal ins_upd_del As String) As Boolean
        Try
            OpenDB()
            scmd = New SqlCommand(ins_upd_del, dbCon)
            scmd.ExecuteNonQuery()
            scmd.Dispose()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    ''' <summary>
    ''' #######  ExcuteReader   #######
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ExcuteReader(ByVal sTr As String) As SqlDataReader
        If Not dbCon.State = ConnectionState.Open Then
            dbCon.Open()
        End If
        dbCmd = New SqlCommand(sTr, dbCon)
        ExcuteReader = dbCmd.ExecuteReader

    End Function
End Module



ไม่มีความคิดเห็น:

แสดงความคิดเห็น