📄 genericdataaccess.cs
字号:
using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// GenericDataAccess 的摘要说明
/// </summary>
public class GenericDataAccess
{
public GenericDataAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region
// execute a command and return the results as a DataTable object
public static DataTable ExecuteSelectCommand(DbCommand command)
{
DataTable tabel=null;
try
{
command.Connection.Open();
DbDataReader reader = command.ExecuteReader();
tabel = new DataTable();
tabel.Load(reader);
reader.Close();
}
catch(Exception ex)
{
Utilities.LogError(ex);
}
finally
{
command.Connection.Close();
}
return tabel;
}
#endregion
#region
// execute an update, delete, or insert command
// and return the number of affected rows
public static int ExecuteNonQuery(DbCommand command)
{
// The number of affected rows
int affectedRows = -1;
// Execute the command making sure the connection gets closed in the end
try
{
// Open the connection of the command
command.Connection.Open();
// Execute the command and get the number of affected rows
affectedRows = command.ExecuteNonQuery();
}
catch (Exception ex)
{
// Log eventual errors and rethrow them
Utilities.LogError(ex);
throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
// return the number of affected rows
return affectedRows;
}
#endregion
#region
// execute a select command and return a single result as a string
public static string ExecuteScalar(DbCommand command)
{
// The value to be returned
string value = "";
// Execute the command making sure the connection gets closed in the end
try
{
// Open the connection of the command
command.Connection.Open();
// Execute the command and get the number of affected rows
value = command.ExecuteScalar().ToString();
}
catch (Exception ex)
{
// Log eventual errors and rethrow them
Utilities.LogError(ex);
throw ex;
}
finally
{
// Close the connection
command.Connection.Close();
}
// return the result
return value;
}
#endregion
#region
public static DbCommand CreatCommand()
{
string dbProviderName = BalloonShopConfiguration.DbProviderName;
string connectionString = BalloonShopConfiguration.DbConnectionString;
DbProviderFactory factory = DbProviderFactories.GetFactory(dbProviderName);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connectionString;
DbCommand comm = conn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
return comm;
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -