本文整理汇总了C#中VistaDBCommand.ExecuteScalar方法的典型用法代码示例。如果您正苦于以下问题:C# VistaDBCommand.ExecuteScalar方法的具体用法?C# VistaDBCommand.ExecuteScalar怎么用?C# VistaDBCommand.ExecuteScalar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VistaDBCommand
的用法示例。
在下文中一共展示了VistaDBCommand.ExecuteScalar方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: tgDataResponse
tgDataResponse IDataProvider.ExecuteScalar(tgDataRequest request)
{
tgDataResponse response = new tgDataResponse();
VistaDBCommand cmd = null;
try
{
cmd = new VistaDBCommand();
if (request.CommandTimeout != null) cmd.CommandTimeout = request.CommandTimeout.Value;
if (request.Parameters != null) Shared.AddParameters(cmd, request);
switch (request.QueryType)
{
case tgQueryType.TableDirect:
cmd.CommandType = CommandType.TableDirect;
cmd.CommandText = request.QueryText;
break;
case tgQueryType.StoredProcedure:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = Shared.CreateFullName(request);
break;
case tgQueryType.Text:
cmd.CommandType = CommandType.Text;
cmd.CommandText = request.QueryText;
break;
case tgQueryType.DynamicQuery:
cmd = QueryBuilder.PrepareCommand(request);
break;
}
try
{
tgTransactionScope.Enlist(cmd, request.ConnectionString, CreateIDbConnectionDelegate);
#region Profiling
if (sTraceHandler != null)
{
using (esTraceArguments esTrace = new esTraceArguments(request, cmd, "ExecuteScalar", System.Environment.StackTrace))
{
try
{
response.Scalar = cmd.ExecuteScalar();
}
catch (Exception ex)
{
esTrace.Exception = ex.Message;
throw;
}
}
}
else
#endregion Profiling
{
response.Scalar = cmd.ExecuteScalar();
}
}
finally
{
tgTransactionScope.DeEnlist(cmd);
}
if (request.Parameters != null)
{
Shared.GatherReturnParameters(cmd, request, response);
}
}
catch (Exception ex)
{
CleanupCommand(cmd);
response.Exception = ex;
}
return response;
}
示例2: OnRowUpdated
protected static void OnRowUpdated(object sender, VistaDBRowUpdatedEventArgs e)
{
try
{
PropertyCollection props = e.Row.Table.ExtendedProperties;
if (props.ContainsKey("props"))
{
props = (PropertyCollection)props["props"];
}
if (e.Status == UpdateStatus.Continue && (e.StatementType == StatementType.Insert || e.StatementType == StatementType.Update))
{
tgDataRequest request = props["tgDataRequest"] as tgDataRequest;
tgEntitySavePacket packet = (tgEntitySavePacket)props["esEntityData"];
string source = props["Source"] as string;
if (e.StatementType == StatementType.Insert)
{
if (props.Contains("AutoInc"))
{
string autoInc = props["AutoInc"] as string;
VistaDBCommand cmd = new VistaDBCommand();
cmd.Connection = e.Command.Connection;
cmd.Transaction = e.Command.Transaction;
cmd.CommandText = "SELECT LastIdentity([" + autoInc + "]) FROM [" + source + "]";
object o = null;
#region Profiling
if (sTraceHandler != null)
{
using (esTraceArguments esTrace = new esTraceArguments(request, cmd, "OnRowUpdated", System.Environment.StackTrace))
{
try
{
o = cmd.ExecuteScalar();
}
catch (Exception ex)
{
esTrace.Exception = ex.Message;
throw;
}
}
}
else
#endregion Profiling
{
o = cmd.ExecuteScalar();
}
if (o != null)
{
e.Row[autoInc] = o;
e.Command.Parameters["@" + autoInc].Value = o;
}
}
if (props.Contains("EntitySpacesConcurrency"))
{
string esConcurrencyColumn = props["EntitySpacesConcurrency"] as string;
packet.CurrentValues[esConcurrencyColumn] = 1;
}
}
if (props.Contains("Timestamp"))
{
string column = props["Timestamp"] as string;
VistaDBCommand cmd = new VistaDBCommand();
cmd.Connection = e.Command.Connection;
cmd.Transaction = e.Command.Transaction;
cmd.CommandText = "SELECT LastTimestamp('" + source + "');";
object o = null;
#region Profiling
if (sTraceHandler != null)
{
using (esTraceArguments esTrace = new esTraceArguments(request, cmd, "OnRowUpdated", System.Environment.StackTrace))
{
try
{
o = cmd.ExecuteScalar();
}
catch (Exception ex)
{
esTrace.Exception = ex.Message;
throw;
}
}
}
else
#endregion Profiling
//.........这里部分代码省略.........
示例3: esDataResponse
esDataResponse IDataProvider.ExecuteScalar(esDataRequest request)
{
esDataResponse response = new esDataResponse();
VistaDBCommand cmd = null;
try
{
cmd = new VistaDBCommand();
if (request.CommandTimeout != null) cmd.CommandTimeout = request.CommandTimeout.Value;
if (request.Parameters != null) Shared.AddParameters(cmd, request);
switch (request.QueryType)
{
case esQueryType.TableDirect:
cmd.CommandType = CommandType.TableDirect;
cmd.CommandText = request.QueryText;
break;
case esQueryType.StoredProcedure:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = Shared.CreateFullName(request);
break;
case esQueryType.Text:
cmd.CommandType = CommandType.Text;
cmd.CommandText = request.QueryText;
break;
case esQueryType.DynamicQuery:
cmd = QueryBuilder.PrepareCommand(request);
break;
}
try
{
esTransactionScope.Enlist(cmd, request.ConnectionString, CreateIDbConnectionDelegate);
response.Scalar = cmd.ExecuteScalar();
}
finally
{
esTransactionScope.DeEnlist(cmd);
}
if (request.Parameters != null)
{
Shared.GatherReturnParameters(cmd, request, response);
}
}
catch (Exception ex)
{
CleanupCommand(cmd);
response.Exception = ex;
}
return response;
}
示例4: OnRowUpdated
// If it's an Insert we fetch the @@Identity value and stuff it in the proper column
protected void OnRowUpdated(object sender, VistaDBRowUpdatedEventArgs e)
{
try
{
if(e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
{
TransactionMgr txMgr = TransactionMgr.ThreadTransactionMgr();
string[] identityCols = this.GetAutoKeyColumns().Split(';');
VistaDBCommand cmd = new VistaDBCommand();
foreach(string col in identityCols)
{
cmd.CommandText = "SELECT LastIdentity([" + col + "]) FROM [" + this.QuerySource + "]";
// We make sure we enlist in the ongoing transaction, otherwise, we
// would most likely deadlock
txMgr.Enlist(cmd, this);
object o = cmd.ExecuteScalar(); // Get the Identity Value
txMgr.DeEnlist(cmd, this);
if(o != null)
{
e.Row[col] = o;
}
}
e.Row.AcceptChanges();
}
}
catch {}
}
示例5: GetDatabaseVersionFunction
public static string GetDatabaseVersionFunction()
{
try
{
//To open a SQL connection to VistaDB from within a CLR Proc you must set the connection string to Context connection=true like this....
//NOTE: We DO want to dispose of this object because we are the ones allocating it
using (VistaDBConnection conn = new VistaDBConnection("Context Connection=true"))
{
conn.Open();
using (VistaDBCommand command = new VistaDBCommand())
{
command.Connection = conn;
command.CommandText = "SELECT @@Version";
return Convert.ToString(command.ExecuteScalar());
}
}
}
catch (Exception e)
{
return e.Message;
}
}
示例6: GetDatabaseVersionProcedure
public static int GetDatabaseVersionProcedure(out string versionString )
{
try
{
//To open a SQL connection to VistaDB from within a CLR Proc you must set the connection string to Context connection=true like this....
//NOTE: We DO want to dispose of this object because we are the ones allocating it
using (VistaDBConnection conn = new VistaDBConnection("Context Connection=true"))
{
conn.Open();
using (VistaDBCommand command = new VistaDBCommand())
{
command.Connection = conn;
command.CommandText = "SELECT @@Version";
versionString = Convert.ToString(command.ExecuteScalar());
return 0;
}
}
}
catch (Exception e)
{
throw new ApplicationException("Unable to get the database version due to Application Error", e);
}
}
示例7: CallGetDatabaseVersionFunctionSQL
/// <summary>
/// Call the Sql Function version to get the database version
/// </summary>
public static void CallGetDatabaseVersionFunctionSQL()
{
Console.WriteLine("Attempting to execute CLR Function GetDatabaseVersionFunction");
using (VistaDBConnection connection = new VistaDBConnection(SampleRunner.ConnectionString))
{
connection.Open();
try
{
// Straight forward way to call a function is just using SELECT
// You cannot EXEC a SqlFunction, and you cannot set the command here to be a stored proc
// Setting this command to a stored proc is a common error, the two are not the same
// SqlFunction = SELECT to call
// SqlProcdure = EXEC or direct call using StoredProcedure command type
using (VistaDBCommand command = new VistaDBCommand())
{
command.Connection = connection;
command.CommandText = "SELECT GetVersionFunction();";
// The results are returned as a part of the standard rowset, so we only need to get back the first entry
Console.WriteLine(Convert.ToString(command.ExecuteScalar()));
}
}
catch (Exception e)
{
Console.WriteLine("Failed to execute CLR Function GetVersionFunction, Reason: " + e.Message);
}
}
}
示例8: ExecuteScalar
/// <summary>
/// Execute a VistaDBCommand (that returns a 1x1 resultset) against the specified VistaDBTransaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int orderCount = (int)ExecuteScalar(trans, CommandType.Text, "Select count(Order) from TableTransaction where ProdId=?", new VistaDBParameter("@prodid", 24));
/// </remarks>
/// <param name="transaction">A valid VistaDBTransaction</param>
/// <param name="commandType">The CommandType (TableDirect, Text)</param>
/// <param name="commandText">The T-SQL command</param>
/// <param name="commandParameters">An array of VistaDBParamters used to execute the command</param>
/// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(VistaDBTransaction transaction, CommandType commandType, string commandText, params VistaDBParameter[] commandParameters)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );
// Create a command and prepare it for execution
VistaDBCommand cmd = new VistaDBCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, (VistaDBConnection)transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
// Execute the command & return the results
object retval = cmd.ExecuteScalar();
// Detach the VistaDBParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
}