Stored procedures from entry to the master (reprint)

① Why use stored procedures?
Because it is faster than the SQL statement is executed.

What ② storage process?
Put together a bunch of SQL statements Lo, you can also execute SQL statements according to different conditions

③ to one of the most simple stored procedure
CREATE PROCEDURE dbo .testProcedure_AX
AS
the SELECT USERS userID from the Order by userid desc

Note: dbo.testProcedure_AX is the name of the stored procedure you create, you can change: AXzhz, etc., do not tell keyword conflicts on the line .AS following is a SQL statement, do not write SQL Please avoid statements.

④ how I call the stored procedure in ASP.NET?
yellow at the end of these two lines is enough to make a.
public static String GetCustomerCName (ref arrayCName ArrayList, ArrayList arrayID ref)
{
the SqlConnection CON = ADConnection.createConnection ();
the SqlCommand the SqlCommand cmd = new new ( "testProcedure_AX", CON);
cmd.CommandType = CommandType.StoredProcedure;

con.Open ();
the try
{
The SqlDataReader cmd.ExecuteReader DR = ();
the while (dr.Read ())
{
IF (DR [0] .ToString () == "")
{
arrayCName.Add (DR [. 1] .ToString ());
}
}
con.Close ();
return "! the OK";
}
the catch (Exception EX)
{
con.Close ();
return ex.ToString ();
}
}
Note: in fact, the previous
SqlCommand cmd = new SqlCommand ( "select userID from USERS order by userid desc ", con);
the SQL statements to replace the stored procedure name, and then type cmd labeled CommandType.StoredProcedure (stored procedures)

to write a stored procedure with parameters ⑤ it, a little above the simple miserable, but still quite useful.
parameter band took two, one did not face too petty.

PROCEDURE dbo.AXzhz the CREATE
/ *
Here write comment
* /
@startDate VARCHAR (16),
@EndDate VARCHAR (16)
AS
the SELECT from the above mentioned id table_AX the WHERE commentDateTime> @startDate and commentDateTime <@EndDate the Order by contentownerid DESC
Note: @startDate varchar ( 16) is a statement @startDate this variable, the variable name used across multiple [,] apart. SQL can use the back of the variables.

⑥ how I call this stored procedure with parameters in ASP.NET?

public static String GetCustomerCNameCount (startDate String, String endDate, the DataSet DS REF)
{
the SqlConnection ADConnection.createConnection = CON ();
// Note that this section ----------------------- -------------------------------------------
the SqlDataAdapter the SqlDataAdapter new new DA = ( " AXzhz ", con);
the SqlParameter = new new para0 ( "@ startDate", startDate);
para1 the SqlParameter new new = ( "@ endDate", endDate);
da.SelectCommand.Parameters.Add (para0);
da.SelectCommand.Parameters.Add (para1);
DA. = CommandType.StoredProcedure SelectCommand.CommandType;
// ---------------------------------------- ------------------------------------

the try
{
con.Open ();
da.Fill (DS) ;
con.Close ();
return "OK";
}
the catch (Exception EX)
{
return ex.ToString ();
}
}


Note: add parameters to the command to go, OK
birds, change the font color of something too much garbage , the change is not good, Minato live to see everyone.

⑦ I would like to see the successful implementation of the SQL command no.
Note the following three lines of red statement

the CREATE PROCEDURE dbo.AXzhz
/ *
@ parameter1 username
@ parameter2 new password
* /
@password nvarchar (20),
@username nvarchar (20)
AS
DECLARE @ Err0 int
Update WL_user the SET password = password @ UserName = @ userName the WHERE
the SET @@ error = @ Err0
the SELECT @ Err0 aS Err0
Note: declare an integer variable @ err0, give it a value for the @@ error (this is the statement given by the system automatically if executed successfully, 0 is successful, the other for the failure), and finally selected by select it, someone can say that the Supreme return to return, beyond the scope of my knowledge, I will not be temporary, add it later

⑧ how to get it back from the implementation of this ? the success of value do
the following piece of code can tell you the answer:
public static String GetCustomerCName ()
{
the SqlConnection CON = ADConnection.createConnection ();

SqlCommand cmd=new SqlCommand("AXzhz",con);
cmd.CommandType=CommandType.StoredProcedure;
para0=new SqlParameter("@startDate","2006-9-10");
para1=new SqlParameter("@endDate","2006-9-20");
da.SelectCommand.Parameters.Add(para0);
da.SelectCommand.Parameters.Add(para1);
con.Open();
try
{
Int32 re=(int32)cmd.ExecuteScalar(); //返回结果集中第一行第一列值
con.Close();
if (re==0)
return "OK!";
else
return "false";
}
catch(Exception ex)
{
con.Close();
return ex.ToString();
}
}

Note: that is, () methods to retrieve the SqlCommand ExecuteScalar by this value, this sentence is to find from the MSDN, I believe into:
int = Re (int) cmd.ExecuteScalar (); 99% correctly, have no time to verify and look forward to your test !!!

⑨ me to judge which of the SQL statement is executed according to the parameters passed !! ~
the following stored procedure to meet our requirements, turned out to be Pascal / VB wording, Begin ---- End, {not} ,,, for me in C #, this syntax is a bit sick .........
the ALTER PROCEDURE dbo.selectCustomerCNameCount
@CustomerID int
AS
IF @ = -1 customerID
the begin
the SELECT contentownerid, userCName, COUNT ( *) AS CountAll from view_usercomment Group by contentownerid, userCName the Order by contentownerid DESC
End
the else
the begin
select contentownerid ,userCName,count(*) as countAll from view_usercomment where contentownerid=@customerID group by contentownerid,userCName order by contentownerid DESC
end

Reproduced in: https: //www.cnblogs.com/guoxiaowen/archive/2008/01/29/1056968.html

Guess you like

Origin blog.csdn.net/weixin_33971130/article/details/93329967