SqlParameter无法正常工作

时间:2016-09-11 00:46:50

标签: c# sql .net sql-server

。我无法让SqlParameter在我的SqlCommand上工作。当我运行下面的代码时,SqlParameters不会应用于SqlCommand。

SqlCommand tSQL = new SqlCommand();
tSQL.CommandText = "SELECT TraceEvent.name AS [EventName], Trace.* FROM sys.fn_trace_gettable(CONVERT(VARCHAR(150), (@filepath)), @numberoffiles) Trace JOIN sys.trace_events TraceEvent ON Trace.EventClass = TraceEvent.trace_event_id WHERE TraceEvent.name LIKE @eventType ORDER BY Trace.StartTime";

tSQL.Parameters.AddWithValue("filepath", @"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Log\log_11.trc");
tSQL.Parameters.AddWithValue("numberoffiles", 1);
tSQL.Parameters.AddWithValue("eventType", '%');

tSQL.Connection = Connection;

Connection.Open();
Connection.ChangeDatabase("master");

DataSet DataSet = new DataSet();
SqlDataAdapter Adapter = new SqlDataAdapter(tSQL);

Adapter.Fill(DataSet);

foreach (DataTable Table in DataSet.Tables)
{
    Result.Add(Table);
}

1 个答案:

答案 0 :(得分:1)

你在sql参数之前错过了'@'..

tSQL.Parameters.AddWithValue("@filepath", @"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Log\log_11.trc");
tSQL.Parameters.AddWithValue("@numberoffiles", 1);
tSQL.Parameters.AddWithValue("@eventType", '%');

我想这会有所帮助..