081229
Parameter names can be distinguished using [J. ID]
- Dbhelper. getparameter ("@ ID", sqldbtype. Int, 4, "[J. ID]", ID) // multiple Commas are correct,
1. sqlparameter usage
Two constructor types:
Sqlparameter Param = new sqlparameter (paramname, paramtype, paramsize, colname );
Sqlparameter Param = new sqlparameter (paramname, paramtype );
Param. value = paramvalue;
The above is the column corresponding to the data table. To summarize the following usage:
Public static sqlparameter getparameter (string paramname, sqldbtype paramtype, int32 paramsize, string colname, object paramvalue)
{
Sqlparameter Param = new sqlparameter (paramname, paramtype, paramsize, colname );
Param. value = paramvalue;
Return Param;
}
Dbhelper. getparameter ("@ gid", sqldbtype. Int, 4, "[GID]", GID)
When multiple tables are queried, the parameters are the same and can be distinguished directly in the constructor.
Public datatable getlist (INT ocheck, string UID)
{
Stringbuilder sb = new stringbuilder ();
SB. append ("select OID,. gid, B. gname,. onum,. summary, B. gspec from t_takeout_temp A, t_goods B where. gid = B. GID and ocheck = @ ocheck and. uid = @ uid ");
Sqlparameter [] Param =
{
Dbhelper. getparameter ("@ ocheck", sqldbtype. Int, 4, "[ocheck]", ocheck ),
Dbhelper. getparameter ("@ uid", sqldbtype. varchar, 50, "A. [uid]", UID) // use a. [uid] Here
};
Return dbhelper. executedt (sb. tostring (), Param );
}
I don't quite understand the principle, but I will use it.
2. Skills with single value.
- /// <Summary>
- /// Obtain the personnel name based on the personnel ID
- /// </Summary>
- /// <Param name = "rydm"> </param>
- /// <Returns> </returns>
- Public String getrymc (string rydm)
- {
- Datatable dt = getuser (rydm );
- Return DT. Rows [0] ["rymc"]. tostring ();
- }
- /// <Summary>
- /// Query by personnel ID
- /// </Summary>
- /// <Param name = "rydm"> </param>
- /// <Returns> </returns>
- Public datatable getuser (string rydm)
- {
- Stringbuilder sb = new stringbuilder ();
- SB. append ("select *");
- SB. append ("from p_rydm where [rydm] = @ rydm ");
- Sqlparameter [] Param = {dbhelper. getparameter ("@ rydm", sqldbtype. varchar, 50, "[rydm]", rydm )};
- Return dbhelper. executedt (sb. tostring (), Param );
- }
First, write a method to get the datatable, and then directly call DT. Rows [0] ["field name"]. tostring ()
- Datatable table = dbhelper. executedt (sb. tostring ());
- If (table. Rows. Count> 0)
- {
- If (table. Rows [0] ["Sid"]. tostring ()! = "")
- Return table. Rows [0] ["Sid"]. tostring ();
- Else
- Return "1001 ";
- }
- Else
- {
- Return "0 ";
- }
Here, the rows. Count judgment is necessary. In addition, null characters may be returned, so it is also necessary to determine whether it is ""
3. dbhelper. executedt () has two kinds of overloading:
Execute SQL statements with or without parameters.
4. The most common statements do not need to be blurred.
Query select [gname], [gspec] from goods where [GID] = @ [GID] and [OID] = @ OID
Update goods set [gname] = @ gname, [gspec] = @ gspec where [GID] = @ GID
Delete Delete from goods where [GID] = @ GID
Add insert into goods ([gname], [gspec]) values (@ gname, @ gspec)
5. Accumulation of methods used
SB. append ("Update t_goods set [Storage] = [Storage]-@ num where [GID] = @ gid ");
Dbhelper. getparameter ("@ num", sqldbtype. Int, 4, "[num]", num)
[Num] is not a field in the data table, that is, @ num does not need a column name,
However, there is no constructor without the corresponding column name, so I have to add a corresponding column.
The strange thing is that sqlparameter does not have the corresponding column constructor, and even the length is gone.
It seems that the length does not need to be written. There is a default value, and you have time to try it.
5.2
Select distinct Sid from storein where sdate between @ start and @ end
6. dbhelper. executesql (sb. tostring (), Param );
This method does not have any overload without parameters. You have time to write it yourself.
The solution is to add a parameter without using this parameter.
7. In addition to searching for additions, deletions, and changes with executedt (), executesql () is used ()
In addition, the self-written executeds () and the method for batch SQL Execution executesqls () will be studied later and will not be used for the time being.
The two methods are sufficient.
End