Solved

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

Posted on 2006-07-16
3
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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