代码 /* * 文件名:SQLHelper * 说明:SQL Server帮助类 * 作者:Alexis * 网站:http://www.cnblogs.com/alexis * 创建时间:20100428 * */ using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient;
/// ///SQLHelper 的摘要说明 /// public class SQLHelper { SqlConnection conn;
public SQLHelper() { string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MessageBoard"].ToString(); conn = new SqlConnection(connectionString); }
/// /// 执行SQL命令,将数据赋给数据集的引用 /// public bool RunSQL(string cmdText, ref DataTable dt) { try { conn.Open(); SqlCommand cmd = new SqlCommand(cmdText, conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds1 = new DataSet(); sda.Fill(ds1); dt = ds1.Tables[0]; } catch (SqlException se) { return false; throw new Exception(se.Message, se); } return true; }
/// /// 执行带参数的SQL语句 /// /// /// public bool RunSQL(string cmdText, SqlParameter[] sp) { try { if(conn.State== ConnectionState.Closed) conn.Open(); SqlCommand cmd = new SqlCommand(cmdText, conn); if (sp != null) { for (int i = 0; i < sp.Length; i++) { cmd.Parameters.Add(sp[i]);//将参数加到Command中 } } cmd.ExecuteNonQuery(); } catch (SqlException se) { return false; throw new Exception(se.Message, se);
} finally { conn.Close(); } return true;
}
public DataTable getDataTable(string cmdText) { DataTable dt = null; try { if (conn.State == ConnectionState.Closed) conn.Open(); SqlCommand cmd = new SqlCommand(cmdText, conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); dt = ds.Tables[0]; } catch (SqlException se) { throw new Exception(se.Message, se); } finally { conn.Close(); } return dt; } }
6. 创建数据库对象的实体类,也是十分简单的,就是对应数据库的字段;
代码 /* * 文件名:Message * 说明:Message实体类 * 作者:Alexis * 网站:http://www.cnblogs.com/alexis * 创建时间:20100428 * */ using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace MessageBoard { /// /// 留言类 /// public class Message { private int id;//留言的标识
public int Id { get { return id; } set { id = value; } }
private string msg_content;//留言的内容
public string Msg_content { get { return msg_content; } set { msg_content = value; } }
private string msg_nickname;// 昵称
public string Msg_nickname { get { return msg_nickname; } set { msg_nickname = value; } }
private string msg_face;//选择的头像
public string Msg_face { get { return msg_face; } set { msg_face = value; } }
private DateTime msg_time;//留言的时间
public DateTime Msg_time { get { return msg_time; } set { msg_time = value; } }
代码 /* * 文件名:MessageBoard * 说明:使用Ajax的留言板 * 作者:Alexis * 网站:http://www.cnblogs.com/alexis * 创建时间:20101226 * */ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data;
namespace MessageBoard { public partial class MessageBoard : System.Web.UI.Page { protected DataTable dt;