Classic ASP adding records to MySQL - mysql

I have had to move on of my old classic ASP projects to a new host, and I'm having problems connecting to their MySQL server.
I have attached below the script I used with the old host which now errors
Data source name not found and no default driver specified
After a bit of digging it seems I have to change the driver to {MySQL ODBC 5.3 Unicode Driver} but it still errors. It seems to point to the cursor/lock type but I have used all option with no success.
ODBC driver does not support the requested properties.
<%
Dim Conn
Dim Rs
Dim sql
Set Conn = Server.CreateObject("ADODB.Connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=xxx; PORT=xxx; DATABASE=xxx; UID=xxx; PASSWORD=xxx; OPTION=3"
sql= "SELECT * FROM table;"
Rs.CursorType = 2
Rs.LockType = 3
Rs.Open sql, Conn
Rs.AddNew
Rs.Fields("database") = Request.Form("form")
Rs.Update
Rs.Close
Set Rs = Nothing
Set Conn = Nothing
%>

You don't need this to insert a record. Instead, use plain SQL which should be supported by all database drivers:
Dim oCommand
Const adInteger = 3
Const adDate = 7
Const adVarChar = 200
sql = "Insert Into table (database) Values (?)"
Set oCommand = Server.Createobject("ADODB.Command")
Set oCommand.ActiveConnection = Conn
oCommand.CommandText = sql
oCommand.Parameters.Append(oCommand.CreateParameter("database", adVarChar, , 512, Request.Form("form")) )
oCommand.Execute
This is indeed bit more to write, but should preserve all the benefits of the other way (e.g. SQL Injections attack protection) and not being dependant on specific drivers.

Related

create linked tables with vba through odbc connection (MySql) in excel

I have an excel file, which needs to have a connection to a mysql database with odbc. I am aware of the option to do it through UI, but i would prefer not to (data - get_data - from other sources - from odbc). I have made it in access, the linked tables are the same as the ones that i can create through the UI. In excel, i have not found a solution for this.
For now, i have some weird code that establishes the connection just fine, but creates weird tables in excel when trying to get the data from the database (numbers being converted to dates, some data not even loading, ...).
Here is the code that i have for now in excel:
Function connect2mssql()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Set con = New ADODB.connection
Dim SQL As String
con.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=192.168.12.192;DATABASE=rezervni_deli;UID=vzdrzevalec;PWD=unichem"
''####################
SQL = "SELECT * FROM rezervni_deli;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("rezervni_deli").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("rezervni_deli").Range("A2").CopyFromRecordset rs
''####################
SQL = "SELECT * FROM p_linija;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("p_linija").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("p_linija").Range("A2").CopyFromRecordset rs
''####################
rs.Close
Set rs = Nothing
End Function

How to Connect to an Access Database Using VBScript

I have an Access database for a client who wants to connect to and query the database using vbscript (so they can automate without actually opening the Access 2000 MDB). I can't figure out how to make the database connection.
I've tried several scripts, using both DAO and OLEDB. Below I've pasted the closest I've got, using an ODBC File DSN (I'm afraid using a System DSN would require extra work on the client's end, I'm trying to keep it simple).
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
'ERROR OCCURS HERE
objConnection.Open "FileDSN=D:\RLS.dsn;"
objRecordset.CursorLocation = adUseClient
objRecordset.Open "SELECT County FROM CountyTBL" , objConnection, adOpenStatic, adLockOptimistic
Here is the contents of RLS.dsn (I created this using Windows Control Panel so I am confident it's correct):
[ODBC]
DRIVER=Microsoft Access Driver (*.mdb)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=8
MaxBufferSize=2048
FIL=MS Access
DriverId=25
DefaultDir=D:\
DBQ=D:\RLS_be.mdb
The error message I got (and this was similar with the other 2 scripts I tried as well) was:
"Line 5, Char 4 Error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified. Source: Microsoft OLE DB Provider for ODBC Drivers"
You can simply use ADO to connect to the file without setting up a DSN. This will be simpler for your client.
For Access 2000, 2002-2003 MDB, use the following connection string:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\RLS_be.mdb"
For Access 2007, 2010, 2013 ACCDB:
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\RLS_be.accdb"
The overall connection code:
' Build connection string
Dim sConnectionString
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\RLS_be.mdb"
' Create connection object
Dim objConnection
Set objConnection = CreateObject("ADODB.Connection")
' Open Connection
objConnection.open sConnectionString
' Get recordset from SQL query
Dim objRecordset
Dim sQuery
sQuery = "SELECT County FROM CountyTBL"
Set objRecordset = CreateObject("ADODB.Recordset")
objRecordset.CursorLocation = adUseClient
objRecordset.Open sQuery, objConnection, adOpenStatic, adLockOptimistic
Here is another version using the connection string for a MS-Access 2016 database. The example connects to the 'clients.accdb' database and retrieves the value of the 'ClientID' field of the record where the 'ClientName' is equal to 'Joe Smith', then it loops through the records returned by the SQL statement.
Note that the extra quotations in the SQL statement are used in order to handle the scenario in which the name might contain a single quot ('), for example O'Connor.
Dim oConn, oRS
Dim ClientName : ClientName = "Joe Smith"
Set oConn = WSH.CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.ACE.OLEDB.16.0; Data Source=C:\test\clients.accdb"
Set oRS = oConn.Execute("Select ClientID From Clients Where ClientName=""" & ClientName & """")
Do While Not(oRS.EOF)
'Do something with the record.
oRS.MoveNext
Loop
oRS.Close
oConn.Close

MySQL query error (ODBC 3.51)

I'm trying to execute query in a VB6 app.
Here is my code:
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.ConnectionString = "Driver={MySQL ODBC 3.51 Driver}; Server=***; Database=***; Username=***; Password=***; Option=3"
con.Open
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = con
.CommandText = "SELECT COD_CONFIG FROM FDT_CONFIG"
.CommandType = adCmdText
End With
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockOptimistic
I've hidden some information in the connection string but in my app I'm using the correct values.
I can successfully open the connection, but when I try to execute the query I get:
"Execution error '2147467259 (800004005)' : unspecified error"
Can someone tell me where I'm going wrong?
If you want to assign a Command object's ActiveConnection to an existing Connection object, you must use Set:
With cmd
Set .ActiveConnection = con
....
End With
It's a little confusing because you can also assign a string to the ActiveConnection property and ADO will create an ad-hoc connection for you. In that case, you wouldn't use Set because you're just assigning a value to an intrinsic type (string):
With cmd
.ActiveConnection = "Driver={MySQL ODBC 3.51 Driver}; Server=***; Database=***; Username=***; Password=***; Option=3"
...
End With
So the property can be used multiple ways. In your scenario, however, since you're assigning an object reference, you'll need to use the Set keyword.

MySQL Sample for Visual Basic 6.0 - read/write

I'd like to find a simple example of working with remote MySQL base. I know, there are some tutorial over the internet, explaining how to set up ADODB.Connection and connectionstrings, but I couldnt make it work. Thanks for any help!
Download the ODBC connector from the MySQL download page.
Look for the right connectionstring over here.
In your VB6 project select the reference to Microsoft ActiveX Data Objects 2.8 Library. It's possible that you have a 6.0 library too if you have Windows Vista or Windows 7. If you want your program to run on Windows XP clients too than your better off with the 2.8 library. If you have Windows 7 with SP 1 than your program will never run on any other system with lower specs due to a compatibility bug in SP1. You can read more about this bug in KB2517589.
This code should give you enough information to get started with the ODBC connector.
Private Sub RunQuery()
Dim DBCon As adodb.connection
Dim Cmd As adodb.Command
Dim Rs As adodb.recordset
Dim strName As String
'Create a connection to the database
Set DBCon = New adodb.connection
DBCon.CursorLocation = adUseClient
'This is a connectionstring to a local MySQL server
DBCon.Open "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;"
'Create a new command that will execute the query
Set Cmd = New adodb.Command
Cmd.ActiveConnection = DBCon
Cmd.CommandType = adCmdText
'This is your actual MySQL query
Cmd.CommandText = "SELECT Name from Customer WHERE ID = 1"
'Executes the query-command and puts the result into Rs (recordset)
Set Rs = Cmd.Execute
'Loop through the results of your recordset until there are no more records
Do While Not Rs.eof
'Put the value of field 'Name' into string variable 'Name'
strName = Rs("Name")
'Move to the next record in your resultset
Rs.MoveNext
Loop
'Close your database connection
DBCon.Close
'Delete all references
Set Rs = Nothing
Set Cmd = Nothing
Set DBCon = Nothing
End Sub

How to MySQL query with Visual Basic 6

two things:
first: I have googled everywhere, including stackoverflow. Just about questions regarding sql vs vb6 are about connection string. I have this down pat. Any reference to mysql queries are for the query itself - but not in tangent with vb6 or any language
second: I am very proficient in PHP/MySQL so that aspect of help I am not seeking.
what I am stuck on, is how vb6 handles sql queries a little (lot) different than php. So once I get connected, how to I tell vb6 to look up a field.
php version
$sql = "SELECT * FROM table field = data where something = that";
$query = mysql_query($sql) or die("bad query: <br>$sql<br>".mysql_error());
then either use a fetch array or work with this.
how is this accomplished in vb6?
I saw some source referring to rdoQry. Can someone shed some light on this with example code? I don ont need the connection part. have that set.
my connection is this:
Dim cnMySql As New rdoConnection
cnMySql.CursorDriver = rdUseOdbc
cnMySql.Connect = "uid=root;pwd=root;" _
& "server=127.0.0.1;" _
& "driver={MySQL ODBC 3.51 Driver};" _
& "database=mydatabase;dsn=;"
cnMySql.EstablishConnection
works perfect.
ADO is the successor to RDO. I use code similar to this to query MySQL from Visual Basic using ADO.
Dim conn As New ADODB.Connection
conn.Open "connection string"
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = conConnection
.CommandText = "SELECT fields FROM table WHERE condition = ?"
.CommandType = adCmdText
End With
Dim param As New ADODB.Parameter
Set param = cmd.CreateParameter("condition", adVarChar, adParamInput, 5, "value")
cmd.Parameters.Append p
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockOptimistic
Dim temp
Do While Not rs.EOF
temp = rs("field")
rs.MoveNext
Loop
rs.Close
conn.Close
Typically, with VB6 (gosh, are people still using this??) you would connect to a database using ADO. ADO is a common database class which allows you to use the same syntax for any database.
The connection code you've provided is using RDO, which is a predecessor to ADO (and since VB6/ADO is pretty old now, that means RDO is historic). For your purposes, the two should work fairly similarly, but I'd suggest switching to ADO now if you have a chance, before you've written too much code.
This thread seems to be pointing someone else in the right direction in writing the connection code: http://www.vbforums.com/showthread.php?t=654819
Once you've got a connection, you need to run your queries. The process for this should make sense if you're used to querying from PHP; it's basically the same process, although you typically need to mess around with configuring a few more options than with PHP. It would look something like this:
Set rs = New ADODB.Recordset
rs.ActiveConnection = adoconn
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.Open "SELECT blah blah blah"
While Not rs.EOF
Text1.Text = rs("Name")
rs.MoveNext
Wend
rs.Close
Hope that helps.