Click here to Skip to main content
15,848,724 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Given the following:

NOTE: I've indicated a solution below because it fixed the problem in this mocked up code below, but for those of you looking at this in the future, my read only properties are valid C#, introduced in C# 6.0.

using System;

public class RBOleDb
{
	public string DbType { get; }
	public string DbDataSource { get; }
	public string DbPath { get; }
	public string DbProvider { get; }


	/// <summary>
	/// Use this for connections to files, such as MS ACCESS, EXCEL, etc.
	/// </summary>
	/// <param name="file_path">Path to Database</param>
	public RBOleDb ( string file_path )
	{
		DbType = System.IO.Path.GetExtension ( file_path ).Replace ( "." , "" ).ToUpper ( );
		DbPath = file_path;
		DbDataSource = file_path;
	}


	public string GetProvider ( )
	{
		string result = "";
		switch ( DbType )
		{
			case "MDF":
				result = "SQLNCLI11";
				break;
			case "MDB":                   // return "Microsoft.Jet.OLEDB.4.0";
			case "ACCDB":
			case "XLSX":
			case "XLS":
			case "DBF":
				result = "Microsoft.ACE.OLEDB.12.0";
				break;
			default:
				result = "!!UNABLE TO DETERMINE PROVIDER!!";
				throw new ArgumentException ( $"DbType has a value of {DbType}, which is not valid.  RBDataOleDb clould not determine what database provider to use when connecting to database" );
		}
		return result;
	}
}

Why doesn't GetProvider() return a value from the switch statement? Since the file extension of the input file is MDB, I'm expecting to get "Microsoft.ACE.OLEDB.12.0" back from this method.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ricks_Class_Testing
{
	class Program
	{
		static void Main ( string[ ] args )
		{
			string mdb = $@"D:\FIRECOMM\Ricks Dallas D Drive\db\COUNTY DATA\statecounty00.MDB";

			RBDataOleDb db = new RBDataOleDb ( mdb );
			Console.WriteLine ( db.DbType );
			Console.WriteLine ( db.DbPath );
			Console.WriteLine ( db.DbProvider );
		}
	}


}
Posted
Updated 19-Jan-18 8:40am
v2

1 solution

So first thing, in your RBOleDb class, your DbType, DbDataSource, DbPath, and DbProvider all lack a setter in the property so i'm not sure how your code even compiles so you'll need to change those from public string DbType { get; } to public string DbType { get; set; }.

Next thing I see, you indicate the problem class is RBOleDb but your usage in your console app shows you using a different class called RBDataOleDb which i suspect is your issue.

I ran your code and it works as is, after I made the previously mentioned compile errors fixes, so I think you've mixed up which class you intended to use in your console app.

This usage

C#
string mdb = @"D:\FIRECOMM\Ricks Dallas D Drive\db\COUNTY DATA\statecounty00.MDB";

            RBOleDb db = new RBOleDb(mdb);
            Console.WriteLine ( db.DbType );
            Console.WriteLine ( db.DbPath );
            Console.WriteLine ( db.DbProvider );

            Console.WriteLine("Result:" + db.GetProvider());


With the mentioned fixes in RBOleDb produce the following output

MDB
D:\FIRECOMM\Ricks Dallas D Drive\db\COUNTY DATA\statecounty00.MDB

Result:Microsoft.ACE.OLEDB.12.0
 
Share this answer
 
Comments
Animesh Datta 15-Jan-18 1:15am    
5!
[no name] 15-Jan-18 10:01am    
Thanks David and Animesh. The getters don't have setters because they're read only and can only be set in the constructor. It's valid, although I may not be using it correctly. I'll correct the typo. I mocked this up to keep it brief, but that won't have anything to do with the switch. When I replaced the switch with IF THEN statements, it ran fine... was wondering why.
David_Wimbley 15-Jan-18 11:20am    
Just a note on the setters, leaving out the setter itself doesn't allow it to just have one magically. If you want it to only work in the constructor then you need to attach the readonly modifier. Would look something like private readonly string DbType {get; private set;} but in this situation your whole property would need to be private as it cannot be decorated readonly and also public.

That, or if you only want the class itself to set the value and nothing outside of it, you can make the setter private. So that would look like public string DbType { get; private set; }

Correcting the class you provided (it had all the compile errors i mentioned) and adjusting your usage example to use RBOleDb instead of RBDataOleDb the switch statement worked fine.

If the switch statement is till not working it sounds like you've got something else going on and need to add break points to your code and start to step through/debug what is going on.
Richard Deeming 16-Jan-18 12:51pm    
Nope - read-only automatic properties without setters have been valid in C# for quite a while now.

C# : The New and Improved C# 6.0[^] - scroll down to "Auto-Property Initializers".
David_Wimbley 16-Jan-18 13:11pm    
I was aware of public string DbType { get; } = "MDB"; but are you saying doing public string DbType { get; }...then later in code DbType = "MDF"; is now valid? I don't see that in the article, honest question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900