接着上面,文章相应的DAO逻辑 已经写好了,但我们这时也会发现,少了个执行SQL 的Helper,在这里,我用了早期的PETSHOP的Helper作了个小的改动,如下:
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;

namespace BreakerLib

...{


/**//// <summary>
/// DAOHelper
/// </summary>

public class DAOHelper ...{

private OleDbConnection con ;
public int RunSQL(string strSQL)

...{
strSQL = filterSQL(strSQL);
try

...{
OleDbCommand cmd = CreateCommand(strSQL,null);
cmd.ExecuteNonQuery();
this.Close();
return 0;
}

catch...{
throw new Exception("RunSQL");
}
}
public int RunSQL(string strSQL, OleDbParameter[] prams)

...{
strSQL = filterSQL(strSQL);
try

...{
OleDbCommand cmd = CreateCommand(strSQL, prams);
cmd.ExecuteNonQuery();
this.Close();
return 0;
}

catch(Exception e)...{
throw new Exception(e.Message);
}
}

public void RunSQL(string strSQL, out OleDbDataReader dataReader)

...{
strSQL = filterSQL(strSQL);
OleDbCommand cmd = CreateCommand(strSQL, null);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}

public void RunSQL(string strSQL, OleDbParameter[] prams, out OleDbDataReader dataReader)

...{
strSQL = filterSQL(strSQL);
OleDbCommand cmd = CreateCommand(strSQL, prams);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public void Open()

...{
if (con == null)

...{
string strConnect;
// strConnect =
// @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:帮联文件ProjectsKarstBreakerSimplyDataBaseBreakerSimply.mdb";
strConnect = ConfigurationSettings.AppSettings["DBConnectionString"];
con = new OleDbConnection(strConnect);
con.Open();
}
}
public void Close()

...{
if (con != null)
con.Close();
}
public OleDbCommand CreateCommand(string strSQL, OleDbParameter[] prams)

...{
Open();
strSQL = filterSQL(strSQL);
OleDbCommand cmd = new OleDbCommand(strSQL, con);
cmd.CommandType = CommandType.Text;
if (prams != null)

...{
foreach (OleDbParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
return cmd;
}
public OleDbParameter MakeInParam(string ParamName, OleDbType DbType, int Size, object Value)

...{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}

public OleDbParameter MakeParam(string ParamName, OleDbType DbType, Int32 Size, ParameterDirection Direction, object Value)

...{
OleDbParameter param;

if(Size > 0)
param = new OleDbParameter(ParamName, DbType, Size);
else
param = new OleDbParameter(ParamName, DbType);

param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;

return param;
}

/**//// <summary>
/// ????
/// </summary>
public void Dispose()

...{
// ????Connection
if (con != null)

...{
con.Dispose();
con = null;
}
}
public string filterSQL(string strSQL)

...{
strSQL = strSQL.Replace("'", " ");
return strSQL;
}
}
}

























































































































































