Like Tree1Likes
  • 1 Post By nvoigt

Oledb Connection(Completely Different from first post)

This is a discussion on Oledb Connection(Completely Different from first post) within the C# Programming forums, part of the General Programming Boards category; i wrote this bit of c# code to go into the database and fetch a username but it keeps on ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Oledb Connection(Completely Different from first post)

    i wrote this bit of c# code to go into the database and fetch a username but it keeps on saying the connection is not open but i opened it so i do not understand. This code is written within a class which i call i the main :

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    
    namespace ConsoleApplication1
    {
        class @dbconnection
        {
            private OleDbConnection conn = new OleDbConnection();
    
            void con()
            {
                conn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost\\sqlexpress;Integrated Security=SSPI;Initial Catalog=ecg_projectDB2";
                conn.Open();
            }
    
             public OleDbConnection connection
             {
                get
                {
                     return conn;
                }              
             }      
        }
    
        class @test
        {
            private static string user;
            public static string username
            {
                get
                {
                    return user;
                }
                set
                {
                    user = value;
                }
            }
    
            public void getname()
            {
    
                OleDbCommand cmd = new OleDbCommand();
                dbconnection db = new dbconnection();
                cmd.Connection = db.connection;
    
                cmd.CommandText = "select * from ecg_projectDB2.dbo.adminusers where password = 'user'";
                cmd.CommandType = CommandType.Text;
                
                OleDbDataReader dr =  default(OleDbDataReader);
                
                dr = cmd.ExecuteReader();
                if(dr.Read())
                {
                    username = (string)dr["username"];
                    Console.Write(username);
                }
                dr.Dispose();
                dr.Close();
           
            }
        }
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Maybe you start with structuring your program so you will understand it's flow. Have a look at my previous example. This is half a mess of a program. I don't see any main and even you don't know what gets called when.

    Set a breakpoint to conn.Open() or put a Console.WriteLine in there. It will probably never be reached. So your compiler is right. The connection never gets opened.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Yeah Sorry, this is just the class file not the main and for you saying it's messy, this is how i was taught in vb and i'm trying to rewrite the code and apply some of the same techniques in c#, plus i don't know any other method so if you have a better way of doing it i'd really appreciate it.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I'll take a wild guess and say this is not what you were taught. Because even VB people aren't that weird. You have different classes, properties, functions and yet your problem is with neither of those. So tackle your problem. Scrap all that classes, properties and functions and write one single function to test your database access. I posted an example of a very clear and easy database access in your last thread. If it's not exactly what you need, change the SQL statement until it fits your problem.
    itsme86 likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OleDB connection
    By GolDRoger in forum C# Programming
    Replies: 10
    Last Post: 12-01-2012, 10:46 AM
  2. Using OleDb in C#?
    By jcafaro10 in forum C# Programming
    Replies: 10
    Last Post: 06-29-2009, 10:28 AM
  3. OLEDB Provider Properties
    By IfYouSaySo in forum Windows Programming
    Replies: 0
    Last Post: 04-26-2006, 12:26 AM
  4. Writing OLEDB Provider
    By IfYouSaySo in forum Windows Programming
    Replies: 0
    Last Post: 04-06-2006, 04:55 PM
  5. OLEDB Connection String to Excel
    By gozlan in forum C# Programming
    Replies: 4
    Last Post: 07-06-2003, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21