5,693,062 members and growing! (15,476 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Creating CRUD operations using the SqlNetFramework and CodeSmith

By Luis Ramirez

This article shows you how to generate SQL code for CRUD operations using the SqlNetFramework and CodeSmith.
VB, SQL, C# 2.0, C#, Windows, .NET, .NET 2.0, ASP.NET, SQL Server, ADO.NET, WebForms, VS2005, Visual Studio, DBA, Dev

Posted: 20 Apr 2007
Updated: 20 Apr 2007
Views: 9,377
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
1 vote for this Article.
Popularity: 0.00 Rating: 2.00 out of 5
0 votes, 0.0%
1
1 vote, 100.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The SqlNetFramework helps you to reduce the quantity of code that you need to connect and to execute operations in a relational database. In the following article we will show you how you can reduce the development time of your SQL statements. To automate the creation of the SQL code we will use the CodeSmith tool. We will focus in the automation of the Create, Read, Update and Delete operations used to maintain a database table.

Installing software

Before you can use the new tool to create the CRUD operations you need to install the required software.

  • Install the SqlNetFramework. Download it from www.sqlnetframework.com
  • Install the CodeSmith code generation tool. Download it from www.codesmithtools.com.
  • Register the SqlNetFramework add-in for CodeSmith. Copy the [SqlNetFrameworkInstallDir]\SqlNetFramework 1.0\CodeSmith\AddIn\SqlNetFrameworkCSAddIn.dll and SqlNetFrameworkCodeHelper.dll files to the [CodeSmithInstallDir]\currentVersion\AddIns directory. Repeat the same for the file located at [SqlNetFrameworkInstallDir]\SqlNetFramework 1.0\bin\SqlNetFramework.Core.dll and SqlNetFramework.Shared.dll.

    Examining CRUD template

    Now you are ready to examine the CRUD template. It is assumed that you already have a basic understanding about how CodeSmith works. CodeSmith has very easy tutorials for beginners, they are included in the CodeSmith installation.

    1. Open the CodeSmith Studio.
    2. Open the CRUD template. It is located at [SqlNetFrameworkInstallDir]\SqlNetFramework 1.0\CodeSmith\Templates\CRUD.cst.
    3. The CRUD template properties are displayed in the properties window. Three properties are mandatory and the rest are optional.
    4. Table property. Your need to specify the database table for which the CRUD operations will be created. Use the "Table Picker" editor included with CodeSmith to choose the database table.
    5. DbFactoryType property. You need to specify the database factory that will be used to create the SQL statements.
    6. SqlDataStore property. You need to specify the SQL repository where the SQL code will be stored.
    7. The CreateDelete, CreateInsert, CreateSelect and CreateUpdate properties are used to specify which SQL statements will be created. By default all the SQL statements are created.
    8. The InsertFields, SelectFields and UpdateFields are used to specify which fields will be used in the Insert, Select and Update SQL statement, respectively.
    9. The DeleteStatementId, InsertStatementId, SelectStatementId and UpdateStatementId properties are used to assign the identifier in the SQL data store. Their values are assigned automatically the first time that the template is executed.

    Generating SQL code

    Now you will use the CRUD template to generate SQL code automatically. You will use the DemoDb.mdb Access database deployed with the SqlNetFramework C# demo.

    1. Open CodeSmith Studio.
    2. Open the CRUD template.
    3. Select the Table property and then click the … button. The "Table Picker" editor will be displayed.
    4. Add a new data source. Click the ... button at the right of the data source dropdown list.
    5. The "Data Source Manager" dialog will be displayed. Click the Add button.
    6. The "Data Source" dialog is displayed. Assign the values as shown in the table below.
      Name AccessDemoDb
      Provider type ADOXSchemaProvider
      Connection string PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE='[YourDirectory]\DemoDb.mdb'
    7. Click on Test to test the connection and then click OK.
    8. Select the AccessDemoDb data source and click on Close.
    9. The DemoDb database tables must be displayed in the "Table Picker" editor.
    10. Select the Customer database table. Click on Select.
    11. You will use the OleDb database factory to create the SQL statements. Assign OleDbFactory to the DbFactoryType property.
    12. You will use a XML repository to store the SQL code. Select the SqlDataStore property and then click the … button. The "SqlDataSource Manager" editor will be displayed.
    13. Click the Add… button to add a new SqlDataStore. The "Add/Edit SqlDataStore" dialog is displayed. Assign the values as shown in the table below.
      Name DemoDbXml
      Manager type XmlSqlDataStoreManager
      Connection settings [SqlNetFrameworkInstallDir]\SqlNetFramework 1.0\Demos\SqlNetFrameworkEvaluationCS\App_Data\DemoDb.xml
    14. Click Ok to close the "Add/Edit SqlDataStore" dialog.
    15. Select the DemoDbXml and click on "Select" button.
    16. Now you are ready to run the CRUD template. Click F5 to run it.
    17. As you can see the properties window has been refreshed. The InsertFields, SelectFields and UpdateFields properties contain new values. These values are inferred from the database table schema.
    18. The DeleteStatementId, InsertStatementId, SelectStatementId and UpdateStatementId properties contain the identifier assigned by the XML SqlDataStore.

    Now you will use a DetailsView control and a SqlStoreDataSource control display data and execute operations in the Customer database table. I have created the webform into the SqlNetFramework C# demo included with the SqlNetFramework installation.

    <SqlNetFramework:SqlStoreDataSource ID="dsCustomers" SelectStatementID="12" InsertStatementID="13" 
    UpdateStatementID="14" DeleteStatementID="15" ConnectionID="DemoDb" runat="server" 
    DataSourceMode="DataSet"></SqlNetFramework:SqlStoreDataSource>
     
    <asp:DetailsView ID="dvCustomers" DataSourceID="dsCustomers" runat="server" AllowPaging="True" 
    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" 
    AutoGenerateRows="False" DataKeyNames="CustomerId">
        <Fields>
            <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" InsertVisible="False"
                ReadOnly="True" SortExpression="CustomerId" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        </Fields>
    </asp:DetailsView>    
    

    Conclusion

    As you can see the SqlNetFramework reduce the quantity of code that you need to program. With the use of the SqlNetFramework and CodeSmith you can automate the creation of SQL code.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Luis Ramirez


    Luis Ramirez is creator and owner of http://www.sqlnetframework.com. The SqlNetFramework is a compelling alternative to the Microsoft Data Access Application Block. Luis Ramirez is a Microsoft Certified Professional specialized in .NET development. If you want to contact him to work in your projects or for any other inquiry that you have write him to lramirez [at] sqlnetframework.com.
    Occupation: Software Developer (Senior)
    Company: www.sqlnetframework.com
    Location: Mexico Mexico

    Other popular ASP.NET articles:

    Article Top
    Sign Up to vote for this article
    You must Sign In to use this message board.
    FAQ FAQ Noise ToleranceSearch Search Messages 
     Layout  Per page   
     Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
    GeneralTo SmartCodememberCharles1019:04 26 May '07  
    GeneralRe: To SmartCodememberLuis Ramirez13:42 12 Jun '07  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 20 Apr 2007
    Editor:
    Copyright 2007 by Luis Ramirez
    Everything else Copyright © CodeProject, 1999-2008
    Web19 | Advertise on the Code Project