|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
DataBase Connectivity-VB + SQL
Can Somebody send me the coding database Connectivity of VB With SQL. Cz I m new to the VB.
|
|
#2
|
||||
|
||||
|
Hi,
You haven't provided a lot of information - exactly what version of VB are you using? VB6? You say SQL, I assume you mean SQL Server, what version? etc.. The more information you provide the better the response you will get. Heres a quick demonstration of using VB6 to set up an ADODB connection with a SQL Server DB. Note, you must first add a reference to the relevant library, go to Project -> References, scroll down to "Microsoft ActiveX Data Objects 2.x Library" (where x is the highest number in the list), put a tick in the box and click "OK". You can now declare and use an ADODB connection to your DB: Code:
'First declare variables to hold details of your connection and recordset - which will hold any records returned
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
'Establish a connection to your DB - you can find the correct connection string at www.connectionstrings.com
cn.Open "Provider=MSDataShape;DRIVER={SQL SERVER};Server=yourserver;Database=yourdatabase;UI D=youruser;PWD=yourpassword;"
'You can now query the database connection and store any records returned in your recordset
'Define string variable to hold SQL statement
Dim strSQL As String
'Construct SQL String
strSQL = "set dateformat dmy; select * from mytable order by myfield asc"
'Execute the SQL Statement
Set rs = cn.Execute(strSQL)
'You can now test to see if the sql statement returned rows, if it did you can loop through the recordset displaying the details
If rs.EOF <> True And rs.BOF <> True Then
While Not rs.EOF
'Print the contents of the first field of the current record
Debug.Print rs.Fields(0)
'Move to the next record
rs.MoveNext
Wend
Else
MsgBox "No records returned"
End If
'Dont forget to close your recordset/connection
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
This is just a basic demonstration, you will find tons of information on the Net to help you get started. If you get stuck please post a detailed description of your problem, along with any code you have, and we will try and point you in the right direction. |
|
#3
|
|||
|
|||
|
VB Version is 6.o & SQL Version is 2000.
Database Table- create table stuinfo(roll int,name varchar(10),marks int) select * from stuinfo Till Now, I have currently did as below- Private Sub Form_Load() Dim gcn As New ADODB.Connection gcn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia" Dim query As String query = "Insert into stuinfo values(" End Sub I want to just insert the records into the database. |
|
#4
|
||||
|
||||
|
Quote:
I'm afraid I dont understand what you are asking, are you getting an error? Possible problems you may face are: You are only defining the field "name" as up to ten characters, is this really enough to store a name? Secondly, at least one of your field names is a reserved word (name), you will need to surround them with square brackets so SQL Server will know to treat them as field names. Finally, you dont appear to be opening the connection, use the .Open command. Heres an example which you should be able to adapt. Code:
Private Sub Form_Load()
Dim gcn As New ADODB.Connection, query As String
gcn.Open = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
Dim intRoll, intMarks As Integer
Dim strName As String
intRoll = 12
intMarks = 80
strName = "Sync_or_Swim"
query = "Insert into stuinfo([roll], [name], [marks]) " & _
"values(" & intRoll & ", '" & strName & "', " & intMarks & ")"
gcn.Execute(query)
gcn.Close
Set gcn = Nothing
End Sub
|
|
#5
|
|||
|
|||
|
Private Sub Form_Load()
Dim gcn As New ADODB.Connection gcn.Open = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia" Dim query As String query = "Insert into stuinfo values([roll],[name],[marks]), &_" values("& me.txtroll.Text &","& me.txtname.Text &","&me.txtmarks.Text &") gcn.Execute (query) MsgBox "Record Inserted" gcn.Close End Sub I have tried this code,but errors are there,Plz help to find out where are they?? |
|
#6
|
||||
|
||||
|
Quote:
What errors are there???? The more information you give me the more I can help!!! There are several factors that might cause errors, it may be that there is a problem accessing the database, or it could be that you have a syntax error in your SQL statement. At a glance, it appears that you have mis-typed the code I posted, you have to be careful that you write things in the correct order. Here is the code you posted: Code:
Private Sub Form_Load()
Dim gcn As New ADODB.Connection
gcn.Open = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
Dim query As String
query = "Insert into stuinfo values([roll],[name],[marks]), &_"
values("& me.txtroll.Text &","& me.txtname.Text &","&me.txtmarks.Text &")
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub
If you focus on the sql query: your continuation character on the end of the first line should be outside of the quotes, and there should be a double quote on the end of the string. Also, the column definitions should be before the values. Try copying this VERBATIM: Code:
Private Sub Form_Load()
Dim gcn As New ADODB.Connection, query As String
gcn.Open = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
query = "Insert into stuinfo ([roll],[name],[marks]) " & _
"values("& me.txtroll.Text & ", " & me.txtname.Text & ", " & me.txtmarks.Text & " )"
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub
|
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > DataBase Connectivity-VB + SQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
![]() |
|