-2

I have the following code which establishing an SQL connection inside of a project I am working on. What I want to do is to create a for loop which contains a method and every time the loop repeats the method runs with a different value until all views of the returned query are used.

I can't figure out how to refer to every value of the view without saving the view to a list or an array first. Any ideas?

SqlConnection Con = new SqlConnection(@"Data Source=localhost\**;Initial Catalog=ML;User Id=sa;Password='**'");
string sql = @"select product_id,Name from E_PRODUCT_PROPERTY";
var mylist = new List<WineRating>();
using (var command = new SqlCommand(sql, Con))
{
    Con.Open();
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
            new WineRating { product_id = reader.GetInt32(0), Name = reader.GetString(1) };                    

///Here goes the code I suppose

        method_name(reader.GetInt32(0), reader.GetString(1));

    }
}

public static int method_name(int product_id, string Name)
{
int num = x *2;
Console.WriteLine(num + Name);
}
banjo
  • 7
  • 3
  • Do you mean you want to filter the data in the database using a 'WHERE' clause? or do you want to save the values in a dictionary and access them via a 'KEY'? – Barns May 23 '20 at 01:33
  • I'm not sure I understand what you're asking. The same way you access members of the current result record (or, if you want to access the columns by name, using [the indexer](https://learn.microsoft.com/dotnet/api/system.data.sqlclient.sqldatareader.item) or [`GetOrdinal()`](https://learn.microsoft.com/dotnet/api/system.data.sqlclient.sqldatareader.getordinal)) to initialize `new WineRating` is the same way you would access those members to build text for `Console.WriteLine()` to print. You'd use a `while` loop just like you've done, not a `for` loop. – Lance U. Matthews May 23 '20 at 01:33
  • What I actually want is to pass every record of the view into a method one by one but I dont understand how to do that.Can you give me an example of how to reffer every record in a loop and pass them to a method one record each time a loop repeats? – banjo May 23 '20 at 02:03
  • Thanks I edited my question maybe now its more clear, as soon as I am able to try It I will run your code and tell you feedback – banjo May 23 '20 at 02:18
  • Hi again It is not working for some reason,are you sure it is right? – banjo May 23 '20 at 15:30
  • If these comments are in response to [@Barns's answer](https://stackoverflow.com/a/61966594/150605) you should post them on that answer so that user will be notified of them. You should probably elaborate on "It is not working", too. – Lance U. Matthews May 23 '20 at 16:21
  • If the edited code above reflects the code you are testing then, no...it can't work. Because you did not follow the code example I provided! --You did notice the Curly Brackets? They aren't just pretty...they have a function. – Barns May 23 '20 at 17:47

1 Answers1

0

Perhaps like this:

using (var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        MyMethodToPrintToScreen(reader.GetInt32(0), reader.GetString(1));
    }
}

With the method to print to screen

private static void MyMethodToPrintToScreen(int id, string product)
{
    //Do whatever you wish with the data: example
    Console.WriteLine($"My id: {id} | Product: {product}");
}

Edit
Let me make it even more obvious(using your exact code):

SqlConnection Con = new SqlConnection(@"Data Source=localhost\**;Initial Catalog=ML;User Id=sa;Password='**'");
string sql = @"select product_id,Name from E_PRODUCT_PROPERTY";
var mylist = new List<WineRating>();
using (var command = new SqlCommand(sql, Con))
{
    Con.Open();
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            method_name(reader.GetInt32(0), reader.GetString(1));
        }
    }
}
Barns
  • 4,850
  • 3
  • 17
  • 31