473,394 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 3714
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

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
2
by: Jan van Veldhuizen | last post by:
I try to use the Server Data on Insert property to return a column value swhich is set in a trigger. But it does not work. Why not? What am I doing wrong? (BTW I'm using Oracle version 10) My...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
1
by: Abareblue | last post by:
I have no clue on how to insert a record into access. here is the whole thing using System; using System.Drawing; using System.Collections; using System.ComponentModel;
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
3
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 ...
0
by: Eustice Scrubb | last post by:
In line coding problem. Here's my code: <script language="VB" runat="server"> Dim myConnection As SqlConnection Sub Page_Load(Src As Object, e As EventArgs) ' Create a connection to the SQL...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
46
by: dunleav1 | last post by:
I have a process that does inserts that runs 50% slower that Oracle and Mssql. Queries normally take 50% slower than normal. DB2, Oracle, Mssql all are configured on same os, same disk array...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.