Results 1 to 6 of 6

Thread: A few simple ADO.NET questions [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    A few simple ADO.NET questions [Resolved]

    Going through the book, studying as usual, I was instructed that when I want to use the SQLDataAdapter, SQLConnection objects, and generate a DataSet object, and then bind my control (textbox, datagrid, whatever) to it to get the value.

    My first question is:

    Is this the way most of you do it? Or do you prefer doing it through code?

    I did it the way described above, and saw that it generated a lot of code. So, my VB6-remnant brain assumed that using these objects to connect to a database would be as inefficient as using the ADODC control in VB6 (You guys remember that one? )

    So, I tried to do it through code. Here is what I came up with so far:

    VB Code:
    1. Dim scSqlConn As New SqlConnection
    2.         scSqlConn.ConnectionString = "Data Source=(local);" & _
    3.                             "Initial Catalog=NorthWind;uid=sa;pwd=;" & _
    4.                             "Integrated Security=SSPI"
    5.         scSqlConn.Open()
    6.  
    7.         'data set, data adapter
    8.  
    9.         Dim dsDataSet As New DataSet
    10.         Dim daDataAdapter As New SqlDataAdapter
    11.         Dim cmdSELECTCOMMAND As New SqlCommand
    12.         cmdSELECTCOMMAND.CommandText = "SELECT * FROM Customers;"
    13.         cmdSELECTCOMMAND.Connection = scSqlConn
    14.  
    15.  
    16.         daDataAdapter.SelectCommand = cmdSELECTCOMMAND
    17.         daDataAdapter.Fill(dsDataSet, "Customers")

    Second question:
    Am I doing it right? Am I missing something?

    Third question:
    I am simply trying to set a Textbox1.Text property to a value in the table.

    In VB6, this was:

    VB Code:
    1. TextBox1.Text = Objrs.Fields(0).Value

    I can't figure out how to do that in ADO.NET. Any ideas what I should do?



    Thanks.
    Last edited by mendhak; May 24th, 2004 at 11:28 AM.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Regarding Question 1, I manged to write this code:

    VB Code:
    1. Dim dsDataSet As New DataSet
    2.     Dim daDataAdapter As New SqlDataAdapter("SELECT * FROM Customers", sqlConn)
    3.     Dim sqlConn As New SqlConnection
    4.     Dim cmdSelectCommand As New SqlCommand
    5.  
    6. '''''''''''''''''''''''''''''''
    7.  
    8.  sqlConn.ConnectionString = "Data Source=(local);" & _
    9.                             "Initial Catalog=NorthWind;uid=sa;pwd=;" & _
    10.                             "Integrated Security=SSPI"
    11.  
    12.         sqlConn.Open()
    13.  
    14.         daDataAdapter.Fill(dsDataSet, "Customers")
    15.  
    16.  
    17.         TextBox1.Text = dsDataSet.Tables("Customers").Rows(0).Item(0).ToString()

    How's that?

    Question 4:

    Working with the same code here, how can I place two tables into the Dataset?

  3. #3
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Is this the way most of you do it? Or do you prefer doing it through code?
    You mean code vs. dropping those "objects" from the toolbox? I always write the code. Probably comes from VB6 and the back box data controls provided. Plus, at least for me, I never really got how connection, command, recordset worked until I started writing it manually. ADO.Net has been largely the same.

    As far as databinding is concerned, I'm watching your other thread with interest. My gut reaction was the same as yours, data binding is evil and doesn't work. But it does in .Net. And there aren't a lot of answers out there if you don't databind. Which is either b/c it really does work or b/c folks are lazy.

    Remember what a dataset is - a mini-db. Depending on what you're doing, a dataset can be overkill. You may just want to return a straight datatable.

    But if you are using a dataset, you're going to have to navigate to which datatable you want (b/c a dataset can hold many tables) and then navigate to what row you want (b/c a datatable can have many rows) and then you can set your textbox property.

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Originally posted by mendhak
    Working with the same code here, how can I place two tables into the Dataset?
    The dataset tables collection provides a method to add datatables.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thanks a lot for your input.

    I did indeed decide to go the code-it-by-hand way. I'm somewhat satisfied, but as you mentioned, there's so little documentation out there, it's almost appalling. Either that, or it's my search skills that are appalling.

    I managed to solve all three questions, eventually.

    Got a new problem though, that'll be a new post since this thread could be considered "saturated"

    TY!

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I too use the Code-it-by-hand methodology, also probably because of my days as a VB3 thru VB6 coder. but I have also used the objects from the toolbox and found that what you would code by hand is nearly the same code as the designers produce. As for Databinding, it works almost too well. As stated previously, a dataset is a local representation of all objects retrieved from your datasource(mini db). So binding the dataset to say a datagrid will display links for all the tables in your set. If you wish to display individual tables in separate grids get a datatable object for each table in your set and bind the table to your grid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width