DataReader和DateSet区别:
1. DataReader是一行一行的读,且只能向前读。DateSet是一次性读取出来放到内存中,所以,DataReader读取速度更快,占用内存更低。
2. DataReader需要一直和数据库保持连接,DataSet读取完后即和数据库断开。
3. DataSet里面可以储存多个结果集(DataTable)
4. DataSet可以对数据进行缓存,而DataReader不行。
SqlHelper
一:SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
二:SqlHelper做登录效果
protected void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text.Trim();
string pwd = txtPwd.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd))
{
Response.Write("<script>alert(‘用户名或者密码不能为空‘);</script>");
}
else
{
try
{
string strSql = "select UserId from UserInfor where [email protected] and [email protected]";
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@UserName", SqlDbType.NVarChar, 50),
new SqlParameter("@Pwd", SqlDbType.NVarChar, 50) };
para[0].Value = username;
para[1].Value = pwd;
if (SqlHelper.Exists(strSql, para))
//判断sql语句里面的结果是否存在,两种重置方式,第一种传sql语句,第二种传para可以进行sql注入处理
{
Response.Redirect("RNewsM.aspx");
}
else
{
Response.Write("<script>alert(‘用户名或者密码错误‘);</script>");
}
}
catch(Exception ex)
{
//Response.Write("网页正在维修中....");
MessageBox.Show(ex.Message); //显示异常信息
}
}
}
三:如何去获取结果集
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRNews();
}
}
public void BindRNews()
{
StringBuilder sb = new StringBuilder();
sb.Append("select NewsId,Title,CreatedTime,NewsClass,ViewCount from RNews where 1=1");
SqlParameter[] para = new SqlParameter[2];
if (!string.IsNullOrEmpty(txtSRNewsId.Text.Trim()))
{
sb.Append(" and [email protected]");
para[0] = new SqlParameter("@NewsId", SqlDbType.Int);
para[0].Value = Convert.ToInt32(txtSRNewsId.Text.Trim());
}
if (ddlSNewsClass.SelectedIndex > 0)
{
sb.Append(" and [email protected]");
para[1] = new SqlParameter("@NewsClass", SqlDbType.NVarChar, 50);
para[1].Value = ddlSNewsClass.SelectedValue;
}
SqlDataReader read = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, sb.ToString(), para);
//DataTable dt = SqlHelper.ExecuteDataSetText(sb.ToString(), para).Tables[0];
gvRNews.DataSource = read;
//gvRNews.DataSource = dt;
gvRNews.DataBind();
}
四:删除数据
protected void btnDel_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtDNewsId.Text.Trim()))
{
Response.Write("<script>alert(‘新闻ID不能为空‘);</script>");
}
else
{
int newsid = Convert.ToInt32(txtDNewsId.Text.Trim());
string strSql = "delete RNews where [email protected]";
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@NewsId", SqlDbType.Int) };
para[0].Value = newsid;
if (SqlHelper.ExecteNonQueryText(strSql, para) > 0)
{
Response.Write("<script>alert(‘删除成功‘);</script>");
BindRNews();
}
}
五:获取详细数据
public partial class NewsPage : System.Web.UI.Page
{
private int _newsid;
public int NewsId
{
get
{
try
{
_newsid = Request.QueryString["newsid"] == null ? 0 : Convert.ToInt32(Request.QueryString["newsid"].ToString());
}
catch
{
_newsid = 0;
}
return _newsid;
}
set { _newsid = value; }
}
public string GetNews()
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<table id=""tab"">");
if (NewsId > 0)
{
string strSql = "select * from RNews where [email protected]";
SqlParameter[] para = new SqlParameter[] { new SqlParameter("@NewsId", SqlDbType.Int) };
para[0].Value = NewsId;
DataTable dt = SqlHelper.ExecuteDataSetText(strSql, para).Tables[0];
if (dt.Rows.Count > 0)
{
sb.Append(string.Format("<tr><td>标题:</td><td>{0}</td></tr>", dt.Rows[0]["Title"].ToString()));
sb.Append(string.Format("<tr><td>内容:</td><td>{0}</td></tr>", dt.Rows[0]["Text"].ToString()));
sb.Append(string.Format("<tr><td>时间:</td><td>{0}</td></tr>", dt.Rows[0]["CreatedTime"].ToString()));
sb.Append(string.Format("<tr><td>分类:</td><td>{0}</td></tr>", dt.Rows[0]["NewsClass"].ToString()));
sb.Append(string.Format("<tr><td>浏览数:</td><td>{0}</td></tr>", dt.Rows[0]["ViewCount"].ToString()));
}
或者使用SqlDataReader
///
SqlDataReader read = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, strSql, para);
if (read.HasRows)
{
while (read.Read()) //用if语句
{
sb.Append(string.Format("<tr><td>标题:</td><td>{0}</td></tr>", read["Title"].ToString()));
sb.Append(string.Format("<tr><td>内容:</td><td>{0}</td></tr>", read["Text"].ToString()));
sb.Append(string.Format("<tr><td>时间:</td><td>{0}</td></tr>", read["CreatedTime"].ToString()));
sb.Append(string.Format("<tr><td>分类:</td><td>{0}</td></tr>", read["NewsClass"].ToString()));
sb.Append(string.Format("<tr><td>浏览数:</td><td>{0}</td></tr>", read["ViewCount"].ToString()));
}
}
///
}
sb.Append("</table>");
return sb.ToString();
}
}