9/21/2011

'Display Data' & 'Add Data' (MS SQL , ASP.NET)

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





No comments:

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.  ...