Jump to content



Photo

Visual Studios C# Query Fail

solved

  • Please log in to reply
2 replies to this topic

#1 Hendrick

Hendrick

    Neowinian

  • Joined: 14-November 07

Posted 08 February 2013 - 01:48

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, 08 February 2013 - 02:23.



#2 +LogicalApex

LogicalApex

    Software Engineer

  • Tech Issues Solved: 8
  • Joined: 14-August 02
  • Location: Philadelphia, PA
  • OS: Windows 7 Ultimate x64
  • Phone: Nexus 5

Posted 08 February 2013 - 01:54

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?

#3 OP Hendrick

Hendrick

    Neowinian

  • Joined: 14-November 07

Posted 08 February 2013 - 02:12

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!