I have two DateTimePickers (dateStart and dateStop) in a winform. I've set dateStart and dateSop's format like this:
dateStart.Format = DateTimePickerFormat.Custom;
dateStart.CustomFormat = "yyyy:MM:dd HH:mm:ss";
dateStop.Format = DateTimePickerFormat.Custom;
dateStop.CustomFormat = "yyyy:MM:dd HH:mm:ss";
I want to count number of rows from my datebase's table between dateStart and dateStop. My select string:
string stmt = "SELECT COUNT(*) FROM [dbo].[tbl_TagLogging] WHERE DateTime BETWEEN " + Convert.ToString(dateStart.Value) + " AND " + Convert.ToString(dateStop.Value);
This is how link DateStart and DateStop paramaters of my datebase with DateTimePickers.
cmd.Parameters.Add("@DataStart", SqlDbType.DateTime).Value = dateStart.Value;// new DateTime(dateStart.Value);
cmd.Parameters.Add("@DataStop", SqlDbType.DateTime).Value = dateStop.Value;// new DateTime(2020, 02, 05, 13, 12, 25, 703);//2020, 02, 05, 13, 12, 25, 703 //2020, 02, 05, 13, 06, 50, 700
The row counting works perfectly without WHERE clause.
count = (int)cmdCount.ExecuteScalar();//this is how I count the rows
I'm getting here a 'System.Data.SqlClient.SqlException: Incorect syntax near 9'.
I kinda don't understand why if dateStart's format is "yyyy:MM:dd HH:mm:ss", dateStart.value is returned in another format. On my UI, dateStart is displayed in the right format (the one from the code above). But I might have done something faulty in my stmt string. Any thoughts on what is wrong?
EDIT 1 (my entire code):
private void button_Click(object sender, EventArgs e)
{
dateStart.Format = DateTimePickerFormat.Custom;
dateStart.CustomFormat = "yyyy:MM:dd HH:mm:ss";
dateStop.Format = DateTimePickerFormat.Custom;
dateStop.CustomFormat = "yyyy:MM:dd HH:mm:ss";
string stmt = "SELECT COUNT(*) FROM [dbo].[tbl_TagLogging] WHERE DateTime BETWEEN @DataStart AND @DataStop"; // WHERE DateTime BETWEEN " + Convert.ToString(dateStart.Value) + " AND " + Convert.ToString(dateStop.Value)
// string stmt = "SELECT COUNT(*) FROM [dbo].[tbl_TagLogging] WHERE DateTime BETWEEN '" + Convert.ToString(dateStart.Value) + "' AND '" + Convert.ToString(dateStop.Value) + "'";
int count = 0;
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-JQSJAF8\SQLEXPRESS;Initial Catalog=TRTF_TagLogging;Integrated Security=True; MultipleActiveResultSets = true"))
{
using (SqlCommand cmd = new SqlCommand("PS_TagLogging", connection))// used for stored proc
{
using (SqlCommand cmdCount = new SqlCommand(stmt, connection))//used to count number of rows of my table
{
//yyyy:MM:dd hh:mm:ss
connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@DataStart", SqlDbType.DateTime).Value = dateStart.Value;
cmd.Parameters.Add("@DataStop", SqlDbType.DateTime).Value = dateStop.Value;
var values = new List<double>();
var valData = new List<DateTime>();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// values.Add(reader.GetInt64(0));//reads the values of first column (ID) from DB
values.Add(reader.GetFloat(1));//reads the values of second column (Tag1) from DB
valData.Add(reader.GetDateTime(2));//reads the values of third column (DateTime) from DB
}
arrDate = valData.ToArray();
arrVal = values.ToArray();
count = (int)cmdCount.ExecuteScalar();
plot1.XAxes[0].ScaleDisplay.TextFormatting.Style = Iocomp.Types.TextFormatDoubleStyle.DateTime;
plot1.XAxes[0].ScaleRange.Span = 1.0 / 120.0 ; //30sec Span;
for (int i=0; i<count; i++)
{
plot1.Channels.Trace[0].AddXY(arrDate[i], arrVal[i]);
}
connection.Close();
}
}
}
MessageBox.Show("Nr. lines: " + count);
Random().NextDouble() * 100);
}