其他有序参数,因为并非所有OLEDB提供商都支持命名参数

时间:2015-10-07 15:15:15

标签: c# .net database oledb

有人能解释一下以下方法中以下评论的含义吗?

// Additional ordered parameters since not all OLEDB providers support named parameters

我无法理解为什么其他参数包含两次相同的字段(SerialNumber)。

感谢。

protected override void RecordActivation( ServerRequestContext context, ActivationLimit limit, ActivationProfile profile )
{
    using( OleDbConnection conn = new OleDbConnection( ConnectionString ) )
    {
        conn.Open();
        using( OleDbTransaction transaction = conn.BeginTransaction() )
        {
            using( OleDbCommand cmd = new OleDbCommand( String.Format(
@"IF EXISTS ( SELECT * FROM {0} WHERE SerialNumber = ? AND ReferenceId = ? )
UPDATE {0} SET ProfileHash = ?, DateActivated = ?, AllowNewMachine = 0 WHERE SerialNumber = ? AND ReferenceId = ?
ELSE
INSERT INTO {0} ( SerialNumber, DateActivated, ProfileHash, ReferenceId, AllowNewMachine ) VALUES ( ?, ?, ?, ?, 0 )", ActivationTableName ), conn, transaction ) )
            {

                cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) );
                cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) );
                cmd.Parameters.Add( new OleDbParameter( "ProfileHash", profile.Hash ) );
                cmd.Parameters.Add( new OleDbParameter( "DateActivated", DateTime.UtcNow ) );
                // Additional ordered parameters since not all OLEDB providers support named parameters
                cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) );
                cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) );
                cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) );
                cmd.Parameters.Add( new OleDbParameter( "DateActivated", DateTime.UtcNow ) );
                cmd.Parameters.Add( new OleDbParameter( "ProfileHash", profile.Hash ) );
                cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) );

                cmd.ExecuteNonQuery();

            }

            transaction.Commit();           
        }
    }
}

1 个答案:

答案 0 :(得分:1)

re:我无法理解为什么其他参数包含两次相同的字段(SerialNumber)。

因为它们被用于多个地方。 我已经在#s中添加了#,以帮助确定参数在正在执行的语句中的位置:

@"IF EXISTS ( SELECT * FROM {0} WHERE 
SerialNumber = ?1 AND ReferenceId = ?2 )
UPDATE {0} SET ProfileHash = ?3, DateActivated = ?4, AllowNewMachine = 0 
WHERE SerialNumber = ?5 AND ReferenceId = ?6
ELSE
INSERT INTO {0} ( SerialNumber, DateActivated, ProfileHash, ReferenceId, AllowNewMachine ) 
VALUES ( ?7, ?8, ?9, ?a, 0 )", ActivationTableName ), conn, transaction ) )
{    
cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) ); --1
cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) ); --2
cmd.Parameters.Add( new OleDbParameter( "ProfileHash", profile.Hash ) );--3
cmd.Parameters.Add( new OleDbParameter( "DateActivated", DateTime.UtcNow ) ); --4
// Additional ordered parameters since not all OLEDB providers support named parameters
cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) ); --5
cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) );  --6
cmd.Parameters.Add( new OleDbParameter( "SerialNumber", context.SerialNumber ) ); --7
cmd.Parameters.Add( new OleDbParameter( "DateActivated", DateTime.UtcNow ) ); --8
cmd.Parameters.Add( new OleDbParameter( "ProfileHash", profile.Hash ) ); --9
cmd.Parameters.Add( new OleDbParameter( "ReferenceId", profile.ReferenceId ) );  --a