C# 数据库 db = DatabaseFactory.CreateDatabase() 失败;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13993628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failing at Database db = DatabaseFactory.CreateDatabase();
提问by MrS1ck
Failing over around Database db = DatabaseFactory.CreateDatabase();
and I'm not sure why. I executed the stored proc with manual parameters declared and she worked just fine. Though, I'm not being given an error or anything to go off of, so I'm not sure whats wrong. I stick a break in the code on each line leading up to, and after the Database db = DatabaseFactory.CreateDatabase();
line and it stops there, after I tell the IDE to continue it skips that line and the function ends.
失败了Database db = DatabaseFactory.CreateDatabase();
,我不知道为什么。我用声明的手动参数执行了存储过程,她工作得很好。不过,我没有收到错误或任何要处理的信息,所以我不确定出了什么问题。我在每一行的代码中插入一个中断,然后在该Database db = DatabaseFactory.CreateDatabase();
行之后停止,在我告诉 IDE 继续之后,它会跳过该行并且函数结束。
For the record, my references are solid as well. I've cannibalized this code from another project I've done, so I'm likely just missing something silly. Regardless, here's the code:
作为记录,我的参考资料也很可靠。我已经从我做过的另一个项目中蚕食了这段代码,所以我可能只是遗漏了一些愚蠢的东西。无论如何,这是代码:
public class PullDebtor
{
public static DataTable ClientNumber(string strClientID)
{
DataSet dsDebtor = new DataSet();
dsDebtor.Tables.Add("Debtors");
DbCommand dbCommand = null;
try
{
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "sp_PullClientID";
DbCommand dbCommand1 = db.GetSqlStringCommand(sqlCommand);
dbCommand1.CommandType = CommandType.StoredProcedure;
db.AddInParameter(dbCommand1, "@ClientID", DbType.String, strClientID);
db.ExecuteNonQuery(dbCommand1);
dsDebtor = db.ExecuteDataSet(dbCommand1);
}
catch
{
return dsDebtor.Tables[0];
}
finally
{
}
return dsDebtor.Tables[0];
}
}
}
采纳答案by Alex Filipovici
Have you edited your .config
sections? You need somethig like:
你编辑过你的.config
部分吗?你需要这样的东西:
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
And you also need a config section pointing to the connection string you have defined in your connection strings section:
而且您还需要一个配置部分,指向您在连接字符串部分中定义的连接字符串:
<dataConfiguration defaultDatabase="Connection String" />
回答by Austin Salonen
You might know why had you not ignored the exception. catch { ... }
is rarelya good idea (it's effectively never a good idea but I try to avoid always/never when giving recommendations).
您可能知道为什么不忽略异常。catch { ... }
是很少一个好主意(这是有效从来就不是一个好主意,但我试着给的建议时避免经常/从不)。
Modify your code to something like this and see if you still need help:
把你的代码修改成这样,看看你是否还需要帮助:
try
{
Database db = DatabaseFactory.CreateDatabase();
...
}
catch(Exception ex)
{
// this will dump to the output window in VS when running a Debug build.
// Release logging will require something different
System.Diagnostics.Debug.WriteLine(ex);
...
}