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


C# SqlCommand.ToTraceString方法代码示例

本文整理汇总了C#中System.Data.SqlClient.SqlCommand.ToTraceString方法的典型用法代码示例。如果您正苦于以下问题:C# SqlCommand.ToTraceString方法的具体用法?C# SqlCommand.ToTraceString怎么用?C# SqlCommand.ToTraceString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.SqlClient.SqlCommand的用法示例。


在下文中一共展示了SqlCommand.ToTraceString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RunQuery

        /**
         * Given a SqlCommand and a way to execute that command,
         * we finally run the query. Used internally by GetReader, GetScarlar
         * and Execute methods.
         *
         * ```cs
         * 	var cmd = this.BuildCmd("SELECT * FROM Foo");
         * 	this.RunQuery
         * 	(
         * 		cmd,
         * 		query => query.ExecuteReader / ExecuteScalar / ExecuteNonQuery
         * 		affected =>
         * 		{
         * 			// given the result from the above execute method
         * 			// you then need to return the number of affected rows.
         * 		},
         * 		dispose: true // do you want the connection disposed afterwards
         * 	);
         * ```
         */
        protected object RunQuery(SqlCommand cmd, Func<SqlCommand, object> query, Func<object, int> affected, bool dispose = true)
        {
            object results;

            try
            {
                results = query.Invoke(cmd);

                if (this.Db.LogWriter != null)
                {
                    this.Db.LogWriter.WriteLine
                    (
                        cmd.ToTraceString
                        (
                            affected.Invoke(results)
                        )
                    );
                }
            }
            catch
            {
                if (this.Db.LogWriter != null)
                {
                    this.Db.LogWriter.WriteLine("-- !!!NEXT QUERY ERRORED!!!");
                    this.Db.LogWriter.WriteLine(cmd.ToTraceString());
                }

                throw;
            }
            finally
            {
                if (dispose) cmd.Connection.Dispose();
            }

            return results;
        }
开发者ID:brad-jones,项目名称:graceful,代码行数:56,代码来源:Helper.cs

示例2: ToTraceStringWithParamsTest

        public void ToTraceStringWithParamsTest()
        {
            var cmd = new SqlCommand("SELECT * FROM Foo WHERE Id = @p0");
            cmd.Parameters.AddWithValue("@p0", 1);

            var expected = new StringBuilder();
            expected.AppendLine("================================================================================");
            expected.AppendLine("SELECT * FROM Foo WHERE Id = @p0");
            expected.AppendLine("-- @p0: Input Int (Size = 0) [1]");
            expected.AppendLine("-- [0] records affected.");
            expected.AppendLine();

            Assert.Equal
            (
                expected.ToString(),
                cmd.ToTraceString()
            );
        }
开发者ID:brad-jones,项目名称:graceful,代码行数:18,代码来源:ExtensionMethodTests.cs

示例3: ToTraceStringTest

        public void ToTraceStringTest()
        {
            var cmd = new SqlCommand("SELECT * FROM Foo");

            var expected = new StringBuilder();
            expected.AppendLine("================================================================================");
            expected.AppendLine("SELECT * FROM Foo");
            expected.AppendLine("-- [0] records affected.");
            expected.AppendLine();

            Assert.Equal
            (
                expected.ToString(),
                cmd.ToTraceString()
            );
        }
开发者ID:brad-jones,项目名称:graceful,代码行数:16,代码来源:ExtensionMethodTests.cs

示例4: ExecuteScalar

        public object ExecuteScalar(SqlCommand command)
        {
            //overime stav klienta
            this.InternalCheckClientState();

            //osetrenie vstupneho argumentu
            if (command == null)
                throw new ArgumentNullException("command");
            if (String.IsNullOrEmpty(command.CommandText))
                throw new ArgumentException("Argument 'command' is not valid.");

            try
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Verbose, "SQL Command: '{0}'", command.ToTraceString());

                //pridame aktivne spojenie do priakzu
                command.Connection = this.m_connection;
                if (this.m_transaction != null)
                {
                    command.Transaction = this.m_transaction;
                }

                //vykoname pozadovany prikaz
                return command.ExecuteScalar();
            }
            catch (SqlException ex)
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Error, "Chyba pri vykonavani SQL prikazu. {0}", ex);

                //preposleme vynimku vyssie
                throw;
            }
            catch (Exception ex)
            {
                //zalogujeme
                this.InternalTrace(TraceTypes.Error, "Chyba pri vykonavani SQL prikazu. {0}", ex);

                //preposleme vynimku vyssie
                throw;
            }
        }
开发者ID:858project,项目名称:net.858project.dll,代码行数:43,代码来源:SqlClientBase.cs


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