Common Code Snippets for C # operations SQL databases are as follows:
<summary>
///Execute command
///</summary>
///<param name= "procname" > Command text </param>
///<param name= "prams" > Parameter object </param>
///<returns></returns> public
int Runproc (String procname, sqlparameter[] prams)
{
SqlCommand cmd = CreateCommand (procname, prams);
Cmd. ExecuteNonQuery ();
This. Close ();
Get execution Success return value
(int) cmd. parameters["ReturnValue"]. Value;
}
///<summary>///Create a SqlCommand object to execute command text///</summary>///<param name= "ProcName" > Command text </param>///<param name= "prams" command text required parameters </param>///<returns> return SqlCommand pair
Like </returns> private SqlCommand CreateCommand (String procname, sqlparameter[] prams) {//Confirm open connection This.
Open ();
SqlCommand cmd = new SqlCommand (procname, con); Cmd.commandtype = CommandType.Text; Execution Type: Command text//In turn, parameters are passed in the command text if (prams!= null) {foreach (SqlParameter parameter in PR AMS) cmd.
Parameters.Add (parameter); //Join return parameter cmd. Parameters.Add (New SqlParameter ("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, 0, 0, string.
Empty, Datarowversion.default, null));
return cmd; }
If you change SQL into Access. The appropriate code simply needs to replace the SQL type with an OLE DB type.
The common code for C # to manipulate an Access database is as follows:
<summary>
///Execute command
///</summary>
///<param name= "procname" > Command text </param>
///<param name= "prams" > Parameter object </param>
///<returns></returns> public
int Runproc (String procname, oledbparameter[] prams)
{
OleDbCommand cmd = CreateCommand (procname, prams);
Cmd. ExecuteNonQuery ();
This. Close ();
Get executed successfully return value
//return (int) cmd. parameters["ReturnValue"]. Value;
return 1;
}
<summary>///Create a OleDbCommand object to execute the command text///</summary>///<param name= "proc Name ' > Command text </param>///<param name= ' prams ' command text required parameters </param>///<returns> return oledbcom
Mand Objects </returns> Private OleDbCommand CreateCommand (String procname, oledbparameter[] prams) { Confirm open Connection this.
Open ();
OleDbCommand cmd = new OleDbCommand (procname, con); Cmd.commandtype = CommandType.Text; Execution Type: Command text//In turn, the arguments are passed in the command text if (prams!= null) {foreach (Oledbparamet ER parameter in prams) cmd.
Parameters.Add (parameter); //Join return parameter//cmd. Parameters.Add (//New OleDbParameter ("ReturnValue", OleDbType.Integer, 4,//Parameterdirec tion. ReturnValue, False, 0, 0,//String.
Empty, Datarowversion.default, null)); return cmd; }
Because Access does not need to return a value. So the CreateCommand function does not need the following parts:
Cmd. Parameters.Add (
new SqlParameter ("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, False, 0, 0,
string. Empty, Datarowversion.default, null));
If this part is not removed, the code that operates the SQL is still copied. There will be an error "Exception details: System.InvalidOperationException:System.Data.OleDb.OleDbDataAdapter INTERNAL error: Invalid parameter accessor: 2 badbindinfo. ”
If you encounter a similar error, you may wish to comment out the return value as described above.