📄 database.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace EquipmentMS.BaseClass
{
class DataBase:IDisposable
{
private SqlConnection con;
#region open data link
/// <summary>
/// open data link
/// </summary>
private void Open()
{
// open data link
if (con == null)
{
con = new SqlConnection("Data Source=(local);DataBase=db_EquipmentMS;User ID=sa;PWD=");
}
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
}
#endregion
#region close data link
/// <summary>
/// close data link
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
#endregion
#region release source of data link
/// <summary>
/// release source
/// </summary>
public void Dispose()
{
// confirm
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
#region transfer parameter
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
#endregion
#region execution order(no return value)
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
public int RunProc(string procName)
{
this.Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.ExecuteNonQuery();
this.Close();
return 1;
}
#endregion
#region execution order (have return value)
public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName)
{
SqlDataAdapter dap = CreateDataAdaper(procName, prams);
DataSet ds = new DataSet();
dap.Fill(ds, tbName);
this.Close();
return ds;
}
public DataSet RunProcReturn(string procName, string tbName)
{
SqlDataAdapter dap = CreateDataAdaper(procName, null);
DataSet ds = new DataSet();
dap.Fill(ds, tbName);
this.Close();
return ds;
}
#endregion
#region add information to SqlDataAdapter
private SqlDataAdapter CreateDataAdaper(string procName, SqlParameter[] prams)
{
this.Open();
SqlDataAdapter dap = new SqlDataAdapter(procName, con);
dap.SelectCommand.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
dap.SelectCommand.Parameters.Add(parameter);
}
dap.SelectCommand.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return dap;
}
#endregion
#region add information to SqlCommand
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
this.Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.Text;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -