Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Simple CODE

  1. #1

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Resolved [RESOLVED] [2005] Simple CODE

    I am current still learning in database, and i figure some of this hard to code.
    This is how i add a record in a access using VB6.0
    Code:
    Call Opening
    ObjRS.Open "Contact", ObjConn, adLockOptimistic, adOpenStatic, adCmdTable
    ObjRs.AddNew
    ObjRs.Fileds("Sname") = txtName.Text
    ObjRs.Fields("Number") = txtNumber.Text
    ObjRs.Fields("Address") = txtAddress.Text
    ObjRs.Update
    MsgBox ("Record Added")
    Call Closing
    its very simple but when im using VB.Net using this code

    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.Windows.Forms.Form
    Imports System.Data.OleDb
    
    Public Class CreateAccount
        Private contactID As Int64 = 0
    
        Public Property Contact() As Int64 '
            Get
                Return contactID
            End Get
            Set(ByVal value As Int64)
                contactID = value
            End Set
        End Property
    
        Public Sub RetriveRecords()
            Dim ObjConn As New OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\DBase\Bank.mdb;")
            Dim dAdapter As New OleDbDataAdapter("SELECT * FROM UserDatabase", ObjConn)
            Dim dTable As New DataTable
    
            dAdapter.Fill(dTable)
            Database.AccessDatabase.DataSource = dTable
    
        End Sub
    
        Private Sub btnSave_CLick(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
            Dim ObjConn As New OleDbConnection("Provider = Microsoft.Jet.OleDb.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\DBase\Bank.mdb;")
            Dim sql As String = String.Empty
    
            If contactID = 0 Then
                sql = "INSERT INTO UserDatabase(Ename,Address,ContactNumber,AmountPaid,DatePaid,PeriodTime)" _
                & "VALUES('" & txtEname.Text & "','" & txtAddress.Text & "','" & txtContact.Text & "','" & txtAmount.Text & "','" & txtDate.Text & "','" & cboPeriod.Text & "')"
            End If
    
            ObjConn.Open()
            Dim dCMd As New OleDbCommand(sql, ObjConn)
            dCMd.ExecuteNonQuery()
            ObjConn.Close()
            MessageBox.Show("Record Added")
    I use the Public Property to determine what im going to do. Is there any simple code reather than that. As simple in VB6.0

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple CODE

    Thanks for providing that VB6 code, although it's completely useless because this is VB.NET and you're using ADO.NET.

    http://www.vbforums.com/showthread.php?t=469872
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: [2005] Simple CODE

    Thank you for the link, but is it more appropriate to use the SQL namespace instead using the oledb namespace ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple CODE

    Read that thread properly and you'll have your answer. It's in the very first paragraph.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Simple CODE

    it depends oon the kind of database you want to use. if Access best suits your need, you use the oledb otherwise, you go for sql. jmc used sql because that what he was using for his example.

  6. #6

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: [2005] Simple CODE

    Yah your right i get the answer, if i use the SQL namespace i can write less code, all the SQL statements are added in a Dataset, and im going to do is just call the methods.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple CODE

    Quote Originally Posted by Loraine
    Yah your right i get the answer, if i use the SQL namespace i can write less code, all the SQL statements are added in a Dataset, and im going to do is just call the methods.
    No, that's not it.
    Quote Originally Posted by jmcilhinney
    This code uses members of the System.Data.SqlClient namespace. If you're not using SQL Server then it's a simple matter of switching to the corresponding types of the appropriate namespace for your data source.
    SqlClient is, as its documentation says, for use with SQL Server. If you're using an OLEDB provider, like Jet with Access, then you use OleDb.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: [2005] Simple CODE

    Yah i know that, im just saying that if i use the SQLClient i can write less code and just call all the methods of SQL statements that are stored in a Dataset.

  9. #9
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Simple CODE

    that will be exactly the same if you use oledb and dataset. You will ahve to write the same amount of code

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple CODE

    Quote Originally Posted by Loraine
    Yah i know that, im just saying that if i use the SQLClient i can write less code and just call all the methods of SQL statements that are stored in a Dataset.
    No, that doesn't have anything to do with anything I'm afraid. In VS 2005 you can create a typed DataSet with accompnying TableAdapters for basically any data source. If you do that you will be writing less code and you will not be using SqlClient, OleDb or any other provider directly. The IDE will generate most of the code for you, but under the hood it will still use SqlClient for SQL Server and OleDb for Access.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: [2005] Simple CODE

    Thank you jmcillhiney and talkro, well im translating the code of jmc use in SQLClient into an oledb.

  12. #12

    Thread Starter
    Fanatic Member Loraine's Avatar
    Join Date
    Aug 2006
    Location
    8ft. underground
    Posts
    581

    Re: [2005] Simple CODE

    Before im going to Resolved this thread, im trying to use the ManageData but i cant find it in a library ?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Simple CODE

    Quote Originally Posted by Loraine
    Before im going to Resolved this thread, im trying to use the ManageData but i cant find it in a library ?
    I have no idea what you mean. Is this related to the previous question?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width