Sqlparameter: an error-prone problem (@ 2.0)

Source: Internet
Author: User
Recently, I encountered a problem in the training system. The main problems involved are as follows:

Public models. timu listtimu (ilist <models. timudesc> timudescs, bool iskaoshi)
...{

Models. timu rtnvalue = new models. timu ();
Sqlparameter [] Param =... {New sqlparameter ("@ spoint", 0), new sqlparameter ("@ TID", guid. Empty )};
Sqlconnection Cn = new sqlconnection (cnstring );
CN. open ();
Foreach (models. timudesc TD in timudescs)
...{
String SQL = string. Format (SQL _selecttimunotkaoshiformat, TD. point1 );
For (INT I = 1; I <= 3; I ++)
...{
Param [0]. value = I;
Param [1]. value = TD. guid;
Dbutility. sqlhelper. Fill (rtnvalue. _ timu, CN, commandtype. Text, SQL, Param );
}
}
CN. Close ();
Return rtnvalue;
}



Public static void fill (datatable typeddatasetstable, sqlconnection Conn, commandtype primitive type, string plain text, Params sqlparameter [] commandparameters)
...{
Sqlconnection conection = conn;
Sqlcommand cmd = new sqlcommand ();
Preparecommand (CMD, Conn, null, struct type, plain text, commandparameters );
Sqldataadapter Ada = new sqldataadapter (CMD );
Ada. Fill (typeddatasetstable );
}



Private Static void preparecommand (sqlcommand cmd, sqlconnection Conn, sqltransaction trans, commandtype primitive type, string plain text, sqlparameter [] partial parms)
...{

If (conn. State! = Connectionstate. open)
Conn. open ();

Cmd. Connection = conn;
Cmd. commandtext = plain text;

If (trans! = NULL)
Cmd. Transaction = trans;

Cmd. commandtype = primitive type;

If (partition parms! = NULL)
...{
Foreach (sqlparameter parm in milliseconds parms)
Cmd. Parameters. Add (parm );
}
}

The last part is a method in sqlhelper, and the second part is the code of the sqlhelper extension. The following error will be reported during execution.

System. argumentexception: Another sqlparametercollection contains sqlparameter.

Sqlparameter [] Param =... {New sqlparameter ("@ spoint", 0), new sqlparameter ("@ TID", guid. Empty )};
Sqlconnection Cn = new sqlconnection (cnstring );
CN. open ();
Foreach (models. timudesc TD in timudescs)
......{
String SQL = string. Format (SQL _selecttimunotkaoshiformat, TD. point1 );
For (INT I = 1; I <= 3; I ++)
......{
Param [0]. value = I;
Param [1]. value = TD. guid;
Dbutility. sqlhelper. Fill (rtnvalue. _ timu, CN, commandtype. Text, SQL, Param );
}
}
CN. Close ();

Here is the only place where the parameter is saved. It must be the problem here. I am writing this because I want to save a little time for sqlparameter new, in the fill method, the sqlcommand object is new, so each call to the new method fill method cmd (sqlcommand) it is the only new "public place" that can be recorded for multiple calls is the sqlconnection object. But there is no reason why I have to open and close the connection once when I call fill, so I 'd like to try and get new.

Change the code to the following:

Public models. timu listtimu (ilist <models. timudesc> timudescs, bool iskaoshi)
...{

Models. timu rtnvalue = new models. timu ();

Sqlconnection Cn = new sqlconnection (cnstring );
CN. open ();
Foreach (models. timudesc TD in timudescs)
...{
String SQL = string. Format (SQL _selecttimunotkaoshiformat, TD. point1 );
For (INT I = 1; I <= 3; I ++)
...{
Sqlparameter [] Param
=... {New sqlparameter ("@ spoint", I), new sqlparameter ("@ TID", TD. guid )};
Dbutility. sqlhelper. Fill (rtnvalue. _ timu, CN, commandtype. Text, SQL, Param );
}
}
CN. Close ();
Return rtnvalue;
}

Run and solve the problem

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.