创建新的 SQL Server CE DB 文件的方法。提出改进建议
我使用以下代码以编程方式创建 SQL Server CE 数据库文件。我正在使用 C# 2008。
/* Create a new SQL Server CE database file programatically */
protected void CreateDB(string _databaseFileName, bool _encryptFile, string _password)
{
if (File.Exists(_databaseFileName))
MessageBox.Show("A file with this name already exists.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
string connectionString = string.Format("DataSource=\"{0}\"; Password='{1}'", _databaseFileName, _password);
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
}
}
我想改进上面的代码。我的查询如下:
此方法包含在受保护的类中。我希望直接调用这些方法而不创建此类的实例。建议一种没有开销的方法。
虽然我传递了一个 bool 来确定用户是否想要加密文件,但没有在连接字符串行中使用。我没有遵循加密需要包含哪些其他参数以及如何包含加密算法。
我想以最少的开销包含异常处理。我想包含“using”,但不遵循如何用此修改上述代码。
I am using following code to create SQL Server CE database file programmatically. I am using C# 2008.
/* Create a new SQL Server CE database file programatically */
protected void CreateDB(string _databaseFileName, bool _encryptFile, string _password)
{
if (File.Exists(_databaseFileName))
MessageBox.Show("A file with this name already exists.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
string connectionString = string.Format("DataSource=\"{0}\"; Password='{1}'", _databaseFileName, _password);
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
}
}
I want to improve the above code. My queries are below:
This method is contained in a protected class. I want these methods to be called directly without creating an instance of this class. Suggest a way with no overhead.
Though I am passing a bool to determine whether the user wants to encrypt file or not, but not used in the connection string line. I am not following which other parameter need to be included for encryption and how to include encryption algorithm.
I want to include exception handling with least overhead. I want to include "using", but not following how to modify the above code with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态
。不知道你说的这个是什么意思。您应该在 else 中捕获
SqlCeException
。另外,您应该将SqlCeEngine
包装在 using 语句中。<前><代码>尝试
{
string connectionString = string.Format("DataSource=\"{0}\"; 密码='{1}'", _databaseFileName, _password);
使用 (SqlCeEngine 引擎 = new SqlCeEngine(connectionString))
{
引擎.CreateDatabase();
}
}
捕获(SqlCeException)
{
}
static
.Not sure what you mean by this one. You should be catching
SqlCeException
in the else. Also, you should wrap theSqlCeEngine
in a using statement.