Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Connectingto ms sql database using oledb

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


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>