JSM SqlHelper 2.0 新特性(C#)
本文主要介绍JSM SqlHelper2.0新版本的特性和用法,欢迎大家提出宝贵意见!
JSM SqlHelper2.0新特性
-
继承了原SqlHelper的静态方法模式并加以优化。
-
增强web.config配置支持,以方便网站的日常维护。
-
增加面象对象类,使用SqlHelper对象可以轻松实现复杂的程序逻辑。
-
增加对Access、Oracle、MySql数据库支持。
-
增加TableFramework类,用于实现简单的Insert和Update语句,自动生成参数和Sql语句,减少代码量。
JSM SqlHelper 配置方法
打开web.config文件,配置configuration节点下的configurationSettings中的add项,其中name标识为程序默认的读取链接地址,如果不想在
web.config中读取,可以重写和使用方法传入方式传入链接字符串。web.config配置实例代码如下:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add connectionString="server=.;uid=sa;pwd=***;database=dbname" name="SqlServerHelper"/>
<add connectionString="Data Source=orcl;User Id=system;Password=***;Integrated Security=no" name="OracleHelper"/>
<add connectionString="server=localhost;uid=root;pwd=***;database=mysql_dbname" name="MySqlHelper"/>
</connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
JSM SqlHelper静态实现方法
/// <summary>
/// 读取学生信息
/// </summary>
DataTable ReadStudent(long stid)
{
return SqlServerHelper.ReadTable("select * from [Students] where stid=@stid",
SqlServerHelper.CreateInputParameter("@stid", SqlDbType.BigInt, stid));
}
/// <summary>
/// 删除学生信息
/// </summary>
int DeleteStudent(long stid)
{
return SqlServerHelper.ExecuteNonQuery("delete from [Students] where stid=@stid",
SqlServerHelper.CreateInputParameter("@stid", SqlDbType.BigInt, stid));
}
JSM SqlHelper面象对象实现方法
面象对象方法在复杂的数据库操作过程中,可以实现在一次连接数据库执行多个SQL代码,从而提高程序的执行效率。
执行Sql语句
/// <summary>
/// 读取学生信息
/// </summary>
DataTable ReadStudent(long stid)
{
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.Command.CommandText = "select * from [Students] where stid=@stid";
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
helper.Open();
return helper.ReadTable();
}
}
/// <summary>
/// 删除学生信息 实现事务和执行删除SQL语句
/// </summary>
int DeleteStudent(long stid)
{
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.Open();
//启动事务
SqlTransaction tran = helper.Connection.BeginTransaction();
helper.Command.Transaction = tran;
try
{
helper.Command.CommandText = "delete from [Students] where stid=@stid";
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
int r= helper.ExecuteNoneQuery();
tran.Commit();
return r;
}
catch {
tran.Rollback();
throw;
}
}
}
TableFramework实现数据加简单的插入更新
使用TableFramework可以通过Tameframework添加列和数据值信息,使用InsertTable或UpdateTable方法轻松实SQL语句的生成和参数生成,准确而简单,并且容易日后维护,添加更只需添加一个列值即可。举例代码如下:
/// <summary>
/// 添加学生信息
/// </summary>
bool InsertStudent(string studentName, string className)
{
TableFramework tf = new TableFramework("students");
tf.Add("student_name", studentName);
tf.Add("class", className);
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.Open();
//执行插入新的记录
return helper.InsertTable(tf);
}
}
/// <summary>
/// 更新学生信息
/// </summary>
bool UpdateStudent(long stid,string studentName, string className)
{
TableFramework tf = new TableFramework("students");
tf.Add("student_name",studentName);
tf.Add("class", className);
using (SqlServerHelper helper = new SqlServerHelper())
{
helper.AddParameter("@stid", SqlDbType.BigInt, stid);
helper.Open();
return helper.UpdateTable(tf, "where stid=@stid", false);
}
}
分类:
JSM SqlHelper文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探