1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Visual Studio DB Connection

Discussion in 'HTML, Graphics & Programming' started by aix0, 23 Apr 2010.

  1. aix0

    Hitman

    Joined: 16 Mar 2005

    Posts: 708

    Location: Staffordshire

    Hi

    I'm not a dev by any stretch of the imagination, but I do like to dabble occasionally. I'm trying to use Visual Studio to connect to a PostgreSQL database, but I'm having a few issues.

    It's the first time I've used VS and I like to learn as I go along, I find that's often the best method to learn. I want to build a Windows application that first start by authenticating a user by querying the PostgreSQL DB, but I cannot figure out how to do it. Do I use the DataSet wizard? Or should I code it manually? I have an ODBC connection setup in Windows, but I can't for the life of me figure out how to query the DB from within the application's form.

    Basically, I have 2 field at the moment, and a button. I want the application to execute a query when the user clicks the button and verify the username and password by querying the DB. Do I open a DB connection when the button is clicked? Or is the DB connection persistent throughout runtime?

    Please give me a clue!

    Thanks
     
  2. wozencl

    Hitman

    Joined: 27 Jul 2009

    Posts: 590

    Location: UK

    C#
    using System.Data.Odbc
    OdbcConnection cn = new OdbcConnection("DSN=YOUR_DSN_NAME");

    VB.net
    Dim cn as OdbcConnection
    cn = new OdbcConnection("DSN=YOUR_DSN_NAME")

    You then use cn.Open() to attempt to connect.

    I am guessing you'll want to do something like the below once the button is pressed.

    Connect to Datasource

    If connected successfully execute query. e.g.

    SELECT * FROM tableUsers WHERE USERNAME=" & userText & " AND PASSWORD= " & textPass

    * Assuming password is plain-text

    Any results returned? If not let them try again once or twice then disconnect them and close connection.

    If you search Google for "ODBC VB.net" or "ODBC C#" you'll find plenty of examples.