ASP专家 - 需要帮助将此PHP代码转换为ASP

时间:2021-10-05 02:05:24

I am facing some problems converting the PHP SQL insertion code below to ASP, I want to use the same concept in ASP, because it is more flexible, not tied to ASP owned keywords.

我在将下面的PHP SQL插入代码转换为ASP时面临一些问题,我想在ASP中使用相同的概念,因为它更灵活,不依赖于ASP拥有的关键字。

Wonder if anyone can help ASP newbie here.

不知道有没有人可以在这里帮助ASP新手。

$table = 'tbluser';
$array = array (
        'name' => $name,
        'email' => $email,
        'ip' => $remoteIP
        );
$newid = insert_query ($table, $array); 



  function insert_query ($table, $array)
  {
    global $mysql_errors;
    $query = 'INSERT INTO ' . $table . ' ';
    foreach ($array as $key => $value)
    {

      $fieldnamelist .= '' . '`' . $key . '`,';
      if ($value == 'now()')
      {
        $fieldvaluelist .= 'now(),';
        continue;
      }
      else
      {
        $fieldvaluelist .= '\'' . $value . '\',';
        continue;
      }
    }

    $fieldnamelist = substr ($fieldnamelist, 0, 0 - 1);
    $fieldvaluelist = substr ($fieldvaluelist, 0, 0 - 1);
    $query .= '' . '(' . $fieldnamelist . ') VALUES (' . $fieldvaluelist . ')';
    $result = mysql_query ($query);
    if (!$result)
    {
      $message = 'Invalid query: ' . mysql_error () . '<br>';
      $message .= 'Whole query: ' . $query;
    }

    $id = mysql_insert_id ();
    return $id;
  }

it's classic ASP. No choice, company still need to maintain this for a specific project.

这是经典的ASP。没有选择,公司仍然需要为特定项目维护这个。

The main reason I want this specific code style in ASP is because I don't want to tie to the ASP style of querying database (using cmd and etc...)

我想在ASP中使用这种特定代码样式的主要原因是因为我不想绑定查询数据库的ASP风格(使用cmd等...)

By creating array collection with key(table fieldname) and value, then convert into a full sql string, I only need to run .execute(sql) , it means in future I can more easier convert the project to other framework, without needing to rewrite the way to insert/update data into DB.

通过使用key(table fieldname)和value创建数组集合,然后转换为完整的sql字符串,我只需要运行.execute(sql),这意味着将来我可以更轻松地将项目转换为其他框架,而无需重写将数据插入/更新到DB的方式。

2 个解决方案

#1


Not exactly the same, but here are some functions I've used in ASP classic sites.
I think it should be a good starting point for learning.

不完全相同,但这里有我在ASP经典网站中使用的一些功能。我认为这应该是一个很好的学习起点。

Function AddUser(Name, Email, IP )
    dim sqlstmt
    sqlstmt = "INSERT INTO tbluser (Name, Email, IP )"
    sqlstmt = sqlstmt & " values (?, ?, ?)"
    call RunSQL( sqlstmt, array( _
        mp("@Name", adVarChar, 100, Name), _
        mp("@Email", adVarChar, 100, Email), _
        mp("@IP", adVarChar, 100, IP)))
end Function

Function RunSQL(strSqlStmt , params())
    On Error resume next

    ''// Create the ADO objects
    Dim cmd
    Set cmd = server.createobject("ADODB.Command")

    ''// Init the ADO objects & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = strSqlStmt
    cmd.CommandType = adCmdText
    collectParams cmd, params

    ''// Execute the query without returning a recordset
    cmd.Execute , , adExecuteNoRecords
    If err.number > 0 then
        BuildErrorMessage()
        exit function
    end if

    ''// Cleanup
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing

    Exit Function

End Function


Private Sub collectParams(cmd , argparams())
    Dim params , v
    Dim i , l , u

    params = argparams


    For i = LBound(params) To UBound(params)
        l = LBound(params(i))
        u = UBound(params(i))

        ''// Check for nulls.
        If u - l = 3 Then
            If VarType(params(i)(3)) = vbString Then
                If params(i)(3) = "" then
                                    v = null
                else
                    v = params(i)(3)
                end if
            Else
                v = params(i)(3)
            End If

        If params(i)(1) = adLongVarChar Then
                Dim p ''// As New Parameter
                Set p = cmd.CreateParameter(params(i)(0), params(i)(1), adParamInput)
                p.Attributes = adParamLong + adParamSigned
                If Not IsNull(v) Then
                    p.AppendChunk v
                    p.Size = Len(v)
                Else
                    p.Value = v
                    p.Size = 10000
                End If
                cmd.Parameters.Append p
            Else
                cmd.Parameters.Append cmd.CreateParameter(params(i)(0), params(i)(1), adParamInput, params(i)(2), v)
            End If
        Else
            RaiseError m_modName, "collectParams(...): incorrect # of parameters"
        End If
    Next
End Sub

#2


I guess a simple rewrite would not help you that much as you need to establish a DB connection, handle errors, etc.

我想一个简单的重写对你没那么大,因为你需要建立一个数据库连接,处理错误等等。

I would recommend you have a look at ajaxed library which offers you a db.insert() method .. which I believe does the same as the method you require.

我建议你看一下ajaxed库,它为你提供了一个db.insert()方法..我认为它与你需要的方法相同。

#1


Not exactly the same, but here are some functions I've used in ASP classic sites.
I think it should be a good starting point for learning.

不完全相同,但这里有我在ASP经典网站中使用的一些功能。我认为这应该是一个很好的学习起点。

Function AddUser(Name, Email, IP )
    dim sqlstmt
    sqlstmt = "INSERT INTO tbluser (Name, Email, IP )"
    sqlstmt = sqlstmt & " values (?, ?, ?)"
    call RunSQL( sqlstmt, array( _
        mp("@Name", adVarChar, 100, Name), _
        mp("@Email", adVarChar, 100, Email), _
        mp("@IP", adVarChar, 100, IP)))
end Function

Function RunSQL(strSqlStmt , params())
    On Error resume next

    ''// Create the ADO objects
    Dim cmd
    Set cmd = server.createobject("ADODB.Command")

    ''// Init the ADO objects & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = strSqlStmt
    cmd.CommandType = adCmdText
    collectParams cmd, params

    ''// Execute the query without returning a recordset
    cmd.Execute , , adExecuteNoRecords
    If err.number > 0 then
        BuildErrorMessage()
        exit function
    end if

    ''// Cleanup
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing

    Exit Function

End Function


Private Sub collectParams(cmd , argparams())
    Dim params , v
    Dim i , l , u

    params = argparams


    For i = LBound(params) To UBound(params)
        l = LBound(params(i))
        u = UBound(params(i))

        ''// Check for nulls.
        If u - l = 3 Then
            If VarType(params(i)(3)) = vbString Then
                If params(i)(3) = "" then
                                    v = null
                else
                    v = params(i)(3)
                end if
            Else
                v = params(i)(3)
            End If

        If params(i)(1) = adLongVarChar Then
                Dim p ''// As New Parameter
                Set p = cmd.CreateParameter(params(i)(0), params(i)(1), adParamInput)
                p.Attributes = adParamLong + adParamSigned
                If Not IsNull(v) Then
                    p.AppendChunk v
                    p.Size = Len(v)
                Else
                    p.Value = v
                    p.Size = 10000
                End If
                cmd.Parameters.Append p
            Else
                cmd.Parameters.Append cmd.CreateParameter(params(i)(0), params(i)(1), adParamInput, params(i)(2), v)
            End If
        Else
            RaiseError m_modName, "collectParams(...): incorrect # of parameters"
        End If
    Next
End Sub

#2


I guess a simple rewrite would not help you that much as you need to establish a DB connection, handle errors, etc.

我想一个简单的重写对你没那么大,因为你需要建立一个数据库连接,处理错误等等。

I would recommend you have a look at ajaxed library which offers you a db.insert() method .. which I believe does the same as the method you require.

我建议你看一下ajaxed库,它为你提供了一个db.insert()方法..我认为它与你需要的方法相同。