为什么这个开关没有返回值? [英] Why doesn't this switch return a value?
问题描述
鉴于以下内容:
注意: 我已经在下面指出了一个解决方案,因为它修复了这个模拟的问题下面的代码,但对于那些将来看这个的人,我的只读属性是有效的C#,在C#6.0中引入。
使用系统;
公共类RBOleDb
{
public string DbType {get; }
公共字符串DbDataSource {get; }
public string DbPath {get; }
公共字符串DbProvider {get; }
///< summary>
///用于连接文件,例如MS ACCESS,EXCEL等。
///< / summary>
///< param name =file_path>数据库路径< / param>
public RBOleDb(string file_path)
{
DbType = System.IO.Path.GetExtension(file_path).Replace(。,)。ToUpper();
DbPath = file_path;
DbDataSource = file_path;
}
公共字符串GetProvider()
{
string result =;
switch(DbType)
{
caseMDF:
result =SQLNCLI11;
休息;
caseMDB://返回Microsoft.Jet.OLEDB.4.0;
caseACCDB:
caseXLSX:
caseXLS:
caseDBF:
result =Microsoft.ACE.OLEDB.12.0 ;
休息;
默认值:
result =!!无法确定提供商!!;
抛出新的ArgumentException($DbType的值为{DbType},这是无效的.RBDataOleDb无法确定连接数据库时要使用的数据库提供程序);
}
返回结果;
}
}
为什么GetProvider()没有从switch语句返回值?由于输入文件的文件扩展名是MDB,我希望从这种方法中获得Microsoft.ACE.OLEDB.12.0。
我尝试过:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间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);
}
}
}
DbType的值为{DbType},这是无效的.RBDataOleDb无法确定连接到数据库时要使用的数据库提供程序);
}
返回结果;
}
}
为什么GetProvider()没有从switch语句返回值?由于输入文件的文件扩展名是MDB,我希望从这种方法中获得Microsoft.ACE.OLEDB.12.0。
我尝试过:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间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);
}
}
}
首先,在您的RBOleDb
类,您的DbType,DbDataSource,DbPath和DbProvider
都缺少属性中的setter所以我是不确定你的代码是如何编译的,所以你需要从更改那些公共字符串DbType {get; }
到public string DbType {get;组; }
。
接下来我看到,你指出问题类是RBOleDb
但是你在控制台应用程序中的使用显示你使用了另一个名为RBDataOleDb
的类,我怀疑这是你的问题。
我运行了你的代码并且在我之前提到的编译错误修复之后它按原样工作,所以我认为你已经混淆了你打算在你的控制台应用程序中使用哪个类。
此用法
string mdb = < span class =code-string> @ 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( 结果: + db.GetProvider() );
随着RBOleDb中提到的修复产生以下输出
MDB
D:\ FIRECOMM \ Ricks Dallas D Drive\db\COUNTY DATA\statecounty00.MDB
结果:Microsoft.ACE.OLEDB .12.0
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 ); } } }
"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 ); } } }
So first thing, in yourRBOleDb
class, yourDbType, 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 frompublic string DbType { get; }
topublic string DbType { get; set; }
.
Next thing I see, you indicate the problem class isRBOleDb
but your usage in your console app shows you using a different class calledRBDataOleDb
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
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
这篇关于为什么这个开关没有返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!