C # asp.net 2.0 dynamic SQL statement of tableadapter

Source: Internet
Author: User

For a multi-condition search page, if each of the five conditions is written, 25 add queries are used, and 10 are 100 add queries.

TableAdapter can publish two methods: suitable for filling in the Fill method of the existing DataSet and returning the Get method of the filled DataTable object. The former is more suitable for Windows clients (DataSet is stored in memory during the lifetime of the Application), while the latter (that is, the Get method) is more suitable for ObjectDataSource.

Create a class file: (function: add the original tableadapter file and add the required class)
Namespace FernandoTableAdapters
{
Public partial class OutRegisterTableAdapter
{
Public virtual Fernando. OutRegisterDataTable GetDataBySearch (string searchSql)
{
This. Adapter. SelectCommand = new System. Data. OleDb. OleDbCommand (searchSql, this. Connection );
Fernando. OutRegisterDataTable dataTable = new Fernando. OutRegisterDataTable ();
This. Adapter. Fill (dataTable );
Return dataTable;
}
}
}

Then, just like the original operation, open the class file and add the method:
[System. ComponentModel. DataObjectMethodAttribute (System. ComponentModel. DataObjectMethodType. Select, false)]
Public Fernando. OutRegisterDataTable GetOutRegisterBySearch (string searchSql)
{
Return Adapter. GetDataBySearch (searchSql );
}

The last page is displayed:

<Asp: ObjectDataSource ID = "OutRegisterDataSource" runat = "server" SelectMethod = "GetOutRegisterBySearch"
TypeName = "OutRegister" OldValuesParameterFormatString = "original _ {0}" DeleteMethod = "DeleteOutRegister"
UpdateMethod = "LogOutOutRegister">
<SelectParameters>
<Asp: Parameter Name = "searchSql" Type = "String" DefaultValue = "SELECT * from OutRegister"/>
</SelectParameters>
<DeleteParameters>
<Asp: Parameter Name = "outRegisterID" Type = "Int32"/>
</DeleteParameters>
<UpdateParameters>
<Asp: Parameter Name = "outRegisterID" Type = "Int32"/>
</UpdateParameters>
</Asp: ObjectDataSource>

Enter the cs file:
Protected void Button3_Click (object sender, EventArgs e)
{
String searchSql = "SELECT * from OutRegister where OutRegisterID = 2 ";
OutRegisterDataSource. SelectParameters ["searchSql"]. DefaultValue = searchSql;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.