How to insert data to a SQL database Using VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie19
    New Member
    • Jun 2007
    • 122

    #1

    How to insert data to a SQL database Using VB

    I am creating a project that would insert data into a SQL database when the User placed the information into the VB application. Then at a click of a button, it would add this data into the database.

    I am wondering what is the exact or direction of the VB code to insert the data into the database.

    I have the connectionstrin g created and I am connected.

    Thanks.
  • ou812vh
    New Member
    • Oct 2006
    • 3

    #2
    I'm pretty new to this as well. But I did a program a few months ago and I did it like this. The procedure below collects the data and calls a stored procedure in SQL to put the data where I wanted it.


    ' Declaring variables for connection string params
    Dim sServer As String
    Dim sUser As String
    Dim sPWD As String
    Dim sDatabase As String

    ' Declaring variables
    Dim DBcon As New ADODB.Connectio n
    Dim objcmd As New ADODB.Command
    Dim objparameter1 As New ADODB.Parameter
    Dim objparameter2 As New ADODB.Parameter
    Dim objparameter3 As New ADODB.Parameter
    Dim objrs As New ADODB.Recordset

    sServer = "ServerName " ' setting up var with data
    sDatabase = "DataBaseNa me"
    sUser = "username"
    sPWD = "password"

    ' opening connection
    DBcon.Connectio nString = "Provider=sqlol edb;" & _
    "server=" & sServer & ";database= " & sDatabase & ";Integrate d Security=SSPI" 'uid=" & sUser & ";pwd=" & sPWD & ";
    DBcon.CursorLoc ation = adUseServer
    DBcon.Open

    'Setting up the three parameters for date, time and weights
    objparameter1.D irection = adParamInput
    objparameter1.T ype = adInteger
    objparameter1.S ize = 3
    objparameter1.V alue = Text1
    objcmd.Paramete rs.Append objparameter1

    objparameter2.D irection = adParamInput
    objparameter2.T ype = adDBDate
    objparameter2.S ize = 3
    objparameter2.V alue = Date
    objcmd.Paramete rs.Append objparameter2

    objparameter3.D irection = adParamInput
    objparameter3.T ype = adDBTime
    objparameter3.S ize = 3
    objparameter3.V alue = Time
    objcmd.Paramete rs.Append objparameter3

    ' Calling up the stored procedure and passing the params
    objcmd.ActiveCo nnection = DBcon
    objcmd.CommandT ype = adCmdStoredProc
    objcmd.CommandT ext = "sp_Line1"
    Set objrs = objcmd.Execute

    DBcon.Close



    Stored procedure in SQL looked like this and the SP name is "sp_Line1"

    CREATE PROCEDURE sp_Line1(@objpa rameter1 datetime, @objparameter2 int, @objparameter3 float)
    AS

    INSERT into FloatTable1 (DateandTime,Ta gIndex, Val)
    values(@objpara meter1,@objpara meter2, @objparameter3)
    GO


    I hope this helps!

    Comment

    • Newbie19
      New Member
      • Jun 2007
      • 122

      #3
      Thanks for the information, I'll look at it and try and develop a pathway for this project I'm doing.

      Thanks.

      Comment

      Working...