Sunday, March 29, 2015

cara instal aplikasi dengan database sql server

Sebagian besar orag mengalami kesulitan saat menginstal aplikasi di komputer "korban", pada saat aplikasi dibuat di komputer asal semua "fine-fine" saja, tetapi kendala muncul saat aplikasi harus "ditanam" di komputer lain, kendala tersebut antara lain :
1. di kompi korban tidak ada database dan tabel
2. di kompi korban tidak ada DSN
3. di kompi korban tidak ada tools reporting (crystal report)

untuk mengatasi hal tersebut dapat kita lakukan dengan cara :
1. membuat database dengan coding yang ditanam dalam aplikasi
2. membuat tabel dengan coding yang ditanam dalam aplikasi
3. membuat dsn dengan coding yang ditanam dalam aplikasi
4. menginstal tool reporting dengan coding yang ditanam dalam aplikasi

 OK..., tahapannya kita ikuti sebagai berikut
1. Buatlah form dengan bentuk seperti gambar berikut ini :


2. membuat coding sebagai berikut :

Imports System.Data.SqlClient

Public Class Konfigurasi1

    Dim CONN As SqlConnection
    Dim DA As SqlDataAdapter
    Dim DS As New DataSet
    Dim CMD As SqlCommand
    Dim DR As SqlDataReader

    'deklarasi untuk DSN / ODBC
    Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Integer, ByVal ByValfRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer
    Private Const vbAPINull As Integer = 0 ' NULL Pointer
    Private Const ODBC_ADD_DSN As Short = 1 ' Add data source

    Public Sub CreateUserDSNwindows()
        Try
            Dim intRet As Integer
            Dim Driver As String
            Dim Attributes As String
            Driver = "SQL Server"

            Attributes = "SERVER=" & ComboBox1.Text & Chr(0)
            Attributes = Attributes & "DATABASE=" & ComboBox3.Text & Chr(0)
            Attributes = Attributes & "DSN=" & ComboBox3.Text & Chr(0)
            'Attributes = Attributes & "LOGIN ID=" & TextBox1.Text & Chr(0)
            'Attributes = Attributes & "Pwd=" & TextBox2.Text & Chr(0)

            intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, Driver, Attributes)
            If intRet <> 0 Then
                MsgBox("DSN telah dibuat")
            Else
                MsgBox("Pembuatan DSN gagal")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub CreateUserDSNSql()
        Try
            Dim intRet As Integer
            Dim Driver As String
            Dim Attributes As String
            Driver = "SQL Server"

            Attributes = "SERVER=" & ComboBox1.Text & Chr(0)
            Attributes = Attributes & "DATABASE=" & ComboBox3.Text & Chr(0)
            Attributes = Attributes & "DSN=" & ComboBox3.Text & Chr(0)
            'Attributes = Attributes & "LOGIN ID=" & TextBox1.Text & Chr(0)
            'Attributes = Attributes & "TRUSTED_CONNECTION=NO" & Chr(0)
            Attributes = Attributes & "UID=" & TextBox1.Text & Chr(0)
            Attributes = Attributes & "Pwd=" & TextBox2.Text & Chr(0)

            intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, Driver, Attributes)
            If intRet <> 0 Then
                MsgBox("DSN telah dibuat")
            Else
                MsgBox("Pembuatan DSN gagal")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub cariServer(ByVal STR As String)
        CONN = New SqlConnection("data source=" & STR & ";database=master;integrated security=SSPI;")
        CONN.Open()
        CMD = New SqlCommand("select * from sysservers where srvproduct='sql server'", CONN)
        DR = CMD.ExecuteReader
        Do While DR.Read
            ComboBox1.Items.Add(DR.Item(2))
        Loop
    End Sub

    '1 baca server
    Private Sub Konfigurasi1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ComboBox1.Text = System.Net.Dns.GetHostName
        On Error Resume Next
        'cariServer(".")
        cariServer(".\sqlexpress")
    End Sub

    Sub CaraLogin()
        If ComboBox2.Text = "Windows Authentication" Then
            TextBox1.Enabled = False : TextBox1.Clear()
            TextBox2.Enabled = False : TextBox2.Clear()
        Else
            TextBox1.Enabled = True
            TextBox2.Enabled = True
        End If
    End Sub

    '2 pilih cara login

    Sub KoneksiMaster()
        CONN = New SqlConnection("Data Source=" & ComboBox1.Text & ";Database=Master;integrated security=SSPI;")
        CONN.Open()
        CONN.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        ComboBox2.Items.Add("Windows Authentication")
        ComboBox2.Items.Add("SQL Server Authentication")
        TextBox1.Clear()
        TextBox2.Clear()
        ComboBox3.Text = ""
        ComboBox3.Items.Clear()
        Call CariDatabase()
        Call KoneksiMaster()
        Call CaraLogin()
    End Sub

    Sub CariDatabase()
        CONN = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
        CONN.Open()
        ComboBox3.Items.Clear()
        CMD = New SqlCommand("select * from sysdatabases", CONN)
        DR = CMD.ExecuteReader
        While DR.Read
            ComboBox3.Items.Add(DR(0))
        End While
        DR.Close()
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        Call CaraLogin()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            Call CariDatabase()
        ElseIf RadioButton2.Checked = True Then
            ComboBox3.Items.Clear()
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        ComboBox3.Items.Clear()
        ComboBox3.Text = ""
        ComboBox3.Focus()
    End Sub

    Sub Koneksiwindows()
        CONN = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";initial catalog=" & ComboBox3.Text & ";integrated security=true;")
        CONN.Open()
    End Sub

    Sub KoneksiServer()
        CONN = New SqlConnection("Data Source=" & ComboBox1.Text & ";initial catalog=" & ComboBox3.Text & ";User Id=" & TextBox1.Text & ";password=" & TextBox2.Text & "")
        CONN.Open()
    End Sub

    Sub MembuatDatabase()
        Dim str As String
        str = "CREATE DATABASE " & ComboBox3.Text & " ON PRIMARY " & _
          "(NAME = " & ComboBox3.Text & "_Data, " & _
          " FILENAME = 'D:\" & ComboBox3.Text & "Data.mdf', " & _
          " SIZE = 3MB, " & _
          " MAXSIZE = 3MB, " & _
          " FILEGROWTH = 10%) " & _
          " LOG ON " & _
          "(NAME = " & ComboBox3.Text & "_Log, " & _
          " FILENAME = 'D:\" & ComboBox3.Text & "Log.ldf', " & _
          " SIZE = 3MB, " & _
          " MAXSIZE = 3MB, " & _
          " FILEGROWTH = 10%) "
        CONN.Open()
        CMD = New SqlCommand(str, CONN)
        CMD.ExecuteNonQuery()

        If ComboBox2.Text = "Windows Authentication" Then
            Call MembuatTabelWindows()
            Call CreateUserDSNwindows()
            CONN.Close()
        Else
            Call MembuatTabelSQL()
            Call CreateUserDSNSql()
            CONN.Close()
        End If

    End Sub

    Sub MembuatTabelSQL()
        Try
            Call KoneksiServer()
            Dim TBLUSER As String = "CREATE TABLE  [TBLUSER](    [KODE_USER] [char](5) NOT NULL,    [NAMA_USER] [varchar](30) NULL,    [PWD_USER] [varchar](10) NULL,    [STATUS_USER] [varchar](15) NULL,) ON [PRIMARY]"
            CMD = New SqlCommand(TBLUSER, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim SIMPANUSER As String = "INSERT INTO tbluser values ('USR01','AKU','ADMIN','ADMIN')"
            CMD = New SqlCommand(SIMPANUSER, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim TBLKONFIGURASI As String = "CREATE TABLE  [TBLKONFIGURASI](    [Server] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [DTBS] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [UserId] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [Password] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ON [PRIMARY]"
            CMD = New SqlCommand(TBLKONFIGURASI, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim SIMPANKONFIGURASI As String = "INSERT INTO TBLKONFIGURASI values ('" & ComboBox1.Text & "','" & ComboBox3.Text & "','" & TextBox1.Text & "','" & TextBox2.Text & "')"
            CMD = New SqlCommand(SIMPANKONFIGURASI, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tblcustomer As String = "CREATE TABLE  [TBLCUSTOMER](    [KODE_CUSTOMER] [char](5) NOT NULL,    [NAMA_CUSTOMER] [varchar](30) NULL,    [ALAMAT_CUSTOMER] [varchar](50) NULL,    [TELEPON_CUSTOMER] [varchar](15) NULL,    [CONTACT_PERSON] [varchar](30) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblcustomer, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tblpenjualan As String = "CREATE TABLE  [TBLPENJUALAN](    [FAKTUR] [char](10) NOT NULL,    [TANGGAL] [datetime] NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [TOTALHARGA] [numeric](8, 0) NULL,    [DIBAYAR] [numeric](8, 0) NULL,    [KEMBALI] [numeric](8, 0) NULL,    [KODE_CUSTOMER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblpenjualan, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tbldetailjual As String = "CREATE TABLE  [TBLDETAILJUAL](    [FAKTUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [HARGA] [numeric](8, 0) NULL,    [JUMLAH] [numeric](4, 0) NULL,    [SUBTOTAL] [numeric](8, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailjual, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tblbarang As String = "CREATE TABLE  [TBLBARANG](    [KODE_BARANG] [char](13) NOT NULL,    [NAMA_BARANG] [varchar](30) NULL,    [SATUAN] [varchar](15) NULL,    [HARGA_BELI] [numeric](8, 0) NULL,    [HARGA_JUAL] [numeric](8, 0) NULL,    [STOK] [numeric](4, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblbarang, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tblpembelian As String = "CREATE TABLE  [TBLPEMBELIAN](    [FAKTUR] [char](10) NOT NULL,    [TANGGAL] [datetime] NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [TOTALHARGA] [numeric](8, 0) NULL,    [DIBAYAR] [numeric](8, 0) NULL,    [KEMBALI] [numeric](8, 0) NULL,    [KODE_SUPPLIER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblpembelian, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tbldetailbeli As String = "CREATE TABLE  [TBLDETAILBELI](    [FAKTUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [HARGA] [numeric](8, 0) NULL,    [JUMLAH] [numeric](4, 0) NULL,    [SUBTOTAL] [numeric](8, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailbeli, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tblreturpenjualan As String = "CREATE TABLE  [TBLRETURPENJUALAN](    [NOMOR_RETUR] [char](10) NOT NULL,    [TANGGAL_RETUR] [datetime] NULL,    [FAKTUR] [char](10) NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [KODE_CUSTOMER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblreturpenjualan, CONN)
            CMD.ExecuteNonQuery()

            Call KoneksiServer()
            Dim tbldetailreturjual As String = "CREATE TABLE  [TBLDETAILRETURJUAL](    [NOMOR_RETUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [QTY_RETUR] [numeric](4, 0) NOT NULL,    [KETERANGAN] [varchar](30) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailreturjual, CONN)
            CMD.ExecuteNonQuery()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub MembuatTabelWindows()
        Try
            Call Koneksiwindows()
            Dim TBLUSER As String = "CREATE TABLE  [TBLUSER](    [KODE_USER] [char](5) NOT NULL,    [NAMA_USER] [varchar](30) NULL,    [PWD_USER] [varchar](10) NULL,    [STATUS_USER] [varchar](15) NULL,) ON [PRIMARY]"
            CMD = New SqlCommand(TBLUSER, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim SIMPANUSER As String = "INSERT INTO tbluser values ('USR01','AKU','ADMIN','ADMIN')"
            CMD = New SqlCommand(SIMPANUSER, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim TBLKONFIGURASI As String = "CREATE TABLE  [TBLKONFIGURASI](    [Server] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [DTBS] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [UserId] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,    [Password] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) ON [PRIMARY]"
            CMD = New SqlCommand(TBLKONFIGURASI, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim SIMPANKONFIGURASI As String = "INSERT INTO TBLKONFIGURASI values ('" & ComboBox1.Text & "','" & ComboBox3.Text & "','" & TextBox1.Text & "','" & TextBox2.Text & "')"
            CMD = New SqlCommand(SIMPANKONFIGURASI, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tblcustomer As String = "CREATE TABLE  [TBLCUSTOMER](    [KODE_CUSTOMER] [char](5) NOT NULL,    [NAMA_CUSTOMER] [varchar](30) NULL,    [ALAMAT_CUSTOMER] [varchar](50) NULL,    [TELEPON_CUSTOMER] [varchar](15) NULL,    [CONTACT_PERSON] [varchar](30) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblcustomer, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tblpenjualan As String = "CREATE TABLE  [TBLPENJUALAN](    [FAKTUR] [char](10) NOT NULL,    [TANGGAL] [datetime] NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [TOTALHARGA] [numeric](8, 0) NULL,    [DIBAYAR] [numeric](8, 0) NULL,    [KEMBALI] [numeric](8, 0) NULL,    [KODE_CUSTOMER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblpenjualan, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tbldetailjual As String = "CREATE TABLE  [TBLDETAILJUAL](    [FAKTUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [HARGA] [numeric](8, 0) NULL,    [JUMLAH] [numeric](4, 0) NULL,    [SUBTOTAL] [numeric](8, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailjual, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tblbarang As String = "CREATE TABLE  [TBLBARANG](    [KODE_BARANG] [char](13) NOT NULL,    [NAMA_BARANG] [varchar](30) NULL,    [SATUAN] [varchar](15) NULL,    [HARGA_BELI] [numeric](8, 0) NULL,    [HARGA_JUAL] [numeric](8, 0) NULL,    [STOK] [numeric](4, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblbarang, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tblpembelian As String = "CREATE TABLE  [TBLPEMBELIAN](    [FAKTUR] [char](10) NOT NULL,    [TANGGAL] [datetime] NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [TOTALHARGA] [numeric](8, 0) NULL,    [DIBAYAR] [numeric](8, 0) NULL,    [KEMBALI] [numeric](8, 0) NULL,    [KODE_SUPPLIER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblpembelian, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tbldetailbeli As String = "CREATE TABLE  [TBLDETAILBELI](    [FAKTUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [HARGA] [numeric](8, 0) NULL,    [JUMLAH] [numeric](4, 0) NULL,    [SUBTOTAL] [numeric](8, 0) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailbeli, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tblreturpenjualan As String = "CREATE TABLE  [TBLRETURPENJUALAN](    [NOMOR_RETUR] [char](10) NOT NULL,    [TANGGAL_RETUR] [datetime] NULL,    [FAKTUR] [char](10) NULL,    [JUMLAHBARANG] [numeric](4, 0) NULL,    [KODE_CUSTOMER] [char](5) NULL,    [KODE_USER] [char](5) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tblreturpenjualan, CONN)
            CMD.ExecuteNonQuery()

            Call Koneksiwindows()
            Dim tbldetailreturjual As String = "CREATE TABLE  [TBLDETAILRETURJUAL](    [NOMOR_RETUR] [char](10) NULL,    [KODE_BARANG] [char](13) NULL,    [NAMA_BARANG] [varchar](30) NULL,    [QTY_RETUR] [numeric](4, 0) NOT NULL,    [KETERANGAN] [varchar](30) NULL) ON [PRIMARY]"
            CMD = New SqlCommand(tbldetailreturjual, CONN)
            CMD.ExecuteNonQuery()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        If ComboBox2.Text = "SQL Server Authentication" And RadioButton1.Checked = True Then
            Call KoneksiServer()
            'segera membuat tabel2 dan dsn
            MsgBox("Koneksi sukses")
        ElseIf ComboBox2.Text = "Windows Authentication" And RadioButton1.Checked = True Then
            Call Koneksiwindows()
            'membuat tabel2 dan dsn
            MsgBox("Koneksi sukses")
        ElseIf ComboBox2.Text = "SQL Server Authentication" And RadioButton2.Checked = True Then
            Call KoneksiMaster()
            Call MembuatDatabase() 'tabel2 dan dsn
            'Call MembuatTabel()
            MsgBox("Database dan tabel berhasil dibuat")
        ElseIf ComboBox2.Text = "Windows Authentication" And RadioButton2.Checked = True Then
            Call KoneksiMaster()
            Call MembuatDatabase() 'tabel2 dan dsn
            'Call MembuatTabel()
            MsgBox("Database dan tabel berhasil dibuat")
        End If
    End Sub

  
End Class


EmoticonEmoticon