在第9章最后一个实例时遇到困难,一是由于我的OS是WIN10,只能装VS2015和SQL2012,二是示例数据库PUBS无法用,只能用adventureworks2012,三是VB2015不再支持IConfigurationSectionHandler,而是用将Config实体化后进行访问,所以那个实例根本没法用,没办法,我是初学VB.NET,前面根本不知道这本书会有这么多内容落后,硬着头皮把第10章XML先看了,好知道它是什么,再返过来又通过MSDN把ConfigurationSection 看了,才初步了解应怎么改才能完成这个实例,而由于英文不好,多次把单词拼写写错,又造成了多日不知问题错在哪里,MD最后才知,思路基本都是对的,错是错在几个变量名的单词写错了,真不知道我是不是在用正确方式学VB编程。(在几天的搜索中,感觉学VB.net的好少,还刚得知,它刚刚进世界10个编程语言之列,原来还在之外,越来越感觉这东西本没有书上说的那么强,什么是多数应用软件的编程语言,不知是不是选错了,而应去选VC或是JAVA之类,哎,好苦恼~),我走了很多弯路,学得很艰难,把这个改好的示例发上来分享下,希望能给同样的初学者一点点帮助和参考。
首先,给数据库加一个简单的存储过程,名字是:usp_Person_get_by_BusinessEntityID,内容是:
CREATE PROCEDURE [usp_Person_get_by_BusinessEntityID]
@BusinessEntityID int
AS
SELECT * from person.person where BusinessEntityID = @BusinessEntityID;
然后新建一个窗体应用程序,把CONFIG文件改为:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="StoredProcedureSettings"
type ="Dateinterveg.StoredProcedureSectionHandler, Dateinterveg, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<StoredProcedureSettings>
<StoredProcedures>
<StoredProcedure name="dbo.usp_Person_get_by_BusinessEntityID">
<Parameters>
<Parameter name="@BusinessEntityID" datatype="int" direction="Input" isNullable="False" size="100"/>
</Parameters>
</StoredProcedure>
</StoredProcedures>
</StoredProcedureSettings>
<connectionStrings>
<add name="AdonetfeatureTest.My.MySettings.AdventureWorks2012ConnectionString"
connectionString="Data Source=HASEE\SQLEXPRESS;Initial Catalog=AdventureWorks2012;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
注意我没有为程序添加数据源,而是直接在app.Config文件中写了数据连接字段,后面会通过这个字段来连接数据库,要注意的是DATA Source值每个人的电脑并不一样,要改成自已电脑的,如我的是hasee\sqlexpress,如不知,也不妨添加数据源Adventureworks2012,系统会自动配置数据连接字段这部分,然后自行把configsections部分加进去就是了。
然后按教材把窗体设计搞掂:就是加一个datagridview 两个BUTTON控件。
完了后加一个类文件:StoredProcedureSectionHandler.vb在这个类文件封装了实体化Config的几个类。内容如下:
Option Explicit On
Option Strict On
Imports System
Imports System.Configuration
Imports Microsoft.VisualBasic
Public NotInheritable Class StoredProcedureSectionHandler
Inherits ConfigurationSection
<ConfigurationProperty("StoredProcedures", IsDefaultCollection:=False), ConfigurationCollection(GetType(StoredProcedurescollection), AddItemName:="StoredProcedure")>
Public Property StoredProcedures() As StoredProcedurescollection
Get
Dim storedprocedurescollection As StoredProcedurescollection = CType(MyBase.Item("StoredProcedures"), StoredProcedurescollection)
'ConfigurationSection can have many custom sections ,so configurationsection should be set to a collection class ,or because configurationsection is inherited from configurationelement class
Return storedprocedurescollection
End Get
Set(value As StoredProcedurescollection)
Dim storedprocedure As StoredProcedurescollection = value
End Set
End Property
Public Sub New()
Dim storedprocedure As New StoredProcedureElement()
StoredProcedures.add(storedprocedure)
End Sub
End Class
Public Class StoredProcedurescollection
Inherits System.Configuration.ConfigurationElementCollection
Public Sub New()
End Sub
Public Overrides ReadOnly Property collectiontype() As ConfigurationElementCollectionType
Get
Return ConfigurationElementCollectionType.AddRemoveClearMap
End Get
End Property
Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
Return New StoredProcedureElement()
End Function
Protected Overrides Function GetElementKey(element As ConfigurationElement) As Object
Return (CType(element, StoredProcedureElement)).name
End Function
Default Public Shadows Property Item(ByVal index As Integer) As StoredProcedureElement
Get
Return CType(BaseGet(index), StoredProcedureElement)
End Get
Set(value As StoredProcedureElement)
If BaseGet(index) IsNot Nothing Then
BaseRemoveAt(index)
End If
BaseAdd(value)
End Set
End Property
Default Public Shadows ReadOnly Property Item(ByVal name As String) As StoredProcedureElement
Get
Return CType(BaseGet(name), StoredProcedureElement)
End Get
End Property
Public Function Indexof(ByVal element As StoredProcedureElement) As Integer
Return BaseIndexOf(element)
End Function
Public Sub Add(ByVal storedprocedure As StoredProcedureElement)
'MyBase.BaseAdd(storeprocedure, False)
Me.BaseAdd(storedprocedure)
End Sub
Protected Overloads Sub BaseAdd(ByVal element As ConfigurationElement)
BaseAdd(element, False)
End Sub
End Class
Public Class StoredProcedureElement
Inherits ConfigurationElement
Public Sub New()
End Sub
Public Sub</