Ibatisnet uses sqldataadapter for batch update after obtaining the SQL statement

Source: Internet
Author: User
Tags sql using

The reason why we want to combine these two things is that these two things have their own advantages:

Ibatis:Anyone who knows about ibatis knows that the most powerful thing about it is the ing between parameters and query results and object classes. This saves the trouble of splicing SQL statements and separates SQL statements from code. At the same time, ibatis implements dynamic SQL using tags such as <isnotempty>. These make our code look neat and elegant. However, it has a disadvantage, which is not very good for batch processing. For example, if you want to have a grid on the page, you can add, delete, modify, and batch submit the grid. This reminds us of the sqldataadapter update function.

Sqldataadapter:At least for me, batch update is the biggest highlight. both efficiency and implementation difficulty are much better than ibatis. However, the traditional use of sqldataadapter writes SQL into the code, which is still frustrating. So at this time, I thought about whether to use ibatis to obtain the SQL statement and then use sqldataadapter to execute batch update.

Obtain the SQL statement after ibatis ing and ask Baidu to check Google. The following method is found:

public static string GetSql(string statementName, object paramObject)        {            string result = string.Empty;            paramMap = null;            if (Mapper == null) return result;            try            {                IMappedStatement statement = Mapper.GetMappedStatement(statementName);                if (!Mapper.IsSessionStarted)                {                    Mapper.OpenConnection();                }                RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, Mapper.LocalSession);                result = scope.PreparedStatement.PreparedSql;            }            catch (Exception ex)            {                result = "Error for obtaining sql string:" + ex.Message;            }            finally            {                Mapper.CloseConnection();            }            return result;        }

Unfortunately, this method can only obtain SQL statements without parameters, such as select * from a where id = @ param0, the next step is to pass in the parameter values in the object class. Here I think of sqlparameter In ADO. As long as the parameter value in the object class corresponds to @ param0, the problem is solved. So check whether there is any parameter information in statement and find that parametermap is the list of entity-class parameters obtained by ibatis parsing SQL. The problem is cleared.
The final code:

Public static string getsql (string statementname, object paramobject, out parametermap parammap) {string result = string. empty; parammap = NULL; If (mapper = NULL) return result; try {imappedstatement Statement = mapper. getmappedstatement (statementname); If (! Mapper. issessionstarted) {mapper. openconnection ();} requestscope scope = Statement. statement. SQL. getrequestscope (statement, paramobject, mapper. localsession); Result = scope. preparedstatement. preparedsql; parammap = Statement. statement. parametermap;} catch (exception ex) {result = "error for obtaining SQL string:" + ex. message;} finally {mapper. closeconnection ();} return result;} // <summar Y> /// obtain the SQL statement using ibatis and place the parameters into the SQL statement, run the following command to obtain the corresponding sqldataadapter and datatable /// </Summary> /// <Param name = "statementid"> </param> /// <Param name = "paramterobject"> </param> /// <Param name = "sqladap"> </param> /// <returns> </returns> Public static datatable executefordatetable (string statementid, object paramterobject, ref sqldataadapter sqladap) {parametermap parammap = NULL; string sqlstr = getsql (statementid, Paramterobject, out parammap); datatable = new datatable (); string sqlconnectionstring = ibatismapper. mapper. datasource. connectionstring; sqlconnection = new sqlconnection (sqlconnectionstring); sqlcommand = new sqlcommand (sqlstr, sqlconnection); If (parammap! = NULL) {for (INT I = 0; I <parammap. properties. length; I ++) {sqlparameter sp = new sqlparameter ("@ Param" + I, paramterobject. getType (). getproperty (parammap. properties [I]. propertyname ). getvalue (paramterobject, null); sqlcommand. parameters. add (SP) ;}} sqladap = new sqldataadapter (sqlcommand); sqlcommandbuilder sqlbuilder = new sqlcommandbuilder (sqladap); sqlconnection. open (); sqladap. fill (datatable); sqlconnection. close (); Return datatable ;}

For batch update,

SqlDataAdapter.Update()

I believe everyone is familiar with the method.

After studying it for one night, I learned how to record it in the lower left corner and hope it will be useful to everyone.

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.