SQL select query එකකින් TextBox එකකට දැමීම
Public Class Form1Dim con As New SqlConnection
Dim cmd As New SqlCommand
--------------------------------------------------
Private Sub ButtonSearch_Click(ByVa...........
con = New SqlConnection("server=localhost;integrated security=true;database=CDAP")
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "select username from NPUser where Password='aa'"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
TextBox2.Text = lrd.GetString(0)
End While
Catch ex As Exception
MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
Finally
con.Close()
End Try
End Sub
SQL table data insert කිරීම
Dim con As New SqlConnectionDim cmd As New SqlCommand
--------------------------------------------------
con = New SqlConnection("server=localhost;integrated security=true;database=CDAP")
con.Open()
cmd.Connection = con
cmd.CommandText = "insert into PUser values ('a','b','c',500)"
''''''''''මෙහිදී TextBox වල අගයක් quarry එක සදහා යොදාගන්නේ නම් මෙලෙස යොදන්න
'''''''''' cmd.CommandText = "insert into PUser values ( ' "+TextBox1.Text+" ' ,'b','c',500)"
cmd.ExecuteNonQuery()
MessageBox.Show("New Row Inserted")
con.Close()
update, delete කිරීමට හැකි වන පරිදි dataGridView එකකට දත්ත ලබා ගැනීම.
Public Class Form1Dim con As New SqlConnection
Dim adapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim table As DataTable
Dim cmd As New SqlCommand
Dim ds As New DataSet
....................................................................
Private Sub ButtonSearchToGrid_Click(ByVa...........
con = New SqlConnection("server=localhost;integrated security=true;database=Medical Centre")
cmd.CommandText = "select * from Patient where Patient_ID='P0001'"
cmd.Connection = con
con.Open()
adapter = New SqlDataAdapter(cmd)
sBuilder = New SqlCommandBuilder(adapter)
ds = New DataSet()
adapter.Fill(ds, "Stores")
table = ds.Tables("Stores")
con.Close()
DataGridView1.DataSource = ds.Tables("Stores")
'save_btn.Enabled = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub ButtonDeleteGrid_Click(ByVa...........
If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
sAdapter.Update(sTable)
End If
End Sub
Private Sub ButtonUpdateGrid_Click(ByVa...........
Try
adapter.Update(table)
MessageBox.Show("Updated successfully", "Update")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
වෙනත් form එකක් පෙන්වීමට හා තිබෙන එක close කිරීමට
Form2.Show()Me.Close()
String ID එකක් auto generate කිරීම
con = New SqlConnection("server=localhost;integrated security=true;database=DATA")If con.State = ConnectionState.Closed Then
con.Open()
End If
cmd.Connection = con
Dim n As String
cmd.CommandText = "select max(Bill_No)from Bill"
If IsDBNull(cmd.ExecuteScalar) Then
n = "B0001"
txtChanPay_BillNo.Text = n
Else
n = cmd.ExecuteScalar
n = n.Remove(0, 1)
n = n + 1
txtChanPay_BillNo.Text = String.Format("B{0:D4}", Convert.ToInt32(n))
End If
cmd.Dispose()
Form load එකේදී comboBox එකකට sql data column එකක් add කර ගැනීම
Dim con As New SqlConnectionDim cmd As New SqlCommand
-----------------------------------------------------------------------
Private Sub frm_Load...........
con = New SqlConnection("server=localhost;integrated security=true;database=DATA")
Try
con.Open()
cmd.Connection = con
cmd.CommandText = "select HN_ID from Home_Nursing"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
cmbHNid.Items.Add(lrd.GetString(0))
End While
Catch ex As Exception
MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
Finally
con.Close()
End Try
End Sub
No comments:
Post a Comment