Tag: example

SQL replace string in a field

The example replaces the string height=”480″ with the string height=”320″.

UPDATE Blog
SET myPost = REPLACE(CAST(myPost AS varchar(MAX)),’height=”480″‘, ‘height=”320″‘)
FROM Blog
WHERE CHARINDEX(‘height=”480″‘,CAST(myPost as varchar(MAX)))>0
AND myKey=2469

AutoTab to Next Field

Private Sub txtIP3_TextChanged(sender As Object, e As EventArgs)
Handles txtIP3.TextChanged

If txtIP3.Text.Length = txtIP3.MaxLength _
  Then SelectNextControl(ActiveControl, True, True, True, True)

End Sub

Reverse DNS IP Address Lookup

This example illustrates how to get the machine name from an IP address using a reverse DNS lookup.

Imports System
Imports System.Net

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim arr() As String
Dim WS As String = “”

Dim addr As IPAddress = IPAddress.Parse(“155.110.10.65”)
Dim entry As IPHostEntry = Dns.GetHostEntry(addr)
Console.WriteLine(entry.HostName)

arr = Split(entry.HostName, “.”)
WS = arr(0)
Console.WriteLine(WS)

End
End Sub
End Class

Start/Stop/Query Service in VB.NET

Imports System.ServiceProcess

Public Class Form1

Private Sub btnQueryBES_Click(sender As System.Object, e As System.EventArgs) Handles btnQueryBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "query")
End Sub
Private Sub btnStartBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStartBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "start")
End Sub
Private Sub btnStopBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStopBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "stop")
End Sub
Function SC(MachineName As String, ServiceName As String, cmd As String) As String
 Dim txt As String = ""
 Dim TermService As New System.ServiceProcess.ServiceController(ServiceName, MachineName)

Select Case LCase(cmd)
 Case "start"
 TermService.Start()
 Case "stop"
 TermService.Stop()
 Case Else
 End Select

Select Case LCase(TermService.Status)
 Case "1"
 txt = " stopped"
 Case "2"
 txt = " starting"
 Case "3"
 txt = " stop pending"
 Case "4"
 txt = " running"
 Case Else
 txt = " undetermined"
 End Select

Return txt
 End Function

End Class

VB.NET Module for SQL Database

Imports System.Data.SqlClient

Module modDatabase

Public conn As New SqlClient.SqlConnection
Public myReader As SqlDataReader

Public dsRecords As New DataSet
Public daRecords As New SqlDataAdapter
Public MaxRecords As Integer

Public Sub OpenDatabase()
CloseSQLReader()
CloseDatabase()

conn.ConnectionString = “Server=.\sqlexpress;Database=DATABASE;Trusted_Connection=True;”
conn.Open()
End Sub

 

Public Sub CloseDatabase()
Try
conn.Close()
conn.Dispose()
Catch ex As Exception
Application.DoEvents()
End Try
End Sub

 

Public Sub launchBrowser(ByVal browser As String, ByVal url As String)
Try
Process.Start(browser, url)
Catch ex As Exception
Application.DoEvents()
‘MessageBox.Show(ex.Message)
End Try
End Sub

 

Public Sub CloseSQLReader()
Try
myReader.Close()
Catch ex As Exception
Application.DoEvents()
End Try
End Sub

 

Public Sub ReadSqlData(ByVal sqlSelect As String)
Dim myCommand As New SqlCommand(sqlSelect, conn)
myReader = myCommand.ExecuteReader()
End Sub

 

Public Function ExecuteSqlCommand(ByVal sqlSelect As String) As Boolean
Dim flg As Boolean = False
Dim myCommand As New SqlCommand(sqlSelect, conn)

Try
flg = myCommand.ExecuteNonQuery
Catch
flg = False
End Try

Return flg
End Function

 

Public Sub HoldYourPants(ByVal Seconds As Integer)
System.Threading.Thread.Sleep(Seconds * 1000)
Application.DoEvents()
End Sub

End Module