Examples of code

Posted by dragon on Friday, 30 September 2011

I make the example of a table with 2 fields for semplicity: for the text fields use the type varchar which allows a better positioning of the cursor in edit mode.
First add to the project the "Firebirdclient-ADO NET xx Data Provider" reference.
In the connection string use servertype=1 for embedded server and servertype=0 for client server application.
The Dataset is a component of VB.net and is not typified. The field “Clienti” is a field varchar. For “Spesa” field I have chosen the type smallint. We'll use 3 winforms for the project: "MainForm", "NewForm", "EditForm".
The operations are there described with comments at the code:

Imports FirebirdSql.Data.FirebirdClient
__________________________________________________________________
Public Class MainForm



Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Fdataa As New FbDataAdapter("select*from New_table1", "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\Giorgio.gdb")

DataSet1.Tables.Add("New_table1")
Me.DataGridView1.DataSource = DataSet1
Me.DataGridView1.DataMember = "New_table1"

Fdataa.Fill(DataSet1, "New_table1")

End Sub


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
NewForm.Show()
End Sub
'To begin the change
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'I used the trim function because would be problems with positioning of the cursor if you will pass from varcahr type to char type and vice versa.
EditForm.TextBox1.Text =

Microsoft.VisualBasic.Trim(DataGridView1.CurrentRow.Cells(0).Value)


EditForm.TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value


EditForm.Show()
End Sub

'To delete a record


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim mResult
Dim ObjConnection As New FbConnection()
mResult = MsgBox("Si desidera eliminare questo cliente?", _
vbYesNo + vbQuestion, "Conferma eliminazione")
If mResult = vbNo Then
Exit Sub
End If
ObjConnection.ConnectionString = "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\miodb.gdb"
ObjConnection.Open()
Try
Dim ObjCommand As New FbCommand()
ObjCommand.Connection = ObjConnection

ObjCommand.CommandText = "delete from New_table1 where Clienti='" & Me.DataGridView1.CurrentRow.Cells(0).Value & "'"
ObjCommand.ExecuteNonQuery()
Finally
ObjConnection.Close()
End Try

Me.DataGridView1.Rows.Remove(Me.DataGridView1.CurrentRow)


End Sub
End Class

'To update a record

Imports FirebirdSql.Data.FirebirdClient
______________________________________________________________
Public Class EditForm



Private Sub btnAggiorna_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAggiorna.Click
Dim customerRow() = MainForm.DataSet1.Tables("New_table1").Select("Clienti ='" & MainForm.DataGridView1.CurrentRow.Cells(0).Value & "'")


Dim ObjConnection As New FbConnection()

ObjConnection.ConnectionString = "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\miodb.gdb"
ObjConnection.Open()
Try
Dim ObjCommand As New FbCommand()
ObjCommand.Connection = ObjConnection

ObjCommand.CommandText = "update New_table1 set Clienti='" & Me.TextBox1.Text & "', Spesa='" & Me.TextBox2.Text & "' where Clienti='" & 

MainForm.DataGridView1.CurrentRow.Cells(0).Value & "'"
ObjCommand.ExecuteNonQuery()
Finally
ObjConnection.Close()
End Try

customerRow(0)("Clienti") = Me.TextBox1.Text
customerRow(0)("Spesa") = Me.TextBox2.Text

Me.Close()
MainForm.Show()
End Sub
End Class


'To insert e new record

Imports FirebirdSql.Data.FirebirdClient
____________________________________________________________
Public Class NewForm


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ObjConnection As New FbConnection()

ObjConnection.ConnectionString = "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\miodb.gdb"
ObjConnection.Open()
Try
Dim ObjCommand As New FbCommand()
ObjCommand.Connection = ObjConnection
ObjCommand.CommandText = "insert into New_table1 (Clienti, Spesa) values ('" & TextBox1.Text "','" & TextBox2.Text & "')"
ObjCommand.ExecuteNonQuery()
Finally
ObjConnection.Close()
End Try



Dim newCustomersRow As DataRow = MainForm.DataSet1.Tables("New_table1").NewRow()
newCustomersRow("Clienti") = TextBox1.Text
newCustomersRow("Spesa") = TextBox2.Text
MainForm.DataSet1.Tables("New_table1").Rows.Add(newCustomersRow)



Me.Close()
MainForm.Show()
End Sub

End Class


This code is worth for application not connected in net.
For an application connected in net only change the things about which we have before spoken.



{ 0 comments... read them below or add one }

Post a Comment