'This Script connects SQL Server database and gets the record count from a table.
Imports System 'Importing necessary classes
Imports System.IO
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Namespace WinTr
Public Class MainClass
Private Withevents btn as New Button 'Button is defined. Because of button's click event
'will be used it is defined with "Withevents" keyword.
Private Textbox1 as New Textbox 'Textbox is defined.
Public Sub Load 'Load procedure is defining. Script starts working within this procedure.
'WinTr tags should be accessed from this procedure.
'When this procedure finished working,
'WinTr tags can not be reached anymore from this script.
'------- Script Start Line -------
Dim frm as new form 'Form object defined which holds the interface.
Dim lbl as new label 'Label object is created which contain text.
lbl.top=65 'Setting Label's location.
lbl.left=20
lbl.width=75 'Setting Label's width.
textbox1.top=62 'Setting Textbox's location.
textbox1.left=100
lbl.text= "Record Count" 'Assigning text to label
btn.top= 120 'Setting Button's location.
btn.left=105
btn.text= "Ok" 'Assigning text to button's text property.
frm.controls.add(lbl) 'Adding Label object to form
frm.controls.add(btn) 'Adding Button object to form
frm.controls.add(textbox1) 'Adding Textbox object to form
frm.height=200 'Setting form's size
frm.width=300
frm.showdialog
'------- Script End Line -------
End Sub 'End of the load procedure
Sub btnhandler(sender as object,e as system.eventargs) Handles btn.click 'Button's event handler is defined.
Dim SQL_cn As New SqlConnection() 'Database connection object is defined.
SQL_cn.ConnectionString = "Server=(local)\WinTr;uid=sa;pwd=12341234;database=Project_1" 'Database connection string is generated.
SQL_cn.Open() 'Database connection opened.
Dim SQL_Ins As String 'Variable that will contain the SQL query is defined.
SQL_Ins = "SELECT COUNT(*) FROM Table1" 'Query for getting last record's number is assigning to string variable.
Dim SQL_cmdnon As SqlCommand = New SqlCommand(SQL_Ins, SQL_cn) 'SQL query object created which contains database connection and query string.
Dim Last_Value As String 'variable created which will hold last record's number
Last_Value=SQL_cmdnon.ExecuteScalar 'Query object executed. Returned value assigned to "LastValue" variable.
textbox1.text=(Last_Value) 'Last_Value displayed in the textbox.
end sub
End Class
End Namespace