






















面对DotText的源代码,对偶等初学者而言,简直无从下手。只好硬着头皮捡条理清晰的下手。前面看了Blog_Config 表,看了添加新Blog的存储过程,现在自然会想到blog_UTILITY_AddBlog是如何被调用的。
很容易在.\DotTextWeb下找到 register.aspx和register.aspx.cs.重点分析代码文件。
使用了以上几个命名空间。Dottext.Framework命名空间的类视图如下:
一头雾水。。。。继续看代码
前面声明了一些页面的Web控件,主要是一些文本录入控件,验证控件,按钮之类。
后面page_load()这段,表示从"~/Admin/Skins.config"XML文件中,读取内容,绑定到DropDownList ddlSkin中。
再来看当“注册”按钮被按下后
private void Linkbutton1_Click(object sender, System.EventArgs e)
{
if(this.txbTitle.Text=="")
{
this.txbTitle.Text=this.txbUser.Text;
}
string host = Config.Settings.AggregateHost;//ConfigurationSettings.AppSettings["AggregateHost"] as string;
string url = Config.Settings.AggregateUrl;
string sql = "blog_UTILITY_AddBlog";
string conn = Dottext.Framework.Providers.DbProvider.Instance().ConnectionString;
string city=Request.Form.Get("city");
if(city==null)
{
city="";
}
SqlParameter[] p =
{
SqlHelper.MakeInParam("@UserName",SqlDbType.NVarChar,50,this.txbUser.Text),
SqlHelper.MakeInParam("@Password",SqlDbType.NVarChar,50,Security.Encrypt(this.txbPwd.Text)),
SqlHelper.MakeInParam("@Email",SqlDbType.NVarChar,50,this.txbEmail.Text),
SqlHelper.MakeInParam("@Host",SqlDbType.NVarChar,50,host),
SqlHelper.MakeInParam("@Application",SqlDbType.NVarChar,50,this.txbUser.Text),
SqlHelper.MakeInParam("@Author",SqlDbType.NVarChar,50,this.txbAuthor.Text),
SqlHelper.MakeInParam("@Title",SqlDbType.NVarChar,100,this.txbTitle.Text),
SqlHelper.MakeInParam("@SubTitle",SqlDbType.NVarChar,250,this.txbSubtitle.Text),
SqlHelper.MakeInParam("@IsHashed",SqlDbType.Bit,0,1),
SqlHelper.MakeInParam("@Skin",SqlDbType.NVarChar,50,this.ddlSkin.SelectedValue),
SqlHelper.MakeInParam("@City",SqlDbType.NVarChar,50,city),
};
//int result=0;
try
{
Dottext.Framework.Data.SqlHelper.ExecuteNonQuery(conn,CommandType.StoredProcedure,sql,p);
}
catch
{
Response.Write("<font color='red'>用户名已存在</font>");
//throw;
}
Response.Redirect(url+this.txbUser.Text);
}
最最关键的一句就是:
Dottext.Framework.Data.SqlHelper.ExecuteNonQuery(conn,CommandType.StoredProcedure,sql,p);
(1)conn: 连接数据库字符串。由以下方法获取:
string conn = Dottext.Framework.Providers.DbProvider.Instance().ConnectionString;
(2)CommandType.StoredProcedure 调用方式为存储过程。
(3)sql : 定义string sql = "blog_UTILITY_AddBlog"; 调用的存储过程名。
(4) p : SqlParameter[] p, 及存储过程的入口参数。
SqlHelper实际上是Microsoft Application Blocks for .NET组件的一个类。Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以及向 SQL Server 数据库发出 SQL 文本命令。它返回 SqlDataReader、DataSet 和 XmlReader 对象。您可以在自己的 .NET 应用程序中将其作为构造块来使用,以减少需要创建、测试和维护的自定义代码的数量。
Data Access Application Block 可以帮助您:
SqlHelper 类提供了一组静态方法,可以用来向 SQL Server 数据库发出许多各种不同类型的命令。
SqlHelperParameterCache 类提供命令参数缓存功能,可以用来提高性能。该类由许多 Execute 方法(尤其是那些只运行存储过程的重写方法)在内部使用。数据访问客户端也可以直接使用它来缓存特定命令的特定参数集。
而且,Data Access Application Block是一个开源的代码,可以象Dottext一样用到自己的项目中。
不想过于深入,点到为止,呵呵
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。