ចំ/ ត្រៀមប្រលង (T. Neang Mao)

Module Mconnection
Public StrCon As String = “server=.;database=Night;uid=sa”
End Module

Imports System.Data
Imports System.Data.SqlClient
Public Class Cperson
Public Function getData() As DataTable
Dim strcmd As String
strcmd = “select * from TblPersonality”
Dim ODA As New SqlDataAdapter(strcmd, StrCon)
Dim ODT As New DataTable
Try
ODA.Fill(ODT)
Catch ex As Exception
Finally
ODA.Dispose()

End Try
Return ODT
End Function

Public Function AddData(ByVal PNo As String, ByVal Des As String) As Boolean
Dim Ocon As New SqlConnection(StrCon)
Dim StrCmd As String
StrCmd = “Insert into TblPersonality(PNo,Des) Values (@PNo,@Des)”
Dim oCmd As New SqlCommand
oCmd.Connection = Ocon
oCmd.CommandType = CommandType.Text
oCmd.CommandText = StrCmd
oCmd.Parameters.Add(“@PNo”, SqlDbType.VarChar, 50).Value = PNo
oCmd.Parameters.Add(“@Des”, SqlDbType.VarChar, 50).Value = Des
Dim RV As Boolean = True
Try
Ocon.Open()
oCmd.ExecuteNonQuery()
Catch ex As Exception
RV = False
Finally
oCmd.Dispose()
Ocon.Dispose()
End Try
Return RV
End Function
Public Function UpdateData(ByVal OldPNo As String, ByVal PNo As String, ByVal Des As String) As Boolean
Dim Ocon As New SqlConnection(StrCon)
Dim StrCmd As String
StrCmd = “UPDATE TblPersonality SET PNo=@PNo,Des=@Des WHERE PNO=@OldPNo”
Dim oCmd As New SqlCommand
oCmd.Connection = Ocon
oCmd.CommandType = CommandType.Text
oCmd.CommandText = StrCmd
oCmd.Parameters.Add(“@OldPNo”, SqlDbType.VarChar, 50).Value = OldPNo
oCmd.Parameters.Add(“@PNo”, SqlDbType.VarChar, 50).Value = PNo
oCmd.Parameters.Add(“@Des”, SqlDbType.NVarChar, 50).Value = Des
Dim RV As Boolean = True
Try
Ocon.Open()
oCmd.ExecuteNonQuery()
Catch ex As Exception
RV = False
Finally
oCmd.Dispose()
Ocon.Dispose()
End Try
Return RV
End Function
Public Function DeleteData(ByVal PNo As String) As Boolean
Dim Ocon As New SqlConnection(StrCon)
Dim StrCmd As String
StrCmd = “DELETE FROM TblPersonality WHERE PNo = @PNo”
Dim oCmd As New SqlCommand
oCmd.Connection = Ocon
oCmd.CommandType = CommandType.Text
oCmd.CommandText = StrCmd
oCmd.Parameters.Add(“@PNo”, SqlDbType.VarChar, 50).Value = PNo
Dim RV As Boolean = True
Try
Ocon.Open()
oCmd.ExecuteNonQuery()
Catch ex As Exception
RV = False
Finally
oCmd.Dispose()
Ocon.Dispose()
End Try
Return RV
End Function

End Class

Imports System.Data
Public Class Form1
Private Sub getData()
Dim oP As New CPersonality
Dim odt As New DataTable
Try
odt = oP.getData
DGV.DataSource = odt
DGV.Columns(0).Width = 200
DGV.Columns(1).Width = 100
Catch ex As Exception
Finally
oP = Nothing
End Try
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim oP As New CPersonality
If oP.AddData(txtPNo.Text, txtDes.Text) Then
getData()
txtPNo.Enabled = True
ClearMe()
txtPNo.Focus()
MsgBox(“លោកអ្នកបញ្ចូល 1Recordeនេះបានសំរេចហើយ អគុណ!”)
Else
MsgBox(“អ្កកបញ្ចូលមិនត្រឹមត្រូវទេ!”, MsgBoxStyle.Information)

End If

End Sub
Sub ClearMe()
txtPNo.Text = “”
txtDes.Text = “”
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
getData()
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim op As New Cperson
Try
If op.UpdateData(txtPNo.Text, txtDes.Text) Then
getData()
MsgBox(“The record has been succussfully updated!”)
Else
MsgBox(“Error updating Data”, MsgBoxStyle.Information, “Update Infor”)
End If
Catch ex As Exception
Finally

op = Nothing
End Try
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox(“តើអ្នកចង់លុប 1 Recorde នេះចោលមែនទេ?”, MsgBoxStyle.YesNo, “Confirmation”) = MsgBoxResult.Yes Then
Dim op As New Cperson
If op.DeleteData(DGV.SelectedRows(0).Cells(0).Value) Then
getData()
Else
MsgBox(“Existing This!”, MsgBoxStyle.Information)
End If
op = Nothing
End If
End Sub

Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick
Try
txtPNo.Text = DGV.SelectedRows(0).Cells(0).Value
txtDes.Text = DGV.SelectedRows(0).Cells(1).Value
txtPNo.Enabled = False
‘ MsgBox(DGV.SelectedRows(0).Cells(1).Value.ToString())
Catch ex As Exception

End Try
End Sub
End Class
—————————————————————————————————————-
Public Class CNum
‘Data Mmber As Private
Private _Name As String
Private _DOB As Date

‘ReadWrite Property Name
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property

‘ReadWrite Property Date Of Birth
Public Property DOB() As Date
Get
Return _DOB
End Get
Set(ByVal value As Date)
_DOB = value
End Set
End Property

‘ReadOnly Property
Public ReadOnly Property Age() As String
Get
Return (Now.Year – DOB.Year)
End Get
End Property

‘Method Letter To Digit
Private Function Letter_To_Digit(ByVal S As String) As Short

‘Change Sttring S To Upper Case
S = S.ToUpper()

‘Switch Statement For Change Letter To Number
Select Case S
Case “A”, “J”, “S”
Return 1
Case “B”, “K”, “P”
Return 2
Case “C”, “L”, “U”
Return 3
Case “D”, “M”, “V”
Return 4
Case “E”, “N”, “W”
Return 5
Case “S”, “O”, “X”
Return 6
Case “G”, “P”, “Y”
Return 7
Case “H”, “Q”, “Z”
Return 8
Case “I”, “R”
Return 9
Case Else
Return 0
End Select

End Function
‘Method Get String To Number
Private Function Name_To_Num(ByVal n As String) As String

Dim i As Integer
Dim S As String = “”
For i = 0 To n.Length – 1
S = S + (Letter_To_Digit(n(i))).ToString()
Next

Return S
End Function

‘Method Reduce Number
Private Function Reduces(ByVal n As String) As String
Dim i As Integer
Dim Result As Integer

If n.Length = 1 Then
Return Convert.ToInt16(n)
ElseIf n.Length 1
Result = 0
For i = 0 To n.Length – 1
Result = Result + Convert.ToInt16(n(i).ToString())
Next
n = Result.ToString()
‘MsgBox(n)
End While

Return Result
End Function

Public ReadOnly Property Personality() As Short
Get
Return Reduces(Name_To_Num(_Name))
End Get
End Property

Public ReadOnly Property Destination() As Short
Get
Return Reduces(Convert.ToString(_DOB.Year + _DOB.Month + _DOB.Day))
End Get
End Property

End Class

Public Class Form1
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim oNu As New CNum
If txtName.Text “” Then
oNu.name = txtName.Text
lblPersonal.Text = oNu.Personality
End If
oNu.DOB = Dpt.Value
lblDestination.Text = oNu.Destination
oNu = Nothing
End Sub
End Class
¬

About angkortown

I am a programer
This entry was posted in សំរាប់ត្រៀមប្រលង. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s