Classic ASP adding records to MySQL

只谈情不闲聊 提交于 2020-07-08 00:34:46

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/39988294/classic-asp-adding-records-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!