0
string query = @"select p.package_id, pd.download_id
                     from Tools.ftp.package p
                     join Tools.ftp.package_download pd 
                     on p.package_id = pd.package_id
                     where p.package_name = 'foo'";

Trying to pull data from sql server in C# with SqlConnection class, I get output without the where statement but a blank file with it left in, also odd behavior with more complicated joins. Why is that. Is there anything to watch for behavior wise when writing queries in C#, ie formatting issues and whatnot.

Here's the whole thing.

StreamWriter fileout = new StreamWriter(@"C:\test\output9.csv");

    string myConnectionString = @"Data Source=foobar;Initial Catalog=DB;User id=user;Password=pw";
    SqlConnection Conn = new SqlConnection(myConnectionString);


    string query = @"select p.package_id, pd.download_id
                 from Tools.ftp.package p
                 join Tools.ftp.package_download pd 
                 on p.package_id = pd.package_id
                 where p.package_name = 'foo'";

    SqlCommand cmd = new SqlCommand(query, Conn);
    cmd.Connection = Conn;
    Conn.Open();

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();

    da.Fill(dt);
    Conn.Close();
    da.Dispose();


    foreach (DataRow dataRow in dt.Rows)
    {
        foreach (var item in dataRow.ItemArray)
        {
            fileout.Write(item+ ",");
        }
        fileout.Write("\n");
    }
    fileout.Close();
11
  • 5
    Tried running that SQL in management studio and see what results you get out? Any rows returned? I am imagine you are using SqlDataReader or a DataTable too right? Commented Aug 15, 2013 at 14:09
  • You need to provide more than just the dynamic sql also I would recommend putting that query into a stored procedure or if you are not familiar with how to do that.. re-factor that query into Parameterized query.. you need to show the code in which you are declaring the SQLCommand as well as how you are Executing the query Commented Aug 15, 2013 at 14:11
  • I think you are missing 'as' keyword. Here -> w3schools.com/sql/sql_alias.asp Commented Aug 15, 2013 at 14:11
  • What makes you think he is missing the AS keyword.. it doesn't appear that he is using Aliasing so Colin I may have to disagree with you on that one Commented Aug 15, 2013 at 14:12
  • 1
    @ColinSteel AS is optional - he renames them to p and pd Commented Aug 15, 2013 at 14:15

1 Answer 1

2

I can take out the join clause and keep the where clause in and it gives me output

That means there are no matching records in Tools.ftp.package_download Try a left join instead:

select p.package_id, pd.download_id
     from Tools.ftp.package p
     LEFT join Tools.ftp.package_download pd 
     on p.package_id = pd.package_id
     where p.package_name = 'foo'
Sign up to request clarification or add additional context in comments.

3 Comments

I just realized that even when it's returning output, it's different from the output given by management studio, so when I added the where clause it's conceivable there are no longer any matches, so my unfiltered data is incorrect to begin with, but why would the same statement return different results. I checked the db name and schema and made sure I'm in the same environment
I don't think you quite understand how joins work might be worth looking at 1keydata.com/sql/inner-join.html.
It has nothing to do with joins. I changed the environment in management studio and restarted vs 2008 and it worked, just one of those things..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.