Solved

Using SQL to store values in a database in VB.NET 2005

Posted on 2006-07-16
3
223 Views
Last Modified: 2010-04-23
Hi, I would like to Store data from a form in a SQL database. I would like a method to do this without using databinding.

I have the connection string already

Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Payroll.mdf;Integrated Security=True;User Instance=True")

I would like a way to be able to say

database.additem
Name.text = NAME
etc
database.save

with NAME being a field in the database.

Thank you in advance for all your help!

0
Comment
Question by:OsirisJa
3 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 300 total points
ID: 17119765
Ok here is a sample how yhou can proceed
I have taken an eg. where you want to store Employee data
so Have a class like this


Public Class Database

    Dim m_EmpName As String
    Dim m_EmpID As String
    Dim m_EmpSalary As Integer

    Public Property EmpName() As String
        Get
            Return m_EmpName
        End Get
        Set(ByVal value As String)
            m_EmpName = value
        End Set
    End Property

    Public Property EmpID() As String
        Get
            Return m_EmpID
        End Get
        Set(ByVal value As String)
            m_EmpID = value
        End Set
    End Property

    Public Property EmpSalary() As Integer
        Get
            Return m_EmpSalary
        End Get
        Set(ByVal value As Integer)
            m_EmpSalary = value
        End Set
    End Property

    Public Sub Save()
        Dim Cn As New SqlConnection(YourConnectionString)
        Dim Qry As String = String.Format("Insert Into Emp (EmpID, EmpName, EmpSalary) values ('{0}','{1}',{2})", EmpID, EmpName, EmpSalary)
        Dim Cmd As New SqlCommand(Qry, Cn)
        Try
            Cn.Open()
            Cmd.ExecuteNonQuery()
        Catch ex As Exception
        Finally
            Cn.Close()
            Cn.Dispose()
            Cmd.Dispose()
        End Try
    End Sub
End Class

The class has 3 properties for the 3 database fields that you want to Update and a method called Save that will save the data to the Db

Now in order to use this class you need this code

Dim DB As New DataBase
DB.EmpID="E001"
DB.EmpName="Jack"
DB.EmpSalary=10000
DB.Save

0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 200 total points
ID: 17119809
0
 

Author Comment

by:OsirisJa
ID: 17120043
:) Thank you both. I have solved my current problem and learned a lot of new things!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question