469,150 Members | 1,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,150 developers. It's quick & easy.

How to insert data to a SQL database Using VB

123 100+
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 connectionstring created and I am connected.

Thanks.
Jun 20 '07 #1
2 3512
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.Connection
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 = "DataBaseName"
sUser = "username"
sPWD = "password"

' opening connection
DBcon.ConnectionString = "Provider=sqloledb;" & _
"server=" & sServer & ";database=" & sDatabase & ";Integrated Security=SSPI" 'uid=" & sUser & ";pwd=" & sPWD & ";
DBcon.CursorLocation = adUseServer
DBcon.Open

'Setting up the three parameters for date, time and weights
objparameter1.Direction = adParamInput
objparameter1.Type = adInteger
objparameter1.Size = 3
objparameter1.Value = Text1
objcmd.Parameters.Append objparameter1

objparameter2.Direction = adParamInput
objparameter2.Type = adDBDate
objparameter2.Size = 3
objparameter2.Value = Date
objcmd.Parameters.Append objparameter2

objparameter3.Direction = adParamInput
objparameter3.Type = adDBTime
objparameter3.Size = 3
objparameter3.Value = Time
objcmd.Parameters.Append objparameter3

' Calling up the stored procedure and passing the params
objcmd.ActiveConnection = DBcon
objcmd.CommandType = adCmdStoredProc
objcmd.CommandText = "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(@objparameter1 datetime, @objparameter2 int, @objparameter3 float)
AS

INSERT into FloatTable1 (DateandTime,TagIndex, Val)
values(@objparameter1,@objparameter2, @objparameter3)
GO


I hope this helps!
Jun 20 '07 #2
Newbie19
123 100+
Thanks for the information, I'll look at it and try and develop a pathway for this project I'm doing.

Thanks.
Jun 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jan van Veldhuizen | last post: by
16 posts views Thread by Philip Boonzaaier | last post: by
1 post views Thread by Abareblue | last post: by
3 posts views Thread by Hai Nguyen | last post: by
reply views Thread by Eustice Scrubb | last post: by
7 posts views Thread by David Bear | last post: by
46 posts views Thread by dunleav1 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.