Straight to the subject, record
- namespaces: Using System.Data.SqlClient;
- Database link string: SQL Server database link string format: server={0};d atabase={1};uid={2};p wd={3}//0: Server, 1: Database name, 2: User name, 3: password
- Call:
using (SqlConnection connection = new SqlConnection (sqlserverhelper.connectionstring)) {Connec tion. Open (); SqlCommand cmd = new SqlCommand ("Stored Procedure name", connection); Cmd. Parameters.Add ("@param1", SqlDbType.Int); Cmd. parameters["@param1"]. Value = "Param1_value"; Cmd. parameters["@param1"]. Direction = ParameterDirection.Input; Cmd. Parameters.Add ("@param2", SqlDbType.Int); Cmd. parameters["@param2"]. Value = "Param2_value"; Cmd. parameters["@param2"]. Direction = parameterdirection.input;//outpu cmd. Connection = Connection; Cmd.commandtype = commandtype.storedprocedure;//here identifies the invocation type SqlDataReader dr = cmd. ExecuteReader (); Executes the SQL statement, returning the result set//int rerows = cmd. ExecuteNonQuery ();//Execute SQL statement, number of rows affected while (Dr. Read ()) {//Return result processing//dr[0],dr["Col_name"] } Dr. Close (); Close execution connection. Close (); Close Database}
C # calls SQL Server stored procedures