📄 sqlserverstorageproviderbase.cs
字号:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using ScrewTurn.Wiki.PluginFramework;
namespace ScrewTurn.Wiki.PluginPack {
/// <summary>
/// Implements a base class for SQL Server Providers.
/// </summary>
[Serializable]
public abstract class SqlServerStorageProviderBase {
protected string config;
protected IHost host;
public void Init(IHost host, string config) {
this.host = host;
this.config = config;
if(!ValidateConfig()) throw new Exception("Unable to perform the Test Query, check the Configuration String and the Database.");
if(!IsDatabaseUpToDate()) {
if(!UpdateDatabase()) throw new Exception("Unable to update the Database Schema.");
}
}
protected abstract bool ValidateConfig();
protected abstract bool IsDatabaseUpToDate();
protected abstract bool UpdateDatabase();
/// <summary>
/// Gets a new SQL Connection.
/// </summary>
/// <returns>The SQL Connection.</returns>
protected SqlConnection GetConnection() {
return new SqlConnection(config);
}
/// <summary>
/// Gets a new SQL Command, set with a new SQL Connection.
/// </summary>
/// <returns>The SQL Command.</returns>
protected SqlCommand GetCommand() {
return GetConnection().CreateCommand();
}
/// <summary>
/// Closes the Connection used by a SQL Command.
/// </summary>
/// <param name="cmd">The SQL Command.</param>
protected void Close(SqlCommand cmd) {
try {
cmd.Connection.Close();
}
catch { }
}
/// <summary>
/// Executes the method <b>ExecuteScalar</b> of a SQL Command and returns the result.
/// </summary>
/// <param name="cmd">The SQL Command.</param>
/// <returns>The result, or null.</returns>
/// <remarks>The method automatically opens and then closes the connection.</remarks>
protected object ExecuteScalar(SqlCommand cmd) {
object result = null;
try {
cmd.Connection.Open();
result = cmd.ExecuteScalar();
}
catch(Exception ex) {
result = null;
host.LogEntry(ex.Message, LogEntryType.Error, this);
}
Close(cmd);
return result;
}
/// <summary>
/// Executes the method <b>ExecuteReader</b> of a SQL Command and returns the SQL Data Reader.
/// </summary>
/// <param name="cmd">The SQL Command.</param>
/// <returns>The SQL Data Reader, or null.</returns>
/// <remarks>The method automatically opens the connection but it <b>does not</b> closes it.</remarks>
protected SqlDataReader ExecuteReader(SqlCommand cmd) {
SqlDataReader result = null;
try {
cmd.Connection.Open();
result = cmd.ExecuteReader();
}
catch(Exception ex) {
host.LogEntry(ex.Message, LogEntryType.Error, this);
result = null;
}
return result;
}
/// <summary>
/// Executes the method <b>ExecuteNonQuery</b> of a SQL Command and returns the result.
/// </summary>
/// <param name="cmd">The SQL Command.</param>
/// <returns>The result, or -1.</returns>
/// <remarks>The method automatically opens and then closes the connection.</remarks>
protected int ExecuteNonQuery(SqlCommand cmd) {
int result = -1;
try {
cmd.Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch(Exception ex) {
host.LogEntry(ex.Message, LogEntryType.Error, this);
result = -1;
}
Close(cmd);
return result;
}
public void Shutdown() { }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -