DotNetNuke中的SqlCommandGenarator
以前用c#写程序,所以用IBuySpy; 后来用VB.NET,就用DotNetNuke。我发现在DotNetNuke中有一个类SqlCommandGenerator非常好,使得我们在写访问数据库的类的时候的代码量大大减少。如下面的代码:
Public Function GetSingleLink(ByVal ItemID As Integer, ByVal ModuleId As Integer) As SqlDataReader
Dim myConnection As New SqlConnection(GetDBConnectionString)
' Generate Command Object based on Method
Dim myCommand As SqlCommand = SqlCommandGenerator.GenerateCommand(myConnection, _
CType(MethodBase.GetCurrentMethod(), MethodInfo), _
New Object() {ItemID, ModuleId})
myConnection.Open()
Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
Return result
End Function
使用起来非常的方便。但是有个地方我没有看明白,就是在向存储过程中传递参数的时候,我们会传入类型和长度,不知道在DotNetNuke中,他们在哪儿把这两个值传给SqlParameterAttribute的呢?
发表于 Friday, November 28, 2003 10:20 AM
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:34 AM 开心就好建议看看.Text源代码中的SQLHelper类。它应该比这个要好得多,而且各种情况都考虑到了。
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:42 AM yufan
终于看到同好了,我也在学习DotNetNuke,正在作修改呢,以后多交流哦,lobster_syf@hotmail.com
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:51 AM JGTM'2003
这里面有两个关键之处:
1. System.Reflection.MethodBase.GetCurrentMethod()方法,该方法返回用来reflect的当前正在执行的方法的MethodBase实例。你可以用这个实例(它可能是ConstructorInfo或MethodInfo)访问当前执行方法的类型信息和元数据(如方法名称、返回类型及自定义属性等等)。
2. MethodBase.GetParameters(),该方法返回某一MethodBase实例所代表的方法的参数集合(ParameterInfo[])。其中的每一个ParameterInfo都可以得到其对应参数的名称、类型及自定义属性。
有了这两个信息你基本上可以从一个方法构造出一个可以对对应存储过程进行调用的逻辑了。这种方法早在MSDN Magazine August 2002中的文章就有所论述,你可以参见http://msdn.microsoft.com/msdnmag/issues/02/08/NETReflection/default.aspx(或MSDN Library for VS.NET 2003中的ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dnmag02/html/NETReflection.htm)。
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:53 AM 波仔
建议看看SQLHELPER
这是一种APPLICATION BUILDING BLOCK
存储过程的调用,其实可以不提供类型和长度,
因为这些信息都保存在了SQL SERVER的系统表中,
完全可以查到.
有兴趣的话可以看看我写的这方面一篇文章,
发表在<计算机应用>上了.
需要的话,请EMAIL至JASPER_LIU@MSN.COM
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 11:12 AM 飞鹰
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 11:58 AM ghj1976
SQLHelper 类是在这里实现的:
// Microsoft Data Access Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
我现在公共函数库中,数据库访问就是利用的这个。
#回复: DotNetNuke中的SqlCommandGenarator 12/1/2003 1:59 PM 飞鹰
波仔:
不提供类型和长度如何调用存储过程?
我给你写邮件了,把你的那篇文章发给我看看吧!
我看DotNetNuke中的SqlParameterAttribute类定义了类型和长度,却没有发现那儿调用,很奇怪.
#回复: DotNetNuke中的SqlCommandGenarator 12/4/2003 4:09 PM houling
#回复: DotNetNuke中的SqlCommandGenarator 4/22/2004 7:43 AM Reflection
We did not go through the SqlCommandGenerator class in this article. If you would like to research this functionality further this is where you will want to look. Just put a debug breakpoint on the GenerateCommand Function and follow it through. It basically sets the SqlCommandType and adds parameters to the params collection.
浙公网安备 33010602011771号
评论
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:34 AM 开心就好
建议看看.Text源代码中的SQLHelper类。它应该比这个要好得多,而且各种情况都考虑到了。
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:42 AM yufan
终于看到同好了,我也在学习DotNetNuke,正在作修改呢,以后多交流哦,lobster_syf@hotmail.com
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:51 AM JGTM'2003
这里面有两个关键之处:
1. System.Reflection.MethodBase.GetCurrentMethod()方法,该方法返回用来reflect的当前正在执行的方法的MethodBase实例。你可以用这个实例(它可能是ConstructorInfo或MethodInfo)访问当前执行方法的类型信息和元数据(如方法名称、返回类型及自定义属性等等)。
2. MethodBase.GetParameters(),该方法返回某一MethodBase实例所代表的方法的参数集合(ParameterInfo[])。其中的每一个ParameterInfo都可以得到其对应参数的名称、类型及自定义属性。
有了这两个信息你基本上可以从一个方法构造出一个可以对对应存储过程进行调用的逻辑了。这种方法早在MSDN Magazine August 2002中的文章就有所论述,你可以参见http://msdn.microsoft.com/msdnmag/issues/02/08/NETReflection/default.aspx(或MSDN Library for VS.NET 2003中的ms-help://MS.VSCC.2003/MS.MSDNQTR.2003OCT.1033/dnmag02/html/NETReflection.htm)。
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 10:53 AM 波仔
建议看看SQLHELPER
这是一种APPLICATION BUILDING BLOCK
存储过程的调用,其实可以不提供类型和长度,
因为这些信息都保存在了SQL SERVER的系统表中,
完全可以查到.
有兴趣的话可以看看我写的这方面一篇文章,
发表在<计算机应用>上了.
需要的话,请EMAIL至JASPER_LIU@MSN.COM
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 11:12 AM 飞鹰
谢谢各位,我先看看这些文章和方法.
#回复: DotNetNuke中的SqlCommandGenarator 11/28/2003 11:58 AM ghj1976
SQLHelper 类是在这里实现的:
// Microsoft Data Access Application Block for .NET
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
我现在公共函数库中,数据库访问就是利用的这个。
#回复: DotNetNuke中的SqlCommandGenarator 12/1/2003 1:59 PM 飞鹰
波仔:
不提供类型和长度如何调用存储过程?
我给你写邮件了,把你的那篇文章发给我看看吧!
我看DotNetNuke中的SqlParameterAttribute类定义了类型和长度,却没有发现那儿调用,很奇怪.
#回复: DotNetNuke中的SqlCommandGenarator 12/4/2003 4:09 PM houling
wo
#回复: DotNetNuke中的SqlCommandGenarator 4/22/2004 7:43 AM Reflection
We did not go through the SqlCommandGenerator class in this article. If you would like to research this functionality further this is where you will want to look. Just put a debug breakpoint on the GenerateCommand Function and follow it through. It basically sets the SqlCommandType and adds parameters to the params collection.