dcsimg
Results 1 to 6 of 6

Thread: Connecting to and Manipulating a DB without datagrid

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    678

    Connecting to and Manipulating a DB without datagrid

    Morning All,

    this may be a question asked a thousand times, but I keep getting led in a circle.

    I have a DB setup (KF.mdf) which is a single DB file, and not an active SQL instance on a SQL Server. I want to connect my app to this DB, so I went about adding a "Data Source" to the project, which is fine, and I can see the tables etc.

    All tutorials I see at this point however, are so far found to be pointing the dataset view directly at the DB. I dont want to operate in this way, ideally, I just want to use the DB as a source of data, do lookups, add new columns, update rows etc. I last accomplished this many years ago with VB6 and an access database (its been a while!)

    Can anyone point me to an appropriate reference with examples, showing how I would manipulate the table by:

    • Searching and moving between records
    • Creating and updatng existing records
    • Removing records as necessary
    • Reporting on records


    Thanks

    James
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,994

    Re: Connecting to and Manipulating a DB without datagrid

    If it's your intention to change the schema then you shouldn't be creating a Data Source because relies on the specific schema when you create it. Just use straight ADO.NET. If you want to execute an arbitrary SQL statement then create a SqlCommand and call the appropriate method, i.e. ExecuteNonQuery for SQL that doesn't produce a result set, ExecuteScalar to get a single value and ExecuteReader to get a full result set. Follow the Database FAQ link in my signature below and check out some of the .NET resources there.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,994

    Re: Connecting to and Manipulating a DB without datagrid

    If you don't intend to change the schema then you can still use straight ADO.NET but a typed DataSet and Data Source will do just as well. You still retrieve and save data in exactly the same way as you will have seen in those tutorials, i.e. call Fill on a table adapter to populate a DataTable in a DataSet and then call Update on the same adapter to save changes. You simply edit the data in the DataTable directly in code. The DataTable itself is a list of rows so you treat it like any other list. Each DataRow has a property for each column to get and set data. The DataTable has methods to create and add new DataRows and each DataRow has a Delete method. That's all you need.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    678

    Re: Connecting to and Manipulating a DB without datagrid

    Quote Originally Posted by jmcilhinney View Post
    If you don't intend to change the schema then you can still use straight ADO.NET but a typed DataSet and Data Source will do just as well. You still retrieve and save data in exactly the same way as you will have seen in those tutorials, i.e. call Fill on a table adapter to populate a DataTable in a DataSet and then call Update on the same adapter to save changes. You simply edit the data in the DataTable directly in code. The DataTable itself is a list of rows so you treat it like any other list. Each DataRow has a property for each column to get and set data. The DataTable has methods to create and add new DataRows and each DataRow has a Delete method. That's all you need.

    I haven't any idea about using the schema personally. ADO.NET may well be the best method of connecting and using the database connection. I will look at the options.
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,612

    Re: Connecting to and Manipulating a DB without datagrid

    I certainly endorse that approach. The whole datasource and typed datasets can create something of a black box. They do a lot for you, and you get a whole lot of functionality very cheaply, you just may not quite understand that functionality, which can make some changes quite difficult.

    The Command object would be the place to start reading, in my opinion, rather than ADO.NET. If you go looking for ADO.NET, you'll likely get a whole lot of background, which may not provide much information of immediate use. Using ADO.NET is pretty trivial, but the Command object is at the core of it all, so that seems the place to start.

    Any tutorial by JMC is usually a pretty good place to start, too.
    My usual boring signature: Nothing

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,994

    Re: Connecting to and Manipulating a DB without datagrid

    Quote Originally Posted by Shaggy Hiker View Post
    The Command object would be the place to start reading, in my opinion, rather than ADO.NET. If you go looking for ADO.NET, you'll likely get a whole lot of background, which may not provide much information of immediate use. Using ADO.NET is pretty trivial, but the Command object is at the core of it all, so that seems the place to start.
    Just to clarify, the DbCommand class is the base class for all ADO.NET command types and, if you're using SQL Server as a database, you should be using the System.Data.SqlClient provider and it uses the SqlCommand class specifically.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width