Selecting specific records in a SQL Server database from C #

I am currently trying to grab some rows from a SQL Server database using C #, which have the following criteria:

  • From the RamResults
  • in the table Results
  • where the Date column is the current date

I have the following:

 // Open the same connection with the same connection string. using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString)) { con.Open(); // Read specific values in the table. using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date == @Form1.date", con)) { SqlCeDataReader reader = com.ExecuteReader(); while (reader.Read()) { int resultsoutput = reader.GetInt32(0); MessageBox.Show(resultsoutput.ToString()); } } } 

Using SELECT Result FROM RamResults WHERE Date == Form1.date causes an error:

An error occurred while parsing the request. [Token line number = 1, token line offset = 43, token with error = =]

Although I take out the WHERE statement, for example.

 SELECT Result FROM RamResults 

it works great

+6
source share
9 answers

Description

2 things

  • Use = instead of == because it is the correct equality operator in T-SQL . Your request should look like this

    SELECT Result FROM RamResults WHERE Date = @Date

  • You forgot to pass the parameter.

Example

 // Open the same connection with the same connection string. using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString)) { con.Open(); // Read specific values in the table. using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @Date", con)) { com.Parameters.AddWithValue("@Date", Form1.date); SqlCeDataReader reader = com.ExecuteReader(); while (reader.Read()) { int resultsoutput = reader.GetInt32(0); MessageBox.Show(resultsoutput.ToString()); } } } 
+6
source

Try parameterizing your query and replace == with = in the WHERE :

 // ... using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @date", con)) { com.Parameters.Add(new SqlParameter("date", Form1.date)); // ... } // ... 
+5
source

Try the following:

 using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @date", con)) { com.Parameters.AddWithValue("date",Form1.date); SqlCeDataReader reader = com.ExecuteReader(); while (reader.Read()) 

Always use SQL parameters instead of string concatenation.

+2
source

Not == , use = for the equation in SQL:

 WHERE Date = @Form1.date 
+1
source

The operator "==" is not valid syntax for SQL. Use the single equal sign "=" in the where clause.

+1
source

Try

 var query = "SELECT Result FROM RamResults WHERE Date = " + Form1.date; using (SqlCeCommand com = new SqlCeCommand(query, con)) 

I would suggest using parameters, as in the example on MSDN

+1
source

Replace SQL Query with the following text:

 SELECT Result FROM RamResults WHERE Date like DATEADD(day, DATEDIFF(day, 0, getdate()), 0) 

Hope this works.

+1
source

This is quite confusing, and many people make similar mistakes. While C # uses == for equality operations (Ok We also have Equal ()), SQL uses just = for it.

In addition, you also forgot to pass the parameter here

0
source

I found two things in your code that are creating a problem

1) assign values ​​to parameter 2) == instead of = (just to work correctly with SQL)

so the code should look like this:

 using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString)) { con.Open(); using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @Date", con)) { com.Parameters.AddWithValue("@Date", Form1.date); SqlCeDataReader reader = com.ExecuteReader(); while (reader.Read()) { int resultsoutput = reader.GetInt32(0); MessageBox.Show(resultsoutput.ToString()); } } } 
0
source

Source: https://habr.com/ru/post/904922/


All Articles