本文整理汇总了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;
}
示例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()
);
}
示例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()
);
}
示例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;
}
}