none
datatable difficulties in query with database RRS feed

  • Question

  • HI

    when i make a select query and transfer it to a datatable

    it count all the rows in the query exactly like i wanted

    but also it take the same rows many time

    like 3 times

    i don't understand how

    the number of rows is 310 but in my case more than that

     Dim cmdDLigne2 As New SqlCommand("select * from DL_Ligne_Table where Numero=@Numero", cn)
            With cmdDLigne2.Parameters
    
                .Add("@Numero", SqlDbType.NVarChar).Value = (TextBox1.Text)
    
            End With
            Dim adapt As New SqlDataAdapter(cmdDLigne2)
            Dim tb As New DataTable
            adapt.Fill(tb)
            MessageBox.Show("longueur de table = " & tb.Rows.Count)
            DataGridView2.DataSource = tb

    please help

    Wednesday, October 2, 2019 12:28 PM

All replies

  • From Server Explorer in Visual Studio, create a connection to the server/database. Once done expand the Table node.

    Now right click on the table and select "New Query", in the new query window use your query and create a parameter via DECLARE.

    So here is an example where I join two tables (the majority of my databases are relational as in this case the customer table has CountryIdentifier which points to a Country Table with country id and country name. I expect 11 rows for this query.

    Now if you then take this query and use in your code and get different results my first question is, are you sure you are working with the same database and table? Writing the SELECT (or any SQL) first outside of code is always the correct way to write them and verify they return the proper results before using them in code.

    DECLARE @CountryIdentifier AS INT = 9
    
    SELECT Customers.CustomerIdentifier,
           Customers.CompanyName,
           Customers.ContactId,
           Customers.CountryIdentifier,
           Customers.Phone
    FROM Customers
         INNER JOIN Countries ON Customers.CountryIdentifier = Countries.CountryIdentifier
    WHERE dbo.Customers.CountryIdentifier = @CountryIdentifier


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 2, 2019 1:27 PM
    Moderator
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 3, 2019 7:58 AM
    Moderator
  • Hello Houssem12,

    Has this issue been resolved or not? If not please ask what you are having issues with, otherwise if the issue is resolved by replies here please consider marking them as answered which will help others with the same issue/question.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, October 13, 2019 11:41 AM
    Moderator