01.
using
(SqlConnection connection =
new
SqlConnection(
"connection string"
))
02.
using
(SqlCommand command = connection.CreateCommand())
03.
{
04.
DateTime start = DateTime.Now;
05.
DateTime end = start.AddDays(10);
06.
07.
StringBuilder sb =
new
StringBuilder();
08.
sb.AppendLine(
"SELECT A.[id],"
);
09.
sb.AppendLine(
"A.[timestamp],"
);
10.
sb.AppendLine(
"A.[Computer_Name],"
);
11.
sb.AppendLine(
"A.[Packet_Type],"
);
12.
sb.AppendLine(
"B.[User_Name],"
);
13.
sb.AppendLine(
"A.[F_Q_User_Name],"
);
14.
sb.AppendLine(
"A.[Client_Friendly_Name],"
);
15.
sb.AppendLine(
"B.[Framed_IP_Address],"
);
16.
sb.AppendLine(
"B.[Calling_Station_Id]"
);
17.
sb.AppendLine(
"FROM [DBNAME].[dbo].[accounting_data] AS A"
);
18.
sb.AppendLine(
"INNER JOIN [DBNAME].[dbo].[accounting_data] AS B ON (A.[id]=B.[id]+1)"
);
19.
sb.AppendLine(
"WHERE A.[Packet_Type] IN (3, 4) AND A.[timestamp] BETWEEN @StartDate AND @EndDate;"
);
20.
21.
command.CommandType = CommandType.Text;
22.
command.CommandText = sb.ToString();
23.
command.Parameters.AddWithValue(
"@StartDate"
, startDate);
24.
command.Parameters.AddWithValue(
"@EndDate"
, endDate);
25.
26.
DataTable dt =
new
DataTable();
27.
using
(SqlDataAdapter adapter =
new
SqlDataAdapter())
28.
{
29.
adapter.SelectCommand = command;
30.
adapter.Fill(dt);
31.
}
32.
}