MySQL and ADO - hopeless? - mysql

Tearing hair out and googling all weekend. I am frantically converting an ASP site to use MySQL. Am I correct in thinking that I would be barking up the wrong tree to try to use recordset commands to get data in and out of MySQL via ODBC and should only use MySQL procedures? If so I face a nightmare, I think as have then to work out how to capture the returned values.
Sorry if this sounds hideously ignorant, but no idea where to turn.
DETAILS:
Set SConn = createobject("ADODB.Connection")
conn.open = "DRIVER={MySQL ODBC 5.1 Driver};"_
& "SERVER=" & SQLServer & ";"_
& "DATABASE=" & SQLDbase & ";"_
& "UID=" & SQLUser & ";PWD=" & SQLPW & "; OPTION=35;"
SQL = "SELECT tblNodes.SingleSiteChildID "
SQL = SQL & "FROM tblNodes "
SQL = SQL & "WHERE (((tblNodes.NodeID)=" & m_lngNodeID & "));"
set RS=server.CreateObject("adodb.Recordset")
RS.CursorType = 1
RS.LockType = 2
RS.Open SQL, conn
With RS
If not .Fields("SingleSiteChildID") >0 or isnull(.Fields("SingleSiteChildID")) then
If m_lngChildCount>0 then
.Fields("SingleSiteChildID")=0
m_lngSingleSiteChildID=0
Else
.Fields("SingleSiteChildID")=null
m_lngSingleSiteChildID=null
End if
.UPDATE
End if
END WITH
RS.Close
Set RS = Nothing
Set conn = Nothing
End Sub

You can use Recordset and Command objects in exactly the same way you are use other databases at the moment.
If you use DSNs, all you need to do is create a system DSN to the MySQL DB, although a DSN-less connection is a better way of doing it.
Head to the MySQL site to install the ODBC drivers then your connection string will look something like this:
Driver={MySQL ODBC 5.1 Driver}; Server=localhost; uid=user_name; pwd=password; database=db_name; option=67108899; port=3306;
Do a quick Google to check that the options specified there are correct for you, I have ripped this from a working Classic ASP site that uses MySQL.
Simply switching the connection string should allow you to migrate very simply to a new DB type. Do check that all your SQL commands conform to MySQL standards, and do not use words specific to MSSQL or Access, e.g. TOP is MS only, use LIMIT for MySQL.

Related

ODBC connection string to Snowflake for Access Pass Thru Query

I am trying to create a connection string to get to Snowflake data from Access 2010 and above. I can create a database DSN and link to the tables I use, but I need to build DSN-less connection strings for distributed applications. Here's what I have so far, it fails with the message "ODBC connection to xxxx failed". Here's what I have so far:
ODBC;Driver={SnowflakeDSIIDriver}; Server=https://server name; Role=role name;Warehouse=warehouse name;Database=db name;Schema=schema name;UID=snowflake ID; PWD=snowflake password;
I think you are on the right track. I have the same thing and it works.
ODBC;
driver={SnowflakeDSIIDriver};
server=accountname.snowflakecomputing.com;database=dbname;
schema=public;
warehouse=whname;
role=rlname;
Uid=userid;
Pwd=password;
Very odd that the DSN one works and your doesn't.
I can confirm that DNS-free connections work fine in Access 2013. I have not tested on Access 2010, but I have it available if that needs testing.
The first problem I encountered is that the Snowflake ODBC driver reports 32/64-bit in the ODBC section of Control Panel, but it may not have one or the other installed.
In my case, it showed in the DSN sources as 32/64-bit, but I had only the 64-bit version installed. Notice that after installing the 32-bit driver, the Programs and Features (where to go normally for uninstalling apps) shows both the 64 and 32 bit drivers.
After installing the 32-bit driver, it was just a matter of getting the connection string right. You want to copy it from the URL on your Snowflake web UI. Strip off the https:// part, and then keep everything up to and including the snowflakecomputing.com in the url. That's what you'll use for the server.
Edit 2: I missed the part of the question that referenced pass through queries and was describing a procedure I tested recently for DNS-free connection using VBA. I tested the pass-through connection and it worked fine. The only difference is in the ODBC connection string you need to keep the "ODBC;" prefix:
ODBC;Driver{SnowflakeDSIIDriver};server=<your_URL_everything_before_snowflakecomputing.com>.snowflakecomputing.com;uid=greg;pwd=xxxxxx
Edit: One thing I forgot and am adding... The built-in Access data engine did not work for me to connect with a DNS-free connection. The code shows that it's using ActiveX Data Objects (ADO). You need to add a reference to that in your VBA project:
' For the account, use everything after https:// up to and including
' snowflakecomputing.com in your URL when connecting to Snowflake using the web UI.
Const SNOWFLAKE_ACCOUNT = "<your_account>.<your_region>.snowflakecomputing.com"
Const SNOWFLAKE_USER = "greg"
Const SNOWFLAKE_PASSWORD = "xxxxx"
Public Sub Main()
Dim odbc As String
Dim sfCon As ADODB.Connection
Set sfCon = OpenDatabaseConnection(GetConnectionString())
If Not sfCon Is Nothing Then
'Use the connection here...
sfCon.Close
End If
End Sub
Private Function GetConnectionString()
GetConnectionString = "Driver={SnowflakeDSIIDriver}" + _
";server=" + SNOWFLAKE_ACCOUNT + _
";uid=" + SNOWFLAKE_USER + _
";pwd=" + SNOWFLAKE_PASSWORD + _
";network_timeout=60" + _
"login_timeout=60"
End Function
Public Function OpenDatabaseConnection(ConnString As String) As ADODB.Connection
On Error GoTo Handler
Dim database As ADODB.Connection
Set database = New ADODB.Connection
With database
.ConnectionString = ConnString
.ConnectionTimeout = 60
.Open
End With
Set OpenDatabaseConnection = database
Exit Function
Handler:
MsgBox "Error: " + Err.Description
End Function

Classic ASP global.asa SQL Server 2008 connection string

I have been given a web application written in Classic ASP to port from Windows 2003 Server (SQL Server 2000 and IIS 6) to Windows 2008 Server (SQL Server 2008 and IIS 7.5).
The site uses a GLOBAL.ASA file to define global variables, one of which is the connection string (cnn) to connect to SQL Server.
Below is the (old) connection string from GLOBAL.ASA:
Sub Application_OnStart
Dim cnnDem, cnnString
Set cnnDem = Server.CreateObject("ADODB.Connection")
cnnDem.CommandTimeout = 60
cnnDem.Mode = admodeshareexclusive
cnnString = "Provider=SQLOLEDB; Data Source=192.xxx.x.xx; User Id=xxxx; Password=xxxxx; default catalog=xxxxxxx;"
Application("conString")=cnnString
Call cnnDem.Open(cnnString)
Application("cnn") = cnnDem
End Sub
The .ASP pages then use the cnn value like this:
strSQL = "Select * From tblUtilities order by companyname"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, Application("cnn"), adOpenKeyset
However I could not get the connection string to connect – I whittled it down to a “Failed to Login” error message (no matter what Login ID I tried).
I edited the GLOBAL.ASA file as follows and it works.
Sub Application_OnStart
Dim cnnDem, cnnString
Set cnnDem = Server.CreateObject("ADODB.Connection")
cnnDem.CommandTimeout = 60
cnnString = "Provider=SQLNCLI10.1;User Id=xxxx; Password=xxxxx;Initial Catalog=xxxxxxx;Data Source=xxxxxx\SQLEXPRESS;"
Application("conString")=cnnString
Application("cnn")=cnnString
Call cnnDem.Open(cnnString)
End Sub
The main difference is that cnn now contains the connection string, where as previously cnn was an object referring to ADOBD.Connection.
The question I have is what impact (if any) will this have on the application. I have done some basic (local) testing and everything looks ok at the moment. But I am wondering if there might be multi-user issues (or something of that nature) that might arise when this site is deployed again.
One of the best and easiest way to connect to create a Database Connection String is to crease a new ASP file in the root directory or elsewhere and include the Connection string into it:
//Global.asp //
<%
Dim connectionString
connectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=YourSQLServer;UID=sa;PWD=*******;DATABASE=YourDataBase"
%>
Then create an include statement in each file that you would like to call this connection.
<!-- #include virtual="global.asp" -->
Then, where you need to setup your connection call, simply use your code to connect to the Database:
<%
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open = ConnectionString
Set rsReports = Server.CreateObject("ADODB.Recordset")
strSQL = "Select * From Customers"
rsReports.Open strSQL, adoCon
%>
I keep the Connection String in Global.asa but create the connection in a separate function loaded as needed. An Application connection object may not be aware of temporary network issues that may close that connection, and then future attempts to use the connection will not be successful.
Hope this makes sense.

Trouble resetting connection string in Access 2010

New to Access 2010. The following VBA code when run doesn't reset the connection string as expected. I'm pretty sure this used to work. What's wrong?
CurrentDb.TableDefs("AccountNumber").Connect = "ODBC;Description=NativeClient;DRIVER=SQL Native Client;SERVER=server1;DATABASE=Expense;Trusted_Connection=Yes"
CurrentDb.TableDefs("AccountNumber").RefreshLink
I am not sure about that connection string. Which version of SQL Server are you using? You might like to try a connection string from http://www.connectionstrings.com/sql-server-2008#p3, for example, this works for me.
scn = "Driver={SQL Server Native Client 10.0};" & _
"Server=ServerName; Database=test;Trusted_Connection=yes;"
With CurrentDb
.TableDefs(sLocalName).Connect = scn
.TableDefs(sLocalName).RefreshLink
End with

Visual Basic 2005 + mysql

How do I connect to MySQL and get started with VB 2005?
Been using PHP + MySQL in work for bout a year now. Just wondering how to get started with Visual Basic 2005 + MySQL. Just to connect to a database, and run a simple select star query?
db -> db_name
table -> tbl_name
ip -> localhost
You can use the ADO.NET Driver for MySQL (Connector/NET), which you can download here:
http://www.mysql.com/products/connector/
After installing, you can use MySQL in the standard .NET way, using MySqlConnection, MySqlCommand, MySqlDataReader, and so on. The documentation is here:
http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming.html
Some example code:
Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection
myConnection.ConnectionString = "server=127.0.0.1;" _
& "uid=root;" _
& "pwd=12345;" _
& "database=test;"
myConnection.Open()
Dim myCommand As New MySqlCommand("select * from TheTable", myConnection)
Using myReader As MySqlDataReader = myCommand.ExecuteReader()
While myReader.Read()
Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
End While
End Using
myConnection.Close()
The Using statement makes sure the data reader is closed when you no longer need it, even if an exception is thrown. You could also enclose the connection in a Using statement.

vbscript to export an access query to a tab delimited file not working

I have this code:
db = "C:\Dokumente und Einstellungen\hom\Anwendungsdaten\BayWotch4\Neuer Ordner\baywotch.db5"
TextExportFile = "C:\Dokumente und Einstellungen\hom\Anwendungsdaten\BayWotch4\Neuer Ordner\Exp.txt"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source =" & db
strSQL = "SELECT * FROM tblAuction1"
rs.Open strSQL, cn, 3, 3
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(TextExportFile, True)
a = rs.GetString
f.WriteLine a
f.Close
Which is meant to connect to an access database and produce a tab delimited text file. tblAuction1 is a query in the database, and definitly exists and is not misspelt in any way, but I get an error that it cannot be found or does not exist. When I change it to tblAuction which is the name of the table, I get an error stating f.WriteLine a has been called incorrectly.
edit: I now only get a problem with f.writeline a, saying an incorrect argument has been supplied. I no longer have a problem with tblAuction1
edit: the sql code used for my query:
SELECT tblAuction.article_no, tblAuction.article_name, tblAuction.subtitle, tblAuction.current_bid, tblAuction.start_price, tblAuction.bid_count, tblAuction.quant_total, tblAuction.quant_sold, tblAuction.start, tblAuction.ends, tblAuction.origin_end, tblUser.user_name, tblAuction.best_bidder_id, tblAuction.finished, tblAuction.watch, tblAuction.buyitnow_price, tblAuction.pic_url, tblAuction.private_auction, tblAuction.auction_type, tblAuction.insert_date, tblAuction.update_date, tblAuction.cat_1_id, tblAuction.cat_2_id, tblAuction.article_desc, tblAuction.countrycode, tblAuction.location, tblAuction.condition, tblAuction.revised, tblAuction.paypal_accept, tblAuction.pre_terminated, tblAuction.shipping_to, tblAuction.fee_insertion, tblAuction.fee_final, tblAuction.fee_listing, tblAuction.pic_xxl, tblAuction.pic_diashow, tblAuction.pic_count, tblAuction.item_site_id
FROM tblUser INNER JOIN tblAuction ON tblUser.id = tblAuction.seller_id;
I have tried to reproduce this on several databases and machines, I can't get your code to fail.
Leaves :
a corrupt database, could you please run repair and try again ?
Fields in your database that are throwing of the query, I have tried several possibilities but can't find anything that brakes your code. To exclude other things you could try to create a new table and see if your code works on that table.
something wrong with your dll's , could you try it on another machine.
Answer (to see how we came to the answer see the comments)
There are unicode characters in your database that writeline does not accept because you created the textfile as ASCI.The characters in this case specifically where ♥♥♥
To make it work:
Set f = fs.CreateTextFile(TextExportFile, True, True)
P.S.
This question was answered earlier using the transfertext macro here
As Remou points out this looks like a cleaner solution. To make it work with non-default delimiters is a bit of a pain. First start exporting the query you like to export by right clicking and choose export. In the following dialogs specify the specifications and save these. When creating the macro select the specifications you just saved.
I think there is something wrong with the spaces in your connection string
Try this:
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.ConnectionString = db
cn.Open
HTH
Update:
Maybe there is a problem with the access rights to the database?
Or the mdb is already opened exclusively by another user (You with your access in design mode)?
Try this
cn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & db