PetShop中操作数据的方法

本文介绍了PetShop中操作数据的方法,包括SqlHelper.cs整合操作数据方法,阐述定义常量参数可避免重复编译,说明了接口在动态加载程序集的作用,还展示了用abstract抽象类实现工厂方法及Article的增删改查等数据库操作方法。
 题目:PetShop中操作数据的方法

1.:SqlHelper.cs
整合了一些操作数据的方法,节省了时间和空间,简单的就这么讲。

2.:为什么要定义常量参数
有些人奇怪为什么操作数据的方法里为什么这样
1None.gifprivate const string SQL_ARTICLE_INSERT = "INSERT INTO NR_Article([columnid],[sort],[time],[title],[content],[author],[hit],[status]) VALUES(@ColumnID,@Sort,@Time,@Title,@Content,@Author,@Hit,@Status)";
2None.gif
3None.gifprivate const string PARM_ARTICLE_ID = "@ID";
可能奇怪为什么要用PARM_ARTICLE_ID的形式,
原因是编译后只需要调用就可以了,也就是说这部分的代码编译一次以后直接调用就可以了
而如果采用一般的变量的方式,每执行一次就要动态编译一次(不知道这样说有没有问题)

3.:接口的作用
PetShop里实现了几个数据库的调用方式,利用的就是接口然后再利用反射来确定动态加载的程序集

4.在PetShop里用abstract抽象类实现工厂方法
PetShop里用的是接口,但是我们也可以用abstract的形式,参考了CS的代码Provider + SqlProvider

None.gifusing System;
None.gif
using Edot.NewsRelease.Components;
None.gif
using System.Data.SqlClient;
None.gif
using System.Data.SqlTypes;
None.gif
using System.Data;
None.gif
None.gif
namespace Edot.NewsRelease.SqlProvider
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SqlProvider 的摘要说明。
InBlock.gif    
/// 实现数据库操作
ExpandedSubBlockEnd.gif    
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*------------------------------------------------------------------
InBlock.gif     * 结合了SqlHelper进行数据库操作,用操作串进行操作
ExpandedSubBlockEnd.gif     *------------------------------------------------------------------
*/

InBlock.gif    
public class SqlProvider:Provider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string dbOwner = "dbo";
InBlock.gif        
private string connString = null;
InBlock.gif
InBlock.gif        
protected string DBOwner
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return dbOwner;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{dbOwner = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected string ConnString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return connString;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{connString = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public SqlProvider(string strDbOwner,string strConnString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            DBOwner = strDbOwner;
InBlock.gif            ConnString 
= strConnString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SqlConnection GetSqlConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new SqlConnection(ConnString);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
throw new Exception("数据库连接字符串出错,请检查!");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Article操作部分#region Article操作部分
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Article操作数据库语句#region Article操作数据库语句
InBlock.gif        
private const string SQL_ARTICLE_INSERT = "INSERT INTO NR_Article([columnid],[sort],[time],[title],[content],[author],[hit],[status]) VALUES(@ColumnID,@Sort,@Time,@Title,@Content,@Author,@Hit,@Status)";
InBlock.gif        
private const string SQL_ARTICLE_UPDATE = 
InBlock.gif            
"UPDATE NR_Article SET [columnid] = @ColumnID,[sort] = @Sort,[time] = @Time,[title] = @Title,[Content] = @content,[author] = @Author,[hit] = @Hit,[status] = @Status WHERE id = @ID";
InBlock.gif        
private const string SQL_ARTICLE_DELETEBYID = "DELETE FROM NR_Article WHERE [id] = @ID";
InBlock.gif        
private const string SQL_ARTICLE_DELETEBYCOLUMNID = "DELETE FROM NR_Article WHERE [columnid] = @ColumnID";
InBlock.gif        
private const string SQL_ARTICLE_GETARTICLESBYCOLUMNID = "SELECT [id],[sort],[time],[title],[content],[author],[hit],[status] FROM NR_Article WHERE [columnid] = @ColumnID order by [time] desc";
InBlock.gif        
private const string SQL_ARTICLE_GETARTICLEBYID = "SELECT [sort],[time],[title],[columnid],[content],[author],[hit],[status] FROM NR_Article WHERE [id] = @ID order by [time] desc";
InBlock.gif
InBlock.gif        
private const string PARM_ARTICLE_ID = "@ID";
InBlock.gif        
private const string PARM_ATRICLE_COLUMNID = "@ColumnID";
InBlock.gif        
private const string PARM_ATRICLE_SORT = "@Sort";
InBlock.gif        
private const string PARM_ARTICLE_TIME = "@Time";
InBlock.gif        
private const string PARM_ARTICLE_TITLE = "@Title";
InBlock.gif        
private const string PARM_ARTICLE_CONTENT = "@Content";
InBlock.gif        
private const string PARM_ARTICLE_AUTHOR = "@Author";
InBlock.gif        
private const string PARM_ARTICLE_HIT = "@Hit";
InBlock.gif        
private const string PARM_ATRICLE_STATUS = "@Status";
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Article数据库操作方法#region Article数据库操作方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到文章集合
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="id"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override ArticleCollection GetArticlesByColumnID(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter parm 
= new SqlParameter(PARM_ATRICLE_COLUMNID,SqlDbType.Int,4);
InBlock.gif            parm.Value 
= id;
InBlock.gif
InBlock.gif            ArticleCollection articleCollection
= new ArticleCollection();
InBlock.gif            
using(SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.GetConnString(),
InBlock.gif                      CommandType.Text,SQL_ARTICLE_GETARTICLESBYCOLUMNID,parm))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while(dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Article article 
= new Article();
InBlock.gif                    article.ID 
= (int)dr["id"];
InBlock.gif                    article.Sort 
=(int)dr["sort"];                
InBlock.gif                    article.Time 
= Convert.ToDateTime(dr["time"]);
InBlock.gif                    article.Title 
= dr["title"].ToString();
InBlock.gif                    article.Content 
= dr["content"].ToString();
InBlock.gif                    article.Author 
= dr["author"].ToString();
InBlock.gif                    article.Hit 
= (int)dr["hit"];
InBlock.gif                    article.Status 
= Convert.ToInt32(dr["status"]);
InBlock.gif                    articleCollection.Add(article);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return articleCollection;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据ID得到单个文章的数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="id"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public override Article GetArticleByID(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter parm 
= new SqlParameter(PARM_ARTICLE_ID,SqlDbType.Int,4);
InBlock.gif            parm.Value 
= id;
InBlock.gif
InBlock.gif            Article article 
= new Article();
InBlock.gif            
using(SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.GetConnString(),
InBlock.gif                      CommandType.Text,SQL_ARTICLE_GETARTICLEBYID,parm))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    article.Sort 
=(int)dr["sort"];                
InBlock.gif                    article.Time 
= Convert.ToDateTime(dr["time"]);
InBlock.gif                    article.ColumnID 
= Convert.ToInt32(dr["columnid"]);
InBlock.gif                    article.Title 
= dr["title"].ToString();
InBlock.gif                    article.Content 
= dr["content"].ToString();
InBlock.gif                    article.Author 
= dr["author"].ToString();
InBlock.gif                    article.Hit 
= (int)dr["hit"];
InBlock.gif                    article.Status 
= Convert.ToInt32(dr["status"]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return article;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入文章数据
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="article">实例类</param>

InBlock.gif        public override void InsertArticle(Article article)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] parms 
= GetArticleParameters();
InBlock.gif
InBlock.gif            SetArticleParameters(parms,article);
InBlock.gif
InBlock.gif            
using(SqlConnection conn = new SqlConnection(SqlHelper.GetConnString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                
using(SqlTransaction trans = conn.BeginTransaction())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        SqlHelper.ExecuteNonQuery(trans,CommandType.Text,SQL_ARTICLE_INSERT,parms);
InBlock.gif                        trans.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        trans.Rollback();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        conn.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 更新文章数据
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="article"></param>

InBlock.gif        public override void UpdateArticle(Article article)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] parms 
= GetArticleParameters();
InBlock.gif
InBlock.gif            SetArticleParameters(parms,article);
InBlock.gif
InBlock.gif            
using(SqlConnection conn = new SqlConnection(SqlHelper.GetConnString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                
using(SqlTransaction trans = conn.BeginTransaction())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        SqlHelper.ExecuteNonQuery(trans,CommandType.Text,SQL_ARTICLE_UPDATE,parms);
InBlock.gif                        trans.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        trans.Rollback();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        conn.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除单个文章
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="id"></param>

InBlock.gif        public override void DeleteArticle(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter parm 
= new SqlParameter(PARM_ARTICLE_ID,SqlDbType.Int,4);
InBlock.gif            parm.Value 
= id;
InBlock.gif
InBlock.gif            
using(SqlConnection conn = new SqlConnection(SqlHelper.GetConnString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                
using(SqlTransaction trans = conn.BeginTransaction())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        SqlHelper.ExecuteNonQuery(trans,CommandType.Text,SQL_ARTICLE_DELETEBYID,parm);
InBlock.gif                        trans.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        trans.Rollback();
InBlock.gif                        
throw;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        conn.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除某个栏目下的所有文章
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="id"></param>

InBlock.gif        public override void DeleteColumnArticles(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] parms 
= GetArticleParameters();
InBlock.gif            parms[
0].Value = id;
InBlock.gif
InBlock.gif            
using(SqlConnection conn = new SqlConnection(SqlHelper.GetConnString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                
using(SqlTransaction trans = conn.BeginTransaction())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        SqlHelper.ExecuteNonQuery(trans,CommandType.Text,SQL_ARTICLE_DELETEBYCOLUMNID,parms);
InBlock.gif                        trans.Commit();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        trans.Rollback();
InBlock.gif                        
throw new Exception("删除栏目下所有文章出错");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        conn.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 私有成员根据缓存得到参数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static SqlParameter[] GetArticleParameters()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] parms 
= SqlHelper.GetCacheParameters(SQL_ARTICLE_INSERT);
InBlock.gif
InBlock.gif            
if(parms == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                parms 
= new SqlParameter[]
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
InBlock.gif                        
new SqlParameter(PARM_ATRICLE_SORT,SqlDbType.Int,4),
InBlock.gif                        
new SqlParameter(PARM_ATRICLE_COLUMNID,SqlDbType.Int,4), 
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_TIME,SqlDbType.DateTime,8),
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_TITLE,SqlDbType.NVarChar,100),
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_CONTENT,SqlDbType.NText,1000),
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_AUTHOR,SqlDbType.VarChar,50),
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_HIT,SqlDbType.Int,4),
InBlock.gif                        
new SqlParameter(PARM_ATRICLE_STATUS,SqlDbType.TinyInt,1),
InBlock.gif                        
new SqlParameter(PARM_ARTICLE_ID,SqlDbType.Int,4)
ExpandedSubBlockEnd.gif                    }
;
InBlock.gif
InBlock.gif                SqlHelper.CacheParameters(SQL_ARTICLE_INSERT,parms);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return parms;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="parms"></param>
ExpandedSubBlockEnd.gif        
/// <param name="article"></param>

InBlock.gif        private static void SetArticleParameters(SqlParameter[] parms,Article article)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            parms[
1].Value = article.ColumnID;
InBlock.gif            parms[
0].Value = article.Sort;
InBlock.gif            parms[
2].Value = article.Time;
InBlock.gif            parms[
3].Value = article.Title;
InBlock.gif            parms[
4].Value = article.Content;
InBlock.gif            parms[
5].Value = article.Author;
InBlock.gif            parms[
6].Value = article.Hit;
InBlock.gif            parms[
7].Value = article.Status;
InBlock.gif            parms[
8].Value = article.ID;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif}
给出操作Article的部分希望可以帮助一些人
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值