本文整理汇总了C#中IDbCommand.ExecuteScalar方法的典型用法代码示例。如果您正苦于以下问题:C# IDbCommand.ExecuteScalar方法的具体用法?C# IDbCommand.ExecuteScalar怎么用?C# IDbCommand.ExecuteScalar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDbCommand
的用法示例。
在下文中一共展示了IDbCommand.ExecuteScalar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public void Execute(IDbCommand dbCommand)
{
var sqlString = "INSERT INTO " + entity.TableName;
var columnNameString = "";
var valueString = "";
foreach (var property in entity.Properties.Where(q => q.PropertyType != PropertyType.HasMany && q.PropertyName != "Id"))
{
columnNameString += property.ColumnName + ",";
valueString += "@" + property.PropertyName + ",";
var value = property.Value;
var propertyEntity = property.Value as Entity;
if (propertyEntity != null)
value = propertyEntity.Self.Id;
dbCommand.Parameters.Add(new SqlParameter("@" + property.PropertyName, value ?? DBNull.Value));
}
columnNameString = columnNameString.TrimEnd(',');
valueString = valueString.TrimEnd(',');
sqlString = sqlString + "(" + columnNameString + ") VALUES (" + valueString + "); SELECT @@IDENTITY";
dbCommand.CommandText = sqlString;
((dynamic) entity).Id = Convert.ToInt32(dbCommand.ExecuteScalar());
}
示例2: ReadRandomNewCommitment
public static void ReadRandomNewCommitment(string event_Type, ref string name, ref int time_Length,
ref int maxTime, ref int minTime)
{
OpenDB("MakingTime.db");
_dbcm = _dbc.CreateCommand();
sqlQuery = "select count(*) as NumberOfRegions from Event_Type where Category = '" + event_Type + "'";
_dbcm.CommandText = sqlQuery;
Int32 rows = Convert.ToInt32(_dbcm.ExecuteScalar());
sqlQuery = "select * from Event_Type where Category = '" + event_Type + "'";
_dbcm.CommandText = sqlQuery;
_dbr = _dbcm.ExecuteReader();
int commitmentRow = UnityEngine.Random.Range(0, rows);
//Question
for(int countRows = 0; countRows < commitmentRow + 1; countRows += 1)
{
_dbr.Read();
}
name = _dbr.GetString(0);
time_Length = _dbr.GetInt32(1);
maxTime = _dbr.GetInt32(8);
minTime = _dbr.GetInt32(7);
_dbr.Close();
_dbr = null;
_dbcm.Dispose();
_dbcm = null;
_dbc.Close();
_dbc = null;
}
示例3: GetLastId
public int GetLastId(IDbCommand cmd)
{
cmd.Parameters.Clear();
cmd.CommandText = "select @@IDENTITY";
object identity = cmd.ExecuteScalar();
return identity == DBNull.Value ? 0 : Convert.ToInt32(identity);
}
示例4: CreateQuartzDatabase
private void CreateQuartzDatabase(IDbCommand command) {
command.CommandText = "IF EXISTS(select * from sys.databases where name='Quartz') ALTER DATABASE [Quartz] SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
command.ExecuteScalar();
command.CommandText = "IF EXISTS(select * from sys.databases where name='Quartz') DROP Database [Quartz]";
command.ExecuteScalar();
command.CommandText = "CREATE DATABASE [Quartz]";
command.ExecuteScalar();
var script = File.ReadAllText(Path.Combine(this.XpandRootPath(), @"Support\quartz\tables_sqlServer.sql"));
var commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
foreach (string commandString in commandStrings) {
if (commandString.Trim() != "") {
command.CommandText = commandString;
command.ExecuteNonQuery();
}
}
}
示例5: GenerateKey
public object GenerateKey(IDbCommand command)
{
command.CommandType = CommandType.Text;
command.CommandText = CreateCommandText();
var result = command.ExecuteScalar();
return result;
}
示例6: ExtractName
private string ExtractName(IDbCommand command)
{
this.ConfigureQueryWithIdParameter(command);
// ExecuteScalar returns the very first result from the Database.
// ExecuteReader returns an object that can be looped over to
// retrieve all matching objects from the query (if you are expecting multiple results).
return command.ExecuteScalar().ToString();
}
示例7: CreateFile
protected void CreateFile(string database, string tableName, IDbCommand cmd, DbDataAdapter da)
{
cmd.CommandText = string.Format(TableInfoSQL, database.ToLower(), tableName.ToLower());
DataTable dt = new DataTable();
da.Fill(dt);
cmd.CommandText = string.Format(TableCommentSQL, database.ToLower(), tableName.ToLower());
string tableComments = (cmd.ExecuteScalar() ?? string.Empty).ToString();
CreateFile(tableName.ToUpper(), tableComments, dt);
}
示例8: DoInCommand
public object DoInCommand(IDbCommand command)
{
command.CommandText = cmdText;
IDbDataParameter p = command.CreateParameter();
p.ParameterName = "@PostalCode";
p.Value = postalCode;
command.Parameters.Add(p);
return command.ExecuteScalar();
}
示例9: ExecuteScalar
public object ExecuteScalar(IDbCommand command)
{
try
{
setupConnection(command);
return command.ExecuteScalar();
}
finally
{
cleanupConnection(command);
}
}
示例10: executeCmd
private static int executeCmd( IDbCommand cmd, EntityInfo entityInfo )
{
cmd.ExecuteNonQuery();
String sqlId = String.Format( "select id from {0} order by id desc", entityInfo.TableName );
sqlId = entityInfo.Dialect.GetLimit( sqlId, 1 );
//logger.Info( "get id sql:" + sqlId );
cmd.CommandText = sqlId;
return cvt.ToInt( cmd.ExecuteScalar() );
}
示例11: TryExecuteScalar
private static object TryExecuteScalar(IDbCommand command)
{
try
{
command.Connection.Open();
return command.ExecuteScalar();
}
catch (DbException ex)
{
throw new AdoAdapterException(ex.Message, command);
}
}
示例12: GetScalar
private object GetScalar(IDbCommand cmd)
{
using (cmd)
{
if (tran != null)
{
cmd.Transaction = tran;
}
return cmd.ExecuteScalar();
}
}
示例13: ExecuteNonQuery
static int ExecuteNonQuery(IDbCommand sql)
{
var result = -1;
using (var db = CreateConnection())
{
db.Open();
sql.Connection = db;
sql.Prepare();
sql.ExecuteNonQuery();
sql.CommandText = "SELECT last_insert_rowid()";
result = Int32.Parse(sql.ExecuteScalar().ToString());
db.Close();
}
return result;
}
示例14: executeCmd
private static int executeCmd( IDbCommand cmd, EntityInfo entityInfo, ref IEntity obj )
{
// 读取操作结果
int retval = cmd.ExecuteNonQuery();
// 是否采用自增编号
if (DbConfig.Instance.IsAutoId) {
String sqlId = String.Format( "select id from {0} order by id desc", entityInfo.TableName );
sqlId = entityInfo.Dialect.GetLimit( sqlId, 1 );
cmd.CommandText = sqlId;
obj.Id = cvt.ToInt( cmd.ExecuteScalar() );
}
return retval;
}
示例15: ExecuteScalarCommand
private object ExecuteScalarCommand(IDbCommand sc)
{
if (sc == null)
{
throw new ArgumentNullException("sc");
}
Open();
Stopwatch sw = null;
if (mTrackCallsPerPage)
{
sw = Stopwatch.StartNew();
}
try
{
IDbTransaction sqt = (IDbTransaction)CurrentTransaction;
if (sqt != null)
{
sc.Transaction = sqt;
}
if (_CommandTimeout != -1)
{
sc.CommandTimeout = _CommandTimeout;
}
DateTime start = DateTime.UtcNow;
object ret = sc.ExecuteScalar();
if (LongRunningCommandLogThresholdSeconds >= 0 && mCommandLog.IsWarnEnabled)
{
TimeSpan diff = DateTime.UtcNow.Subtract(start);
if (diff.TotalSeconds > LongRunningCommandLogThresholdSeconds)
{
mCommandLog.Warn(String.Format(null, "A database command took {0} milliseconds to run: {1}", diff.TotalMilliseconds, sc.CommandText));
}
}
return ret;
}
finally
{
if (!InTransaction)
{
Close();
}
if (mTrackCallsPerPage)
{
sw.Stop();
DbConnectionBase.AddCallToPage(HttpContext.Current, sw);
}
}
}