本文整理汇总了C#中DBConnection.ExecuteScalarCommand方法的典型用法代码示例。如果您正苦于以下问题:C# DBConnection.ExecuteScalarCommand方法的具体用法?C# DBConnection.ExecuteScalarCommand怎么用?C# DBConnection.ExecuteScalarCommand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBConnection
的用法示例。
在下文中一共展示了DBConnection.ExecuteScalarCommand方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
/// <summary>
/// Executes this command against the specified database connection and returns the auto-increment value of the inserted row, or null if it is not an
/// auto-increment table.
/// </summary>
public object Execute( DBConnection cn )
{
var cmd = cn.DatabaseInfo.CreateCommand();
cmd.CommandText = "INSERT INTO " + table;
if( columnModifications.Count == 0 )
cmd.CommandText += " DEFAULT VALUES";
else {
cmd.CommandText += "( ";
foreach( var columnMod in columnModifications )
cmd.CommandText += columnMod.ColumnName + ", ";
cmd.CommandText = cmd.CommandText.Substring( 0, cmd.CommandText.Length - 2 );
cmd.CommandText += " ) VALUES( ";
foreach( var columnMod in columnModifications ) {
var parameter = columnMod.GetParameter();
cmd.CommandText += parameter.GetNameForCommandText( cn.DatabaseInfo ) + ", ";
cmd.Parameters.Add( parameter.GetAdoDotNetParameter( cn.DatabaseInfo ) );
}
cmd.CommandText = cmd.CommandText.Substring( 0, cmd.CommandText.Length - 2 );
cmd.CommandText += " )";
}
cn.ExecuteNonQueryCommand( cmd );
if( !cn.DatabaseInfo.LastAutoIncrementValueExpression.Any() )
return null;
var autoIncrementRetriever = cn.DatabaseInfo.CreateCommand();
autoIncrementRetriever.CommandText = "SELECT {0}".FormatWith( cn.DatabaseInfo.LastAutoIncrementValueExpression );
var autoIncrementValue = cn.ExecuteScalarCommand( autoIncrementRetriever );
return autoIncrementValue != DBNull.Value ? autoIncrementValue : null;
}
示例2: ExecuteScalar
/// <summary>
/// Executes this sproc against the specified database connection and returns a single value.
/// </summary>
public object ExecuteScalar( DBConnection cn )
{
var cmd = cn.DatabaseInfo.CreateCommand();
setupDbCommand( cmd, cn.DatabaseInfo );
return cn.ExecuteScalarCommand( cmd );
}