Link to home
Start Free TrialLog in
Avatar of Scott Palmer
Scott PalmerFlag for United States of America

asked on

SQL syntax in VB.net

I am trying to run an SQL query in VB.NET and I am having problems with the syntax.  Below is the code and I have attached the error.

 adoConn.CursorLocation = CursorLocationEnum.adUseClient
 adoConn.Open("Driver={MySQL ODBC 5.2 ANSI Driver};" & "server=" & lServer & ";user=spalmer" &
                         ";password=mypassword;database=daily_reports")
rsCodeLookup.Open("SELECT * FROM 100_report WHERE RPT_DATE > date(now()) - 300", adoConn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly)
rsCodeLookup.ActiveConnection = Nothing

Open in new window

SQL-Error.docx
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

SqlConnection sqlConnection1 = new SqlConnection("Driver={MySQL ODBC 5.2 ANSI Driver};server=lServer;user=spalmer;password=mypassword;database=daily_reports");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = ""SELECT * FROM 100_report WHERE RPT_DATE > date(now()) - 300"";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();

Open in new window

Sorry, VB

Dim sqlConnection1 As New SqlConnection("Driver={MySQL ODBC 5.2 ANSI Driver};server=lServer;user=spalmer;password=mypassword;database=daily_reports")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader

cmd.CommandText = "SELECT * FROM 100_report WHERE RPT_DATE > date(now()) - 300"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1

sqlConnection1.Open()

reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.

sqlConnection1.Close()

Open in new window

Avatar of Scott Palmer

ASKER

Thanks, but I was hoping to use my current code as much as possible.

This code below worked, but the query was too large to so tried to filter with the date to get a smaller result.

rsCodeLookup.Open("SELECT * FROM 100_report", adoConn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly)

It was only when I added the where clause that I had problems.
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Had to rework the DATE_ADD order, but got it to work.  Thanks.