0% found this document useful (0 votes)
31 views

Sqlconnection Sqlconnection Sqlcommand Sqlcommand Sqldatareader Eventargs

This document contains code snippets from multiple programming languages (C#, VB.NET, SQL) related to connecting to and querying databases. It shows how to open and close database connections, execute SQL queries to retrieve and insert data, and check login credentials against a database.

Uploaded by

Aditya Manideep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Sqlconnection Sqlconnection Sqlcommand Sqlcommand Sqldatareader Eventargs

This document contains code snippets from multiple programming languages (C#, VB.NET, SQL) related to connecting to and querying databases. It shows how to open and close database connections, execute SQL queries to retrieve and insert data, and check login credentials against a database.

Uploaded by

Aditya Manideep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. imports system.data.

OleDb
Dim conn1 As OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\emp.mdb;")
Dim com1 As OleDbCommand("SELECT * FROM <table>",conn1)
Dim tasksAdapt As OleDbDataReader
conn1.open()
tasksAdapt = com1.executereader
conn1.close()

SqlConnection db = new
SqlConnection("server=CSJN028\\SQLEXPRESS;database=air;integrated
security=true");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
if (db.State == ConnectionState.Open) db.Close();
db.Open();
cmd = new SqlCommand("Insert into flightdet values('" +
txtfno.Text + "','" + txtfname.Text + "','" + txtsplace.Text +
"','" + txtdplace.Text + "','" +
txtwgt.Text + "')", db);
cmd.ExecuteNonQuery();
Msgbox("Saved Successfully");
clear();
}
catch (Exception ex)
{
Msgbox("Check the Data");
}
}

45.
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
OK.Click
'(UserIDTextBox.Text, PasswordTextBox.Text)
' Me.Close()
Dim connection As New SqlClient.SqlConnection
Dim command As New SqlClient.SqlCommand
Dim adaptor As New SqlClient.SqlDataAdapter

Dim dataset As New DataSet


'55Dim DataReader As New SqlClient.SqlDataReader
'Dim dr As New SqlClient.SqlDataReader
connection.ConnectionString = ("Data Source=.;Initial Catalog=MyFirstDB;Integrated
Security=True")
'Dim userid As String = UserIDTextBox.Text
'Dim Pwd As String = PasswordTextBox.Text
'Dim Utype As String = PasswordTextBox.Text
command.CommandText = "SELECT count(*) n FROM Login WHERE UserID='" &
UserIDTextBox.Text & "' AND Password='" & PasswordTextBox.Text & "';"
connection.Open()
command.Connection = connection
adaptor.SelectCommand = command
adaptor.Fill(dataset, "0")
Dim n As Integer
Integer.TryParse(CStr(dataset.Tables(0).Rows(0)("n")), n)
'idataset.Tables(0).Rows(0)("n")
'Dim n As Long = Long.Parse(dataset.Tables(0).Rows(0)(0).ToString())
'Dim str As String
connection.Close()
If n = 0 Then
MsgBox("You are not Authorized ")
ElseIf n = 1 Then
'command.CommandText = "INSERT INTO Login VALUES ('" & UserIDTextBox.Text & "'
,'" & PasswordTextBox.Text & "','admin');"
command.CommandText = "SELECT usertype x FROM Login WHERE UserID='" &
UserIDTextBox.Text & "' AND Password='" & PasswordTextBox.Text & "';"
connection.Open()
command.Connection = connection
adaptor.SelectCommand = command
adaptor.Fill(dataset, "1")
Dim UserType As String
UserType = CStr(dataset.Tables(1).Rows(0)("x"))
If UserType.ToLower() = "admin" Then
MessageBox.Show("admin ")
'MsgBox("admin form")
Else
MessageBox.Show("Other Form")
End If
' Me.Close()
ElseIf n = 2 Then
MessageBox.Show("DataBase Currepted")
End If

46.

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles OK.Click
'Me.Close()
Dim Connection As New SqlClient.SqlConnection

Dim Command As New SqlClient.SqlCommand


Dim Adapter As New SqlClient.SqlDataAdapter
Dim Dataset As New DataSet
Connection.ConnectionString = "Data Source=.;Initial
Catalog=MyFirstDB;Integrated Security=True"
Command.CommandText = "Select * from [Login] where UserID=" &
UsernameTextBox.Text & " AND Password='" & PasswordTextBox.Text & "';"
Connection.Open()
Command.Connection = Connection
Adapter.SelectCommand = Command
Adapter.Fill(Dataset, "0")
Dim count = Dataset.Tables(0).Rows.Count
Connection.Close()
Dim str As String
If count > 0 Then
MsgBox("Login Successfull", MsgBoxStyle.Information, "Login
Passed")
With dataset.Tables(0)
For rowNumber As Integer = 0 To .Rows.Count - 1
str =
dataset.Tables(0).Rows(rowNumber).Item(2).ToString().ToUpper()
'Console.WriteLine(String.Format("Field1: {0}, Field2:
{1}", _
'
dataset.Tables(0).Rows(rowNumber).Item(0).ToString(), _
'
dataset.Tables(0).Rows(rowNumber).Item(1).ToString()))
Next
If str.Contains("ADMIN") Then
Dim obj As New Admin
obj.ShowDialog()
Else
Dim obj As New Others
obj.ShowDialog()
End If
' Me.Close()
End With
Else
MsgBox("Invalid Account", MsgBoxStyle.Critical, "Login Failed")
UsernameTextBox.Clear()
PasswordTextBox.Clear()
End If

2.
ublic Class Form1

02
Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.E
0
3 ventArgs)Handles button.Click
04
05

'The Connection Object

06

'variable con will hold the Connection Object

07

Dim con As New OleDb.OleDbConnection

08
09

'Setting a Connection String

10

Dim dbProvider As String

11

Dim dbSource As String

12
13
14

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Users\Neelesh\Documents\Visual Studio
2008\Projects\TestPlatform\mmc.accdb"

15
16

con.ConnectionString = dbProvider & dbSource

17
18

con.Open() 'Open method of the Connection Object

19

MsgBox("Database is now open")

20
21

con.Close() 'Close method of the Connection Object

22

MsgBox("Database is now Closed")

23
24

End Sub

25 End Class

You might also like