Proses menyambung ASP.NET (web development) kepada database MS SQL Server agakmengelirukan kerana banyak objek yang perlu di guna dan terdapat pelbagai teknik.
Berikut di sertakan 2 contoh:-
1. Paparkan data
2. Tambah data
Interface saya adalah seperti berikut
Koding bagi dua button di atas seperti berikut:-
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
'berjaya pamir data
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As SqlDataAdapter
Dim ds As New DataSet ' yang ni kena ada Imports System.Data
Dim i As Integer
connetionString = "Server =4MEI-PC\MEMO; Database =dbmemo; Trusted_Connection =True;"
connection = New SqlConnection(connetionString)
Try
connection.Open()
adapter = New SqlDataAdapter("Select * from users", connection)
adapter.Fill(ds)
connection.Close()
For i = 0 To ds.Tables(0).Rows.Count - 1
Response.Write(ds.Tables(0).Rows(i).Item(1) & " ")
Response.Write(ds.Tables(0).Rows(i).Item(2) & "
")
Next
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'berjaya TAMBAH DATA
Dim connetionString As String
Dim connection As SqlConnection
Dim adapter As New SqlDataAdapter
Dim sql As String
connetionString = "Server =4MEI-PC\MEMO; Database =dbmemo; Trusted_Connection =True;"
connection = New SqlConnection(connetionString)
sql = "insert into users (uname,pass) values('ISA','4455')"
Try
connection.Open()
adapter.InsertCommand = New SqlCommand(sql, connection)
adapter.InsertCommand.ExecuteNonQuery()
MsgBox("Row inserted !! ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Berikut adalah maklumat MS SQL server dan Table yang di guna
Subscribe to:
Post Comments (Atom)
Cara download Installer windows 10 dalam format ISO
1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan. ...
-
ANSWERS TO CHAPTER EXERCISES (CHAPTER 8) Review Questions 1. List and describe various types of output, including technolog...
-
Based on real life scenario on any local company/ organization which you are very familiar, define the problem that could be addressed thr...
-
A. Response to the following statements by circling either T (TRUE) or F (FALSE). [10M] I. 1. Java enables users...
No comments:
Post a Comment