Results 1 to 7 of 7

Thread: Postgres With VB.NET...

  1. #1

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Postgres With VB.NET...

    Anybody use Postgres (8.0) with VB.NET? I am a total n00b at all this code stuff, I'm getting there. Anybody can build forms, but getting the stuff to talk to the DB is a little beyond my pervue at the moment.

    I could use some help, just little questions here and there. I have the form pretty much ready to start using the DB, and I have the DB set up and running with the tables I need (I think). I also have the connection established on my local machine in VS.

    Using Visual Studio .NET 2003 Professional

    MSN Messenger : [email protected]
    Yahoo Messenger: milkmood

    Thanks a bunch.
    Attached Images Attached Images  

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Postgres With VB.NET...

    I haven't a clue about PostGres, but given that it is a database, and I know a little about them... post those queries (no pun intended), see how fast I can prove my total ignorance

    What sort of thing are you looing for? SQL advice, connection advice..?

  3. #3

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Re: Postgres With VB.NET...

    Mostly connection advice and vb syntax for inserting, updating, selecting, etc.

    I have successfully connected to the DB with the built in ODBC Adapter, and it seems to work fine. Just need to figure out how to make the code talk to it.

    CP

  4. #4
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Postgres With VB.NET...

    OK. So is thsi the sort of thing you want, or is this too basic:
    VB Code:
    1. private void GetRecords()
    2.         {
    3.             OdbcConnection conn = new OdbcConnection();
    4.             OdbcCommand cmd = new OdbcCommand("SELECT field1 FROM table", conn);
    5.             OdbcDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    6.             while( dr.Read() )
    7.             {
    8.                 //do stuff here
    9.             }
    10.         }
    11.         private void DoUpdates()
    12.         {
    13.             OdbcConnection conn = new OdbcConnection();
    14.             OdbcCommand cmd = new OdbcCommand("UPDATE table SET field1 = 99", conn);
    15.             cmd.ExecuteNonQuery();
    16.         }
    (Obviously that's C# , because I don't have VB.Net on this machine, but I can translate if necessary...)

  5. #5

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Re: Postgres With VB.NET...

    That helps a some...VB code would be more helpful, but that's a starting point. The comment 'do stuff here' I assume is where my sql statements go, I also assume the sql statements won't be in comment. Is that going to be strictly in sql format, or is there other syntax that needs to be there?

    So that code should be in a button click event like this?

    VB Code:
    1. Private Sub CustOverwriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustOverwriteButton.Click
    2.         MessageBox.Show("Are you sure you want to overwrite this record?" & Environment.NewLine & "Overwriting this record will permantently delete the old customer record.", "Overwrite Record", _
    3.             MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    4.  'Then some 'update' derivation of your code above.
    5.     End Sub
    Then I'll use another similar piece of code in the textbox subs with a 'select' statement to get data, eh?

    CP

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Postgres With VB.NET...

    In VB, it would be something like this:
    VB Code:
    1. private sub GetRecords()
    2.     dim conn as new OdbcConnection(<your connection string>)
    3.     dim cmd as new OdbcCommand("SELECT field1 FROM table", conn)
    4.     dim dr as OdbcDataReader
    5.     dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    6.     'this is looping through your records
    7.     do while( dr.Read() )
    8.       'do stuff here, for example fill a combo box, or an unbound grid etc.
    9.     loop
    10. end sub
    11. private sub DoUpdates(keyValue as string, value as string)
    12.     dim conn as new OdbcConnection(<your connection string>)
    13.     dim cmd as new OdbcCommand(String.Format("UPDATE table SET field1 = '{0}' WHERE keyfield = '{1}'", value, keyValue), conn)
    14.     cmd.ExecuteNonQuery()
    15. end sub

    (I'm assuming that VB.Net has the String.Format(...) method.)

    How you get the data depends really on how you want to use it - you can either return a DataReader or a DataSet. they both have different uses/limitations.

  7. #7

    Thread Starter
    Lively Member milkmood's Avatar
    Join Date
    Mar 2005
    Location
    Forests of Delta Halo
    Posts
    109

    Question Re: Postgres With VB.NET...

    Ok, let's run through this. Like I said, I'm new to all of this (programming AND DB), and it pretty much got dropped in my lap (though I agreed to try). Please forgive my noobiness...

    The database shows up in my Server Explorer, along with 7 tables, and their columns that I created in pgAdminIII. No problem there.

    This is VS Pro, so I know I can use the wizards to set up the data adapter, connections, etc.

    First, what are the differences between the OdbcDataAdapter, OdbcConnection, and the OdbcCommand controls?

    Is there a particular order these have to be configured in? Do I need all three? Do I need multiple instances of any/all of them?

    Second, what's the difference between a DataSet and DataView? Do I need one or both or multiples of each of these?

    Thanks!

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