473,573 Members | 2,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert data to a SQL database Using VB

123 New Member
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.

Jun 20 '07 #1
2 3718
3 New Member
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

'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


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)

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

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

Jun 20 '07 #3

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

Similar topics

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 atmosphere. Good food.", " (Harry Houdini - 03/01/2004)"); INSERT INTO `reviews` VALUES("", "Le Chow Place", "Lunch", "yada yada", " (Herbert Hoover -...
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 VB source is: Dim mConn As New ADODB.Connection Dim rs As New ADODB.Recordset
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 must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then...
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 one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema:...
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;
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 checked, and the insert works fine (tried to use it from access)... im using visual C# express 2k5... what could be wrong? thanks!
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 3 b 1 4 c 1 5 d
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 Server myConnection = New SqlConnection("Data Source=SERVER;" _ & "Initial Catalog=database;User Id=user;Password=password;")
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 way of expressing this, but I have a problem with the way lists are represented when converted to strings. Lets say my dictionary is
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 (raid 1/0 - 15k disks) but DB2 I/O seems to be significantly slower. Tablespaces are SMS with automatic prefetch set. My thought was to create the...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.