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