首页 > 代码库 > SqlHelper 2
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(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。