• 0

Visual Studios C# Query Fail


Question

Hi-

Wondering if anyone can help me with this. I have a query that returns results just fine in MSSQL, however when I run it in my code, it returns 0 rows. Here's the query:

SELECT m.Email FROM o.dbo.mem m, o.dbo.roleKey u, o.dbo.role r WHERE r.RoleName = 'Member' AND r.rid = u.rid AND u.uid = m.uid

And in the code:

cmd.CommandText = "SELECT m.Email FROM o.dbo.mem m, o.dbo.roleKey u, o.dbo.role r WHERE r.RoleName = 'Member' AND r.rid = u.rid AND u.uid = m.uid";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
Object rowsR = cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
sqlConnection1.Close();

rowsR returns -1. Again, the query works when I run it in MSSQL.

I tried to re-write it with joins, so here is it rewritten a different way, however this doesn't work either. I had to hard code the role values, so I'd prefer to simply fix the first way.

SELECT m.Email FROM o.dbo.mem M JOIN o.dbo.roleKey U ON M.uid = U.uid WHERE rid = 'Key1' OR rid = 'Key2'

I'm really new to Visual Studios. I used Visual Basic about 6 years ago in High School, and have been thrown into Visual Studios in order for this semester long project. So, I may be over looking something incredibly simple, but I'm learning it as I go along.

I also understand that the database isn't set up in the best way, but it's what I've been given and I can't change the structure at this point.

Any help is GREATLY appreciated.

Thanks!

Edited by Hendrick
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Well yes, you're calling the ExecuteNonQuery() method. This is designed to execute queries that don't return a result to it won't return a result...

What are you hoping to accomplish here?

Link to comment
Share on other sites

  • 0

That would totally be it. I must have read the little pop-up wrong on Visual Studios when trying to decide which executer to use.

I was pulling my hair out over this one.

Thanks again for your help!

Link to comment
Share on other sites

This topic is now closed to further replies.