1 Replies - 270 Views - Last Post: 01 December 2012 - 10:11 AM Rate Topic: -----

#1 GolDRoger  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-August 12

Connectingto ms sql database using oledb

Posted 01 December 2012 - 10:09 AM

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 :

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();
        
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Connectingto ms sql database using oledb

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8891
  • View blog
  • Posts: 33,338
  • Joined: 12-June 08

Re: Connectingto ms sql database using oledb

Posted 01 December 2012 - 10:11 AM

You need tobe opening it after line 48.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1