Solved

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

Posted on 2006-07-16
3
203 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now