第四周学习笔记——SqlHelper

                                                   SqlHelper类

第四周学习笔记——SqlHelper

 

SqlHelper,SQL助手,可以将许多重复的代码进行封装,后面使用时只需要调用它即可,避免了代码出现大量重复的繁杂情况。

SqlHelper 类中还包含一些专用函数,用于管理参数和准备要执行的命令。不管客户端调用什么样的方法实现,所有命令都通过 SqlCommand 对象来执行。在 SqlCommand 对象能够被执行之前,所有参数都必须添加到 Parameters 集合中,并且必须正确设置 Connection、CommandType、CommandText 和 Transaction 属性。

SqlHelper 类中的专用实用程序函数:

AttachParameters:该函数用于将所有必要的 SqlParameter 对象连接到正在运行的 SqlCommand。

AssignParameterValues:用于为 SqlParameter 对象赋值。

PrepareCommand:用于对命令的属性进行初始化。

ExecuteReader:用ExecuteReader 实现通过适当的CommandBehavior打开SqlDataReader对象,以便最有效地管理与阅读器关联的连接的有效期。

1.SqlHelper实例:

 <connectionStrings>
    <add name="sql" connectionString="server=(local);database=Big_HomeWork;Integrated Security=sspi;"/>
  </connectionStrings>

2.在程序下添加类SqlHelper,并添加代码:

   public class SqlHelper
    {
        private static SqlCommand GetCommand(string commandText,  SqlParameter[] sqlParameters)
        {
            SqlConnection sqlConnection = new SqlConnection();                                              //声明并实例化SQL连接;
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Sql"].ToString();      //配置管理器从App.config读取连接字符串;
            SqlCommand sqlCommand = sqlConnection.CreateCommand();                                          //调用SQL连接的方法CreateCommand来创建SQL命令;该SQL命令将绑定SQL连接;
            sqlCommand.CommandText = commandText;                                                           //指定SQL命令的命令文本;
           
            if (sqlParameters != null)                                                                      //若SQL参数数组非空;
            {
                sqlCommand.Parameters.AddRange(sqlParameters);                                              //将SQL参数数组内的所有SQL参数,批量添加至SQL命令的参数集合;
            }
            return sqlCommand;                                                                              //返回SQL命令;
        }

        public static object Scalar(string commandText, SqlParameter[] sqlParameters)
        {
            object result = null;                                                                           //查询结果可能类型多样,故先声明对象,并指向空值;
            using (SqlCommand sqlCommand = GetCommand(commandText, sqlParameters))       //调用SQL助手的静态方法GetCommand来创建SQL命令,并定义其作用范围;
            {
                sqlCommand.Connection.Open();                                                               //打开SQL命令的连接;
                result = sqlCommand.ExecuteScalar();                                                        //调用SQL命令的方法ExecuteScalar来执行命令,并返回单个结果(即标量);
                sqlCommand.Connection.Close();                                                              //关闭SQL连接;
            
            return result;                                                                                  //返回查询结果;
        }       
        
    }

3.窗体下代码:

   public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void butt-login_Click(object sender, EventArgs e)
        {
            string commandText = "Select *from Personal Information where [email protected] AND [email protected]";                                         //指定SQL命令的命令文本;命令文本为存储过程名称;
            SqlParameter[] sqlParameters =                                                      //声明SQL参数数组;
                { new SqlParameter("@p-name", Tx-name.Text )                                              //实例化SQL参数,并在构造函数中指定参数的名称、值,以作为SQL参数数组的元素;
                , new SqlParameter("@p-passcode", Tx-passcode.Text) };
            int count = (int)SqlHelper.Scalar(commandText, sqlParameters);                                                                         //声明整型变量,用于保存与输入的用户相匹配的记录个数;
                                       
                                                                                  //返回个数;
        }
        
}

运行结果如下:

 

第四周学习笔记——SqlHelper