SqlHelper 2

SqlHelper 类:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace ADOTesting.DataHelper
{
    public class SqlHelper
    {
        //private string _connString;
        public string ConnString
        {
            get { return WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString; }
            //set
            //{
               
            //    this._connString = WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
            //    _connString = value;
            //}
        }

        public static void PrepareCommand(SqlConnection connection,SqlCommand cmd,SqlTransaction transaction,CommandType cmdType,string cmdText,SqlParameter[] cmdParms)
        {
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            cmd.Connection = connection;
            cmd.CommandText = cmdText;
            if (transaction != null)
            {
                cmd.Transaction = transaction;
            }
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                foreach (var sqlParameter in cmdParms)
                {
                    cmd.Parameters.Add(sqlParameter);
                }
            }
        }

        public SqlConnection SqlConnection()
        {
           
                return new SqlConnection(this.ConnString);
           
        }

        public static int ExcuteNoneQuery(SqlConnection connection,CommandType cmdType,string cmdText,SqlParameter[] parameters)
        {
            SqlCommand cmd=new SqlCommand();
            PrepareCommand(connection,cmd,null,cmdType,cmdText,parameters);
            var i = cmd.ExecuteNonQuery();
            //var i=cmd.e
            cmd.Parameters.Clear();
            return i;
        }

        public static object ExcuteScalar(string connString,CommandType cmdType, string cmdText,
            SqlParameter[] parameters)
        {
            SqlCommand cmd=new SqlCommand();
            using (SqlConnection connection = new SqlConnection(connString))
            {
                PrepareCommand(connection, cmd, null, cmdType, cmdText, parameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return val;    
            }
        }

        public static DataTable ExcuteReader(SqlConnection connection,CommandType cmdType,string cmdText,SqlParameter[] parameters)
        {
            SqlCommand cmd=new SqlCommand();
            DataTable dt=new DataTable();
            PrepareCommand(connection,cmd,null,cmdType,cmdText,parameters);
            //SqlDataReader reader = cmd.ExecuteReader();
            //while (reader.Read())
            //{
                
            //}
            SqlDataAdapter dap=new SqlDataAdapter(cmd);
            dap.Fill(dt);
            return dt;
        }
    }
}

  WebForm.aspx:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADOTesting.DataHelper;

namespace ADOTesting.Pages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cmdText = "Select * From Register";
            //SqlConnection connection=new SqlConnection();
            SqlHelper sqlHelper=new SqlHelper();
            SqlConnection connection = sqlHelper.SqlConnection();
            DataTable dt = SqlHelper.ExcuteReader(connection, CommandType.Text, cmdText, null);
            this.Repeater.DataSource = dt;
            this.Repeater.DataBind();
        }

        protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView rowView = (DataRowView) e.Item.DataItem;
                string firstName = rowView["FirstName"].ToString();
                string lastName = rowView["LastName"].ToString();
                Label firstNameLabel = e.Item.FindControl("FirstName") as Label;
                firstNameLabel.Text = firstName;
                Label lastNameLabel = e.Item.FindControl("LastName") as Label;
                lastNameLabel.Text = lastName;
            }
            //throw new NotImplementedException();
        }
    }
}

  

SqlHelper 2,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。