一.知识点
SqlHelper是一个基于NET Framework的数据库操作组件。组件中包含数据库操作方法。SqlHelper用于简化重复的数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等。
二.思维导图

三.实例
public class SqlHelper
{
private static SqlCommand GetCommand(string commandText, bool isStoredProcedure, SqlParameter[] sqlParameters)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings[“Sql”].ToString();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = commandText;
if (isStoredProcedure)
{
sqlCommand.CommandType = CommandType.StoredProcedure;
}
if (sqlParameters != null)
{
sqlCommand.Parameters.AddRange(sqlParameters);
}
return sqlCommand;
}
public static int ExcuteNonQuery(string commandText, bool isStoredProcedure, SqlParameter[] sqlParameters)
{
int k=0;
using (SqlCommand sqlCommand = GetCommand(commandText, isStoredProcedure, sqlParameters))
{
sqlCommand.Connection.Open();
rowAffected = sqlCommand.ExecuteNonQuery();
sqlCommand.Connection.Close();
}
return k;
}
}
本文介绍了一个基于.NET Framework的数据库操作组件SqlHelper。该组件通过封装常见的数据库操作,如连接(SqlConnection)、命令(SqlCommand)和读取(SqlDataReader),来简化数据库交互过程。使用SqlHelper可以轻松实现SQL命令的执行,并通过提供参数化查询来增强安全性。
330

被折叠的 条评论
为什么被折叠?



