一、在DAL項(xiàng)目中添加類:categoryDAO.CS
1)添加新建項(xiàng)
2)輸入類名(categoryDAO.cs),選擇“類”,點(diǎn)擊“添加”
3)categoryDAO.cs源碼如下:(感興趣的話復(fù)制下來慢慢研究)
/*創(chuàng)建人:無聲歲月,創(chuàng)建時(shí)間:2009.09.04,9:50,類說明:增加新聞?lì)悇e,版權(quán)所有:無聲歲月*/
using System;
using System.Collections.Generic;
using System.Text;
namespace DAL
{
/// <summary>
/// 新聞?lì)悇e表操作類
/// </summary>
public class categoryDAO
{
private SQLHelper salhelper = null;
public categoryDAO()
{
salhelper = new SQLHelper();//構(gòu)造函數(shù)并初始化成SQLHelper類實(shí)例,目的是想調(diào)用SQLHelper中的ExecuteNonQuery(sql)來執(zhí)行插入記錄的SQL語句
}
/// <summary>
/// 增加新聞?lì)悇e方法
/// </summary>
/// <param name="caname">新聞?lì)悇e名</param>
/// <returns>返回受影響的記錄數(shù)</returns>
public bool Insert(String caName)
{
bool flag = false;
string sql = "insert into category(name) values ('"+caName+"')";
int res = salhelper.ExecuteNonQuery(sql);//調(diào)用SQLHelper中的ExecuteNonQuery(sql)來執(zhí)行插入記錄的SQL語句,返回受影響的記錄數(shù)
if (res > 0)
{
flag = true;
}
else
{
flag = false;
}
return flag;
}
}
}
小析:這個(gè)類的作用是:當(dāng)我們從文本框輸入“新聞?lì)悇e名”提交后,就會(huì)執(zhí)行插入該記錄操作。
ExecuteNonQuery(sql)方法是在SQLHelper.cs中,SQLHelper.cs源碼如下:
/*制作人:無聲歲月,制作時(shí)間:2009-09-3,版權(quán)所有:無聲歲月,說明:數(shù)據(jù)庫助手類*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLHelper
{
/// <summary>
/// 此方法執(zhí)行傳入增、刪、改的SQL語句
/// </summary>
/// <param name="sql">將要執(zhí)行增、刪、改的SQL語句</param>
/// <returns>更新的記錄數(shù)</returns>
public int ExecuteNonQuery(string sql)
{
string connstr = @"server=wei/mysqlexpress;database=newsystem;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connstr);//創(chuàng)建庫連接對象
conn.Open();//打開連接的數(shù)據(jù)庫
SqlCommand cmd = new SqlCommand(sql, conn);//創(chuàng)建可以執(zhí)行SQL語句的command實(shí)例
int res = cmd.ExecuteNonQuery();//執(zhí)行無返回行操作,返回受影響的記錄數(shù)
conn.Close();//關(guān)閉數(shù)據(jù)庫連接,以釋放資源
return res;//把受影響的記錄數(shù)返回給ExecuteNonQuery
}
}
}
4)在設(shè)計(jì)頁面拖入一個(gè)文本框和一個(gè)按鈕:
5)雙擊按鈕事件調(diào)用代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string caName = TextBox1.Text;
if (new categoryDAO().Insert(caName))
{
Response.Write("新聞?lì)悇e名:("+ caName +")" +"已成功添加到新聞?lì)悇e表category中!");
}
else
{
Response.Write("新聞?lì)悇e名:("+ caName +")" +"未能成功添加到新聞?lì)悇e表category中!");
}
}
}
6)重新生成一下,按F5或CTRL+F5運(yùn)行一下,為了見效果,我們查看運(yùn)行前數(shù)據(jù)表的記錄情況:
運(yùn)行的初始界面:(如果調(diào)試出錯(cuò)可將//后的注釋刪了再調(diào)試)
現(xiàn)在我往文本框中輸入“花邊新聞”,然后點(diǎn)擊提交:
再看看我們的數(shù)據(jù)表是不是真的新添加了一條新聞?lì)悇e記錄?:
有了,添加任務(wù)完成,這個(gè)例子的難點(diǎn)就是對構(gòu)造函數(shù)的理解,構(gòu)造函數(shù)是在當(dāng)我們對類創(chuàng)建實(shí)例時(shí)才自動(dòng)初始化的。下來看看如何編碼實(shí)現(xiàn)根據(jù)“新聞?lì)悇e名”對表中進(jìn)行查詢:
SQLHelper.cs源碼如下:(新建了Excutequery()方法來執(zhí)行傳入的SQL查詢語句)
/*制作人:無聲歲月,制作時(shí)間:2009-09-3,版權(quán)所有:無聲歲月,說明:數(shù)據(jù)庫助手類*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public class SQLHelper
{
/// <summary>
/// 此方法執(zhí)行傳入增、刪、改的SQL語句
/// </summary>
/// <param name="sql">將要執(zhí)行增、刪、改的SQL語句</param>
/// <returns>更新的記錄數(shù)</returns>
public int ExecuteNonQuery(string sql)
{
string connstr = @"server=wei/mysqlexpress;database=newsystem;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
int res = cmd.ExecuteNonQuery();
conn.Close();
return res;
}
/// <summary>
/// 該方法執(zhí)行SQL查詢語句
/// </summary>
/// <param name="sql">查詢的SQL語句</param>
/// <returns>存放在內(nèi)存表中的查詢結(jié)果集(bool)</returns>
public DataTable ExecuteQuery(string sql)
{
DataTable dt = new DataTable();//創(chuàng)建一個(gè)表實(shí)例DT
string connstr = @"server=wei/mysqlexpress;database=newsystem;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
rd.Close();
conn.Close();
return dt;
}
}
}
categoryDAO.cs源碼如下:(新建了一個(gè)方法:IsExists()來接收從文本框輸入的新聞?lì)悇e名并查詢)
/*創(chuàng)建人:無聲歲月,創(chuàng)建時(shí)間:2009.09.04,9:50,類說明:增加新聞?lì)悇e,版權(quán)所有:無聲歲月*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace DAL
{
/// <summary>
/// 新聞?lì)悇e表操作類
/// </summary>
public class categoryDAO
{
private SQLHelper salhelper = null;
public categoryDAO()
{
salhelper = new SQLHelper();
}
/// <summary>
/// 增加新聞?lì)悇e方法
/// </summary>
/// <param name="caname">新聞?lì)悇e名</param>
/// <returns>返回受影響的記錄數(shù)</returns>
public bool Insert(String caName)
{
bool flag = false;
string sql = "insert into category(name) values ('" + caName + "')";
int res = salhelper.ExecuteNonQuery(sql);
if (res > 0)
{
flag = true;
}
else
{
flag = false;
}
return flag;
}
/// <summary>
/// 判斷新聞?lì)悇e名是否存在
/// </summary>
/// <param name="caName">傳入的新聞?lì)悇e名</param>
/// <returns>如果存在返回真,否則返回假</returns>
public bool IsExists(string caName)
{
bool flag = false;
string sql = "select * from category where [name]='" + caName + "'";
DataTable dt = salhelper.ExecuteQuery(sql);
if (dt.Rows.Count>0)
{
flag = true;
}
else
{
flag = false;
}
return flag;
}
}
}
按CTRL+F5測試
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
