TypedDataSetYesfromDataSetDerived class. Similarly, it inheritsDataSetAll methods, events, and attributes. In additionDataSetProvides strongly typed methods, events, and attributes. This means that tables and columns can be accessed by name instead of using the set-based method. In addition to improving code readabilityDataSetYou can also allow the Visual Studio. NET code editor to automatically enter the lines you type (Code smart prompts to speed up Coding ).
In additionDataSetIt also allows access to values of the correct type during compilation. By using a strongly typedDataSetWill capture Type Mismatch Errors During code compilation (rather than runtime.
Here we will describe the table "EnglishName" with the fields AccountID int (PrimaryKey), FirstName varchar (30), and FamilyName varchar (30.
Step 1: generate (define) A strong data type Dataset
Create an IDE database resource manager and create a new database to connect to the local Northwind database. Create the EnglishName table and add the fields AccountID, FirstName varchar (30), FamilyName varchar (30). Then, create a new Project: Project1 in IDE and add a new dataset type file, EnglishNameDS. xsd. Then, capture the EnglishName file. on the xsd designer, IDE will automatically read the schema of the EnglishName table and add the EnglishName table and its fields. We do not need to input them one by one, this greatly facilitates the speed and correctness of data entity definitions. Save EnglishNameDS. xsd. IDE will automatically generate a strongly Typed DataSet: EnglishNameDS. Class EnglishNameDS inherit from DataSet and have the EngishName (class EngishNameDataTable), while EngishNameDataTable [Index] returns the EngishNameRow type. Class EngishNameRow has three attributes: AccountID (int), FirstName, familyName (string ). You can perform the following operations to view the file EnglishNameDS. cs.
Step 2: Construct a strongly Typed Dataset: EnglishNameDS object
Some people may wonder, how can I convert the data queried from the database table EnglishName into an object of EnglishNameDS?
| SqlHelper helps make the code very simple |
EnglishNameDS lds = new EnglishNameDS (); String SQL = "Select * from EnglishName "; String [] tableNames = new string [] {lds. EnglishName. TableName }; SqlHelper. FillDataset (DBConnectString, CommandType. Text, SQL, lds, tableNames ); |
Step 3: Use a strongly Typed Dataset: EnglishNameDS object
To bind the lds to the DataGrid, It is very simple:
Create procedure dbo. EngishName_Insert
@ AccountID int,
@ FirstName nvarchar (30 ),
@ FamilyName nvarchar (30)
AS
Insert into dbo. EngishName (
AccountID,
FirstName,
FamilyName
) VALUES (
@ AccountID,
@ FirstName,
@ FamilyName
)
IF @ ROWCOUNT = 1
RETURN @ AccountID
ELSE
RETURN-100
GO
EnglishName_Update:
Create procedure dbo. EngishName_Update
@ AccountID int,
@ FirstName nvarchar (30 ),
@ FamilyName nvarchar (30)
AS
UPDATE dbo. EngishName
SET
FirstName = @ FirstName,
FamilyName = @ FamilyName
WHERE
AccountID = @ AccountID
IF @ ROWCOUNT = 1
RETURN @ AccountID
ELSE
RETURN-100
GO
EnglishName_Delete:
Create procedure dbo. EngishName_Delete
@ AccountID int
AS
Delete from dbo. EngishName
WHERE
AccountID = @ AccountID
GO
With SqlHelper, the Insert, Update, and Delete operations are as follows:
| EnglishName_Insert |
EnglishNameDS lds = new EnglishNameDS (); Lds. EngishName. AddEngishNameRow (1000, "Zendy", "Hu "); SqlHelper. ExecuteReaderTypedParams (DBConnectionString, "EnglisName_Insert", lds. EngishName [0]);
|
| EnglishName_Update |
EnglishNameDS lds = new EnglishNameDS (); Lds. EngishName. AddEngishNameRow (1000, "ZendyNew", "Hu "); SqlHelper. ExecuteReaderTypedParams (DBConnectionString, "EnglisName_Update", lds. EngishName [0]);
|
| EnglishName_Delete |
EnglishNameDS lds = new EnglishNameDS (); Lds. EngishName. AddEngishNameRow (1000 ,"",""); SqlHelper. ExecuteReaderTypedParams (DBConnectionString, "EnglisName_Delete", lds. EngishName [0]); |
Summary:
Use IDE to automatically generate a strong-type Dataset with more tables, and then use CodeSmith to automatically generate Insert, Update, and Delete SP for the custom Row class of the strong-type Dataset (for example, EnglishName here) the attribute name is exactly the same as the field name in the database as the Insert, Update, and Delete SP parameter names (This is the key), And then use SqlHelper'sExecuteReaderTypedParams can discard ugly Code such as parameter assignment and field name designation when operating the database, which greatly improves code consistency, readability, maintainability, simplicity, and correctness, this greatly reduces the amount of code.
| DataGrid1.SetDataBinding (lds. EnglishName ,""); |
If you want to obtain the Firstname of the first record, you can: String firstName = lds. EnglishName [0]. FirstName. |
Step 4: Use a strongly-Typed Dataset: EnglishNameDS object to perform Insert, Update, and Delete operations.
First, use Codesmith to generate the SP:
EnglishName_Insert: