Tag » Visual Basic

VB SqlClient Read data from database


Imports System.Data.SqlClient
Public Class frmDatabaseConn

Public strSQLConn As String

Private Sub frmDatabaseConn_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

strSQLConn = "Data Source=ServerName;Initial Catalog=AdventureWorks2014;Persist Security Info=True;User ID=sa;Password=YourSecret"

ReadDataFromDB()

End Sub

Private Sub ReadDataFromDB()

Dim sqlConn As New SqlConnection(strSQLConn)

Dim cmd As New System.Data.SqlClient.SqlCommand
Dim myReader As SqlDataReader

cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "Select itemCode , itemName from ItemMaster "
cmd.Connection = sqlConn

sqlConn.Open()
myReader = cmd.ExecuteReader()

While myReader.Read

dgSample.Rows.Add(myReader.Item("itemCode"), myReader.Item("itemname"))

End While

sqlConn.Close()

End Sub

 

Visual Basic

VB SqlClient Insert Data to database


Imports System.Data.SqlClient
Public Class frmDatabaseConn

Public strSQLConn As String

Private Sub frmDatabaseConn_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

strSQLConn = "Data Source=ServerName;Initial Catalog=AdventureWorks2014;Persist Security Info=True;User ID=sa;Password=Your@Secret"

End Sub

Private Sub btnInsertPara_Click(sender As System.Object, e As System.EventArgs) Handles btnInsertPara.Click
InsertWithPara()
End Sub

Private Sub InsertWithPara()

Dim sqlConn As New SqlConnection(strSQLConn)

Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "INSERT INTO ItemMaster ( ItemCode, ItemName ,CategoryCode ,Color) Values ( @itemcode , @itemName, @catecode, @color) "
cmd.Connection = sqlConn

Try
cmd.Parameters.AddWithValue("@itemcode", "60300")
cmd.Parameters.AddWithValue("@itemName", "AirLine Max")
cmd.Parameters.AddWithValue("@catecode", 9)
cmd.Parameters.AddWithValue("@color", "Green")

'cmd.Parameters.Add("@itemcode", SqlDbType.NVarChar)
'cmd.Parameters["@itemcode"].Value = textBox1.Text

sqlConn.Open()
cmd.ExecuteNonQuery()
sqlConn.Close()

MsgBox("Insert Success", MsgBoxStyle.Information)

Catch ex As Exception
sqlConn = Nothing
End Try

End Sub

 

Visual Basic

VB Inverse Sine, Cosine, Tangent

Inspection


Private Sub frmCalculation_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

Dim dblAdjacent As Double = 3
Dim dblOpposite As Double = 4
Dim dblHypotenuse As Double = 5

Dim mySine As Double
Dim myCosine As Double
Dim myTangent As Double

mySine = CalcSine(dblOpposite, dblHypotenuse)
myCosine = CalcCosine(dblAdjacent, dblHypotenuse)
myTangent = CalcTangent(dblOpposite, dblAdjacent)

LblSineValue.Text = mySine
LblCosineValue.Text = myCosine
LblTangentValue.Text = myTangent

'Returns the angle whose sine is the specified number. 164 kata lagi
Visual Basic

VB Simple Collision Detection

Private Sub CollisionDetect()

LblGroupA_RE.Text = “—”
LblGroupA_LE.Text = “—”
LblGroupA_TC.Text = “—”
LblGroupA_BC.Text = “—”

If PicMe.Left < PicEnemy.Left AndAlso PicMe.Right > PicEnemy.Left Then
If PicMe.Top < PicEnemy.Top AndAlso PicMe.Bottom > PicEnemy.Bottom Then… 142 kata lagi

Visual Basic

VB Move Object Location

Private Sub GetInfo()

InfoPic1.Text = “Seiya” & vbNewLine & _
“Top — ” & PicMe.Top & vbNewLine & _
“Bottom — ” & PicMe.Bottom & vbNewLine & _… 194 kata lagi

Visual Basic

VB IntersectsWith

Private Sub IntersectCheck()

‘PictureBox 1 VS PictureBox 2

If PicMe.Bounds.IntersectsWith(PicEnemy.Bounds) Then
LblGroupA.Text = “Collision”
Else
LblGroupA.Text = “No Contact”
End If

If PictureBox3.Bounds.IntersectsWith(PictureBox4.Bounds) Then
lblGroupB.Text = “Collision” 50 kata lagi

Visual Basic