复制代码 代码如下:
<appsettings>
<add key="rpsconnection" value="mssql"/>
<add key="damconnection" value="sqlite" />
</appsettings>
<connectionstrings>
<add connectionString="" providerName="System.Data.SQLite.SQLiteConnection;System.Data.SQLite"/>
<add connectionString="" providerName="System.Data.SqlClient.SqlConnection;System.Data" />
</connectionstrings>
复制代码 代码如下:
/// <summary>
/// 数据访问层
/// 透过Dam访问数据库
/// </summary>
public class DALTest
{
/// <summary>
/// 最简单的插入操作
/// Execute会帮你自动创建所对应数据库的连接、执行操作、释放Command、关闭连接、释放链接等
/// </summary>
/// <returns>执行后受影响行数</returns>
public int Insert()
{
return Execute.SExecuteNonQuery("sp_name", null);
//return Execute.SExecuteNonQuery("insert into tablename (..) values (..)", CommandType.Text , null);
}
/// <summary>
/// 带参数的插入操作
/// Execute会帮你自动创建所对应数据库的连接
/// 并帮你把参数类型转换为所对应数据库类型的参数进行数据库操作.
/// </summary>
/// <returns></returns>
public int Insert_Paras()
{
DbParameter[] paras =
{
new MySqlParameter("p1","p1value")
,new SqlParameter("p2","p2value")
};
return Execute.SExecuteNonQuery("sp_name", paras);
}
/// <summary>
/// 带参数的插入操作,并能得到参数返回值
/// Execute会帮你自动创建所对应数据库的连接
/// 并帮你把参数类型转换为所对应数据库类型的参数进行数据库操作.
/// 执行完成您可得到返回值
/// </summary>
/// <returns></returns>
public int Insert_Paras(ref string outputValue)
{
int resultInt = 0;
DbParameter[] paras =
{
new MySqlParameter("p1","p1value")
,new SqlParameter("p2_output","")
{
Direction = ParameterDirection.Output
}
};
resultInt = Execute.SExecuteNonQuery("sp_name", paras);
if (paras[1].Value != null) outputValue = paras[1].Value.ToString();
return resultInt;
}
/// <summary>
/// 带参数的插入操作,并能得到参数返回值,此参数从BLL层传来
///同上...
/// </summary>
/// <returns></returns>
public int Insert_Paras(DbParameter[] paras)
{
return Execute.SExecuteNonQuery("sp_name", paras);
//这样可以在BLL层通过 paras[1]来获取返回参数了
}
/// <summary>
/// 执行同连接多操作的事务处理
/// 这里使用1个带参数的存储过程和一个不带参数的sql语句操作
/// 使用是静态调用方法
/// </summary>
/// <returns></returns>
public bool Insert_Transaction()
{
bool resultBool = false;
DbParameter[] paras =
{
new MySqlParameter("p1","p1value")
,new SqlParameter("p2","p2value")
};
DbConnection connection = null;
DbTransaction transaction = null;
try
{
connection = Factory.CreateConnection();
connection.Open();
transaction = connection.BeginTransaction();
int resultInt1 = Execute.SExecuteNonQuery(connection, transaction, "sp_name1", paras);
int resultInt2 = Execute.SExecuteNonQuery(connection, transaction, "insert into tablename (...) values (...)", null);
if (resultInt1 > 0 && resultInt2 > 0)
{
transaction.Commit();
resultBool = true;
}
else
{
transaction.Rollback();
}
}
catch (Exception)
{
if (transaction != null)
{
transaction.Rollback();
}
}
finally
{
if (transaction != null)
{
transaction.Dispose();
}
if (connection != null)
{
//这里需要手动关闭连接!!
connection.Close();
connection.Dispose();
}
}
return resultBool;
}
/// <summary>
/// 执行同连接多任务操作(不使用事务)
/// 这里我们使用对象方法。这样执行多任务操作可能会稳定点和效率高点
/// </summary>
/// <returns></returns>
public void Insert_much()
{
Execute exec = Execute.NewExecute;
DbConnection connection = null;
try
{
connection = Factory.CreateConnection();
exec.ExecuteNonQuery(connection, null, "sp_name", null);
exec.ExecuteNonQuery(connection, null, "sp_name2", null);
}
finally
{
if (connection != null)
{
//因为调用了 带事务的那个方法(但是我们设置事务为NULL,事务无效)
//所以连接不会自动关闭释放,我们需要手动释放
connection.Close();
connection.Dispose();
}
}
}
}
新闻热点
疑难解答
图片精选