Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 47
VB.NET 2008 - Windows Protected Your PC 7 42
Sql server, import complete table, using vb.net 9 43
vb.net winforms sizing/resolution? 4 46
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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