当前位置: 首页>>代码示例>>C#>>正文


C# IDbCommand.ExecuteScalar方法代码示例

本文整理汇总了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());
        }
开发者ID:AndyStewart,项目名称:Dynamo,代码行数:28,代码来源:InsertCommand.cs

示例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;
	}
开发者ID:BernardoOM,项目名称:Making-Time,代码行数:33,代码来源:DBMakingTime.cs

示例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);
 }
开发者ID:jhfoo,项目名称:FooTools,代码行数:7,代码来源:SqlClientFactory.cs

示例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();
         }
     }
 }
开发者ID:aries544,项目名称:eXpand,代码行数:16,代码来源:Updater.cs

示例5: GenerateKey

        public object GenerateKey(IDbCommand command)
        {
            command.CommandType = CommandType.Text;
            command.CommandText = CreateCommandText();

            var result = command.ExecuteScalar();

            return result;
        }
开发者ID:BrunoCaimar,项目名称:Drikka,代码行数:9,代码来源:IdentityKeyGenerator.cs

示例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();
        }
开发者ID:mdwhatcott,项目名称:DatabaseAccessTutorial,代码行数:10,代码来源:PersonQuery.cs

示例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);
        }
开发者ID:Jitlee,项目名称:LPS,代码行数:11,代码来源:Creater.cs

示例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();

            }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:12,代码来源:CommandCallbackDao.cs

示例9: ExecuteScalar

 public object ExecuteScalar(IDbCommand command)
 {
     try
     {
         setupConnection(command);
         return command.ExecuteScalar();
     }
     finally
     {
         cleanupConnection(command);
     }
 }
开发者ID:joshuaflanagan,项目名称:structuremap,代码行数:12,代码来源:AutoCommitExecutionState.cs

示例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() );
        }
开发者ID:nust03,项目名称:xcore,代码行数:12,代码来源:InsertOperation.cs

示例11: TryExecuteScalar

		private static object TryExecuteScalar(IDbCommand command)
		{
			try
			{
				command.Connection.Open();
				return command.ExecuteScalar();
			}
			catch (DbException ex)
			{
				throw new AdoAdapterException(ex.Message, command);
			}
		}
开发者ID:vbedegi,项目名称:Simple.Data,代码行数:12,代码来源:AdoAdapterAggregator.cs

示例12: GetScalar

		private object GetScalar(IDbCommand cmd)
		{
			using (cmd)
			{
				if (tran != null)
				{
					cmd.Transaction = tran;
				}

				return cmd.ExecuteScalar();
			}
		}
开发者ID:kannan-ar,项目名称:MariGold.Data.Database,代码行数:12,代码来源:Database.cs

示例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;
 }
开发者ID:pimanac,项目名称:ConsensusBot,代码行数:15,代码来源:Database.cs

示例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;
        }
开发者ID:2014AmethystCat,项目名称:wojilu,代码行数:15,代码来源:InsertOperation.cs

示例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);
         }
     }
 }
开发者ID:hinikas,项目名称:AlienForce,代码行数:48,代码来源:DbConnectionBase.cs


注:本文中的IDbCommand.ExecuteScalar方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。