Close
Duke shfaqur rezultatin -9 deri 0 prej 6
  1. #1
    i/e regjistruar
    Anėtarėsuar
    21-04-2011
    Postime
    42

    Ma beni nje skjarim cila eshte me e sigurt

    Une jam fillestar ne VB.net kam gjetur disa udhezime per programim ne internet per shtimin e rekordeve .
    mua mi kane mesuar nje menyre shum tjeter po du me ma be dikush nje krahasim mes ketyre dy metodave dhe a ka dikush ndonje metod per vb.net ne database qe eshte shum e lehte per shfrytezim dhe qe eshte e shpejt ne ekzekutim dmth programi te mos ngadalesohet.


    kodi qe e kam gjet ne internet duket keshtu
    -----------------
    Add a New Record

    Adding a new record is slightly more complex. First, you have to add a new Row to the DataSet, then commit the new Row to the Database.

    But the Add New Record button on our form is quite simple. The only thing it does is to switch off other buttons, and clear the textboxes, ready for a new entry. Here's the code for your Add New Record button:
    btnCommit.Enabled = True
    btnAddNew.Enabled = False
    btnUpdate.Enabled = False
    btnDelete.Enabled = False

    txtFirstName.Clear()
    txtSurname.Clear()
    So three buttons are switched off when the Add New Record button is clicked, and one is switched on. The button that gets switched on is the Commit Changes button. The Enabled property of btnCommit gets set to True. But, for this to work, you need to set it to False when the form loads. So return to your Form. Click btnCommit to select it. Then locate the Enabled Property in the Properties box. Set it to False. When the Form starts up, the button will be switched off.

    The Clear/Cancel button can be used to switch it back on again. So add this code to your btnClear:

    btnCommit.Enabled = False
    btnAddNew.Enabled = True
    btnUpdate.Enabled = True
    btnDelete.Enabled = True

    inc = 0
    NavigateRecords()

    We're switching the Commit Changes button off, and the other three back on. The other two lines just make sure that we display the first record again, after the Cancel button is clicked. Otherwise the textboxes will all be blank.

    To add a new record to the database, we'll use the Commit Changes button. So double click your btnCommit to access its code. Add the following:

    If inc <> -1 Then

    Dim cb As New OleDb.OleDbCommandBuilder(da)
    Dim dsNewRow As DataRow

    dsNewRow = ds.Tables("AddressBook").NewRow()

    dsNewRow.Item("FirstName") = txtFirstName.Text
    dsNewRow.Item("Surname") = txtSurname.Text

    ds.Tables("AddressBook").Rows.Add(dsNewRow)

    da.Update(ds, "AddressBook")

    MsgBox("New Record added to the Database")

    btnCommit.Enabled = False
    btnAddNew.Enabled = True
    btnUpdate.Enabled = True
    btnDelete.Enabled = True

    End If

    The code is somewhat longer than usual, but we'll go through it.

    The first line is an If Statement. We're just checking that there is a valid record to add. If there's not, the inc variable will be on minus 1. Inside of the If Statement, we first set up a Command Builder, as before. The next line is this:

    Dim dsNewRow As DataRow

    If you want to add a new row to your DataSet, you need a DataRow object. This line just sets up a variable called dsNewRow. The type of variable is a DataRow.

    To create the new DataRow object, this line comes next:

    dsNewRow = ds.Tables("AddressBook").NewRow()

    We're just saying, "Create a New Row object in the AddressBook DataSet, and store this in the variable called dsNewRow." As you can see, NewRow() is a method of ds.Tables. Use this method to add rows to your DataSet.

    The actual values we want to store in the rows are coming from the textboxes. So we have these two lines:

    dsNewRow.Item("FirstName") = txtFirstName.Text
    dsNewRow.Item("Surname") = txtSurname.Text

    The dsNewRow object we created has a Property called Item. This is like the Item property you used earlier. It represents a column in your DataSet. We could have said this instead:
    dsNewRow.Item(1) = txtFirstName.Text
    dsNewRow.Item(2) = txtSurname.Text

    The Item property is now using the index number of the DataSet columns, rather than the names. The results is the same, though: to store new values in these properties. We're storing the text from the textboxes to our new Row.

    We now only need to call the Method that actually adds the Row to the DataSet:

    ds.Tables("AddressBook").Rows.Add(dsNewRow)

    To add the Row, you use the Add method of the Rows property of the DataSet. In between the round brackets, you need the name of your DataRow (the variable dsNewRow, in our case).
    You should know what the rest of the code does. Here's the next line:

    da.Update(ds, "AddressBook")

    Again, we're just using the Update method of the Data Adapter, just like last time. The rest of the code just displays a message box, and resets the button.

    But to add a new Row to a DataSet, here's a recap on what to do:
    Create a DataRow variable
    Cretae an Object from this variable by using the NewRow() method of the DataSet Tables property
    Assign values to the Items in the new Row
    Use the Add method of the DataSet to add the new row
    A little more complicated, but it does work! Try your programme out. Click your Add New Record button. The textboxes should go blank, and three of the buttons will be switched off. Enter a new First Name and Surname, and then click the Commit Changes button. You should see the message box telling you that a new record has been added to the database. To see the new record, close down your programme, and run it again. The new record will be there.

    Ky eshte web linku ku e kam gjetur kete kod
    http://www.homeandlearn.co.uk/net/nets12p10.html

    Ndersa mua qe me kane msuar duket keshtu

    ------------------------
    Imports System
    Imports System.Diagnostics
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Windows.Forms
    Public Class KlientiAdd
    Dim Koneksioni As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "D:\PROGRAMET E PUNUARA\ACOUNTING\KSFB\KSFB\furnitori.mdb" & ";Persist Security Info=True")
    Dim Dataseti As New DataSet
    Dim shnr As New DataSet


    Private Sub KlientiAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Koneksioni.Open()
    Adap()
    Pamja.DataSource = Dataseti.Tables(0)
    cmbnrsh.DataSource = shnr.Tables(0)
    cmbnrsh.DisplayMember = "shnr"
    Koneksioni.Close()

    End Sub
    Private Sub Adap()
    Dim KlID As String
    KlID = Klienti.CmbNrsh.Text

    If KlID = "" Or KlID = Nothing Then
    Dim Adapteri As New OleDbDataAdapter("Select * from Klienti", Koneksioni)
    Dim shnradapteri As New OleDbDataAdapter("select count(kshifra) as SHNR from Klienti", Koneksioni)
    Dataseti.Clear()
    shnr.Clear()


    Adapteri.Fill(Dataseti, "Klienti")
    shnradapteri.Fill(shnr, "Klienti")


    'Koneksioni.Open()
    'Adap()
    Pamja.DataSource = Dataseti.Tables(0) 'kjo ehte emri i datagridit
    cmbnrsh.DataSource = shnr.Tables(0) 'eshte komboboxi per me mi tregu cili eshte ID apo numri i fundit te klientit
    cmbnrsh.DisplayMember = "shnr"
    ' Adap()
    Koneksioni.Close()

    Else

    Dim Adapteri As New OleDbDataAdapter("Select * from Klienti where KlientiID>" & KlID, Koneksioni)
    Dim shnradapteri As New OleDbDataAdapter("select count(kshifra) as SHNR from Klienti", Koneksioni)
    Dataseti.Clear()
    shnr.Clear()


    Adapteri.Fill(Dataseti, "Klienti")
    shnradapteri.Fill(shnr, "Klienti")


    'Koneksioni.Open()
    'Adap()
    Pamja.DataSource = Dataseti.Tables(0)
    cmbnrsh.DataSource = shnr.Tables(0)
    cmbnrsh.DisplayMember = "shnr"
    ' Adap()
    Koneksioni.Close()
    End If
    End Sub
    ------------------------
    a ka dikush ndonje kod shum me te shkurter sa sa ky por qeeshte i shpejt kerkoj vetem konektim ne database .mdb dhe nj buton per shtim te rekordeve
    se shume po me perzihet kur ka shum kode me duhet disi si me i mesu permensh

  2. #2
    i/e regjistruar
    Anėtarėsuar
    17-11-2009
    Postime
    3
    Siper do ta shkruaj >
    Imports System.Data.OleDb

    Pastaj ketu e ke kodin qe lidhesh me bazen edhe menyren si te shtojsh rekorde ne baze....


    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim rd As OleDbDataReader

    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Lokacioni i bazes.mdb"

    con.Open()
    cmd.Connection = con
    cmd.CommandText = "SQL Query per insert"
    rd = cmd.ExecuteReader
    rd.Close()
    con.Close()

    shpresoj tet ndihmoj

    megjithate ketu ke nje tutorial per tet ndrihumar te bejsh dicka te till...

    Kliko ketu
    Ndryshuar pėr herė tė fundit nga Leotrin : 10-05-2011 mė 15:53

  3. #3
    i/e regjistruar
    Anėtarėsuar
    16-04-2004
    Postime
    674
    Citim Postuar mė parė nga Leotrin Lexo Postimin
    Siper do ta shkruaj >
    Imports System.Data.OleDb

    Pastaj ketu e ke kodin qe lidhesh me bazen edhe menyren si te shtojsh rekorde ne baze....


    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim rd As OleDbDataReader

    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Lokacioni i bazes.mdb"

    con.Open()
    cmd.Connection = con
    cmd.CommandText = "SQL Query per insert"
    rd = cmd.ExecuteReader
    rd.Close()
    con.Close()
    Nuk ke nevoj per DataReader nese ke vetem ta besh insert/futjen e shenimeve, pasi qe command objekti (edhe sql edhe oledb) e ka metoden ExecuteNonQuery.

    Pra ne vend te

    rd = cmd.ExecuteReader

    mund te shkruash

    Dim i as integer = cmd.ExecuteNonQuery

    Vlera i do te jet numri i rreshtave te afektuar pra nese cdo gje eshte ne rregull do te jet 1 nese ka deshtu do te jet 0

  4. #4
    i/e regjistruar
    Anėtarėsuar
    11-10-2009
    Postime
    7
    Menyra ma e leht eshet qe ti besh StronglyTyped dataset
    Me Veglat qe i posedon prej ver. VB 2005ose edhe ma heret .
    Athere do krijosh Clasen per databasen me veglen Dataset Designer qe do ta emrosh ne baz te deshires, ku ajo do ta ket strukturen e njejt me databazen qe e ke kriju ne Acces.
    Pastaj do krijosh (ne menyr vizuale) DataAdapteret per cdo tabel, si dhe do konfigurosh komandat per Select,Inser,Update,Delete, si dhe eventualisht ndonje komand tjeter ne kuader te nje DataAdapteri(e ne rastin tend eshte FillCountOfKShifra ne kuader te AdapteritadapteriKlienti) .
    Kurse ne Aplikacionin tend duhet vetem ta marresh (me maus) datasetin dhe ta lshosh te forma qe don, dhe connection string ta configurosh.
    Ne vend se ta shenosh pastaj:
    Adapteri.Fill(Dataseti, "Klienti")
    shnradapteri.Fill(shnr, "Klienti")
    e shenon

    AdapteriKlienti.Fill(Dataseti.Klienti)
    AdapteriKlientiNr.Fill(dataseti.klientiNr)
    pastaj ne vend te:
    Pamja.DataSource = Dataseti.Tables(0)
    cmbnrsh.DataSource = shnr.Tables(0)
    cmbnrsh.DisplayMember = "shnr"
    e shenon
    Pamja.DataSource = Dataseti.klienti
    cmbnrsh.DataSource = Dataseti.klientiNr
    cmbnrsh.DisplayMember = Dataseti.klientinr.columns(0).name
    e shum tjera lehtesi
    kur don ta besh update i permison shenimet dhe pastaj therret:adapteri.update(dataseti.klienti.getchange s)
    e Įdapteri vet do ta aplikoj komanden gjejese mvarsisht se a eshte Update,insert apo delete

  5. #5
    i/e regjistruar
    Anėtarėsuar
    17-11-2009
    Postime
    3
    Citim Postuar mė parė nga Uke Topalli Lexo Postimin
    Nuk ke nevoj per DataReader nese ke vetem ta besh insert/futjen e shenimeve, pasi qe command objekti (edhe sql edhe oledb) e ka metoden ExecuteNonQuery.

    Pra ne vend te

    rd = cmd.ExecuteReader

    mund te shkruash

    Dim i as integer = cmd.ExecuteNonQuery

    Vlera i do te jet numri i rreshtave te afektuar pra nese cdo gje eshte ne rregull do te jet 1 nese ka deshtu do te jet 0
    E di se funksionon edhe ashtu, bile funksionon edhe edhe pa deklaruar variabel fare, thjesht ta shkruajsh

    cmd.ExecuteNonQuery

    por ashtu e kam perdor edhe e ke parasysh kur te fiksohet dicka edhe e ben gjithmon ashtu si e ke te fiksuar hehe...

    Gjithsesi flm per kritiken.

  6. #6
    i/e regjistruar
    Anėtarėsuar
    16-04-2004
    Postime
    674
    Citim Postuar mė parė nga Leotrin Lexo Postimin
    E di se funksionon edhe ashtu, bile funksionon edhe edhe pa deklaruar variabel fare, thjesht ta shkruajsh

    cmd.ExecuteNonQuery

    por ashtu e kam perdor edhe e ke parasysh kur te fiksohet dicka edhe e ben gjithmon ashtu si e ke te fiksuar hehe...

    Gjithsesi flm per kritiken.
    Nuk e kam pasur me qellim kritike, por thjeshte te prezentimit te alternatives me qellim te shpejtesimit te ekzekutimit. Perdorimin e variables, personalisht e preferoj per shkak te perdorimit si indikator per rezultatin e futjes se shenimeve, mirepo sic e ceke edhe ti vet, absolutisht nuk eshte e domosdoshme ...

    Te kuptoj plotesisht, sa i perket shablloneve ... edhe vet i kam krijuar disa shabllona/veti te cilat nganjehere nuk jane me adekuatet por ma pershpejton punen e kodimit

Regullat e Postimit

  • Ju nuk mund tė hapni tema tė reja.
  • Ju nuk mund tė postoni nė tema.
  • Ju nuk mund tė bashkėngjitni skedarė.
  • Ju nuk mund tė ndryshoni postimet tuaja.
  •