Example of calling a stored procedure

Source: Internet
Author: User
Q:
I created a stored procedure named may_user. The input parameter is @ user varchar (20) and @ passw varchar (20 );
I don't know how to call it in asp.net? Thank you
______________________________________________________________________________________________
Answer 1:
SqlConnection nwindConn = new SqlConnection ("Data Source = localhost; Integrated Security = SSPI; Initial Catalog = northwind ");

SqlCommand salesCMD = new SqlCommand ("SalesByCategory", nwindConn );
SalesCMD. CommandType = CommandType. StoredProcedure;

Example
SqlParameter myParm = salesCMD. Parameters. Add ("@ CategoryName", SqlDbType. NVarChar, 15 );
MyParm. Value = "Beverages ";

NwindConn. Open ();

SqlDataReader myReader = salesCMD. ExecuteReader ();

Console. WriteLine ("{0}, {1}", myReader. GetName (0), myReader. GetName (1 ));

While (myReader. Read ())
{
Console. WriteLine ("{0}, $ {1}", myReader. GetString (0), myReader. GetDecimal (1 ));
}

MyReader. Close ();
NwindConn. Close ();
______________________________________________________________________________________________
Answer 2:
MyCommand = new SqlCommand ("Client_UpdateClients", myConnection );

// Mark the Command as a SPROC
MyCommand. CommandType = CommandType. StoredProcedure;

// Add Parameters to SPROC
MyCommand. Parameters. Add ("@ ID", SqlDbType. Int). Value = client. ID;
MyCommand. Parameters. Add ("@ ClientCode", SqlDbType. VarChar, 20). Value = client. ClientCode;
MyCommand. Parameters. Add ("@ ShortName", SqlDbType. VarChar, 20). Value = client. ShortName;
MyCommand. Parameters. Add ("@ LongName", SqlDbType. VarChar, 50). Value = client. LongName;
MyCommand. Parameters. Add ("@ Province", SqlDbType. VarChar, 20). Value = client. Province;
MyCommand. Parameters. Add ("@ County", SqlDbType. VarChar, 20). Value = client. County;
MyCommand. Parameters. Add ("@ EnterpriseTypes", SqlDbType. VarChar, 20). Value = client. EnterpriseTypes;
MyCommand. Parameters. Add ("@ ClientTypes", SqlDbType. VarChar, 20). Value = client. ClientTypes;
MyCommand. Parameters. Add ("@ Address", SqlDbType. VarChar, 50). Value = client. Address;
MyCommand. Parameters. Add ("@ Telephone", SqlDbType. VarChar, 20). Value = client. Telephone;
MyCommand. Parameters. Add ("@ Fax", SqlDbType. VarChar, 20). Value = client. Fax;
MyCommand. Parameters. Add ("@ Email", SqlDbType. VarChar, 50). Value = client. Email;
MyCommand. Parameters. Add ("@ PostCode", SqlDbType. VarChar, 20). Value = client. PostCode;
MyCommand. Parameters. Add ("@ Account", SqlDbType. VarChar, 20). Value = client. Account;
MyCommand. Parameters. Add ("@ Taxno", SqlDbType. VarChar, 20). Value = client. Taxno;
MyCommand. Parameters. Add ("@ Contactor", SqlDbType. VarChar, 20). Value = client. Contactor;
MyCommand. Parameters. Add ("@ HandSet", SqlDbType. VarChar, 50). Value = client. HandSet;
MyCommand. Parameters. Add ("@ ReMark", SqlDbType. Text). Value = client. ReMark;
MyCommand. Parameters. Add ("@ Credit", SqlDbType. VarChar, 20). Value = client. Credit;

// Execute the command
MyConnection. Open ();

MyCommand. ExecuteNonQuery ();

MyConnection. Close ();
______________________________________________________________________________________________
Answer 3:
Public DataSet GetAllEditiones ()
{
SqlDataAdapter sqlDAArticleEditionesGet = new SqlDataAdapter ("Article_AllEditiones_Get", DataBaseConnection );
SqlDAArticleEditionesGet. SelectCommand. CommandType = CommandType. StoredProcedure;

DataSet sqlDSArticleEditionesGet = new DataSet ();
SqlDAArticleEditionesGet. Fill (sqlDSArticleEditionesGet );

Return sqlDSArticleEditionesGet;
}
______________________________________________________________________________________________
Answer 4:
String SQL = "[procedure name]"
SqlConnection conn = new SqlConnection ("...");
SqlCommand comm = new (SQL, conn );
Comm. CommandType = CommandType. StoredProcedure
Comm. Parameters. Add (new SqlParamenter ("@ Year", SqlDbType. Int, 4 ));
Comm. Parameters ["@ Year"]. value = 2003

______________________________________________________________________________________________
Answer 5:
// Create procedure Article_Editiones_Get @ editionPosition int
// SELECT
//*
// FROM
// Article_Editiones
// WHERE EditionPosition = @ editionPosition
// GO
Public DataSet GetEditiones (int editionPosition)
{
SqlDataAdapter sqlDAArticleEditionesGet = new SqlDataAdapter ("Article_Editiones_Get", DataBaseConnection );
SqlDAArticleEditionesGet. SelectCommand. CommandType = CommandType. StoredProcedure;

SqlDAArticleEditionesGet. SelectCommand. Parameters. Add (new SqlParameter ("@ EditionPosition", SqlDbType. Int, 4 ));
SqlDAArticleEditionesGet. SelectCommand. Parameters ["@ EditionPosition"]. Value = editionPosition;

DataSet sqlDSArticleEditionesGet = new DataSet ();
SqlDAArticleEditionesGet. Fill (sqlDSArticleEditionesGet );

Return sqlDSArticleEditionesGet;
}
______________________________________________________________________________________________
Answer 6:
Com. CommandType = CommandType. StoredProcedure
Com. ExcuteNoquery ()
______________________________________________________________________________________________
Answer 7:
To call your stored procedure, you only need two lines of code,
You do not need to write the code upstairs.

Http://expert.csdn.net/Expert/topic/2292/2292594.xml? Temp = 7.053775E-02

______________________________________________________________________________________________
Answer 8:
Sqlcon = new SqlConnection (System. Configuration. ConfigurationSettings. deleettings [0]);

Strcom = "exec P_Del" + "'" + Request. queryString ["t1"] + "','" + Request. queryString ["t2"] + "','" + Request. queryString ["t3"] + "','" + Convert. toDateTime (Request. queryString ["DOld"]) + "','" + Convert. toDateTime (Request. queryString ["DNew"]) + "'";

SqlCommand sqlcom = new SqlCommand (strcom, sqlcon );
Sqlcon. Open ();
Sqlcom. ExecuteNonQuery ();

It can be used in the same way as the SQL query analyzer.
______________________________________________________________________________________________
Answer 11:
Call mycomm. CommandType = CommandType. StoredProcedure; do you want to reference any namespace?
______________________________________________________________________________________________
Answer 12:
System. Data
System. Data. SqlClient
______________________________________________________________________________________________
Answer 13:
I have referenced the two, but I don't know why he has the following error. Please advise me.
The system reports that the mark "=" in the class, structure, or interface member declaration is invalid.
SalesCMD. CommandType = CommandType. StoredProcedure;

______________________________________________________________________________________________

Answer 15:
Protected Friend Function ExecuteDataTable (ByRef rsTmp As DataTable, ByVal spName As String, ByVal aryParams () As SqlParameter) As Boolean
Dim _ tmpCmd As SqlCommand
Dim _ tmpDS As New DataSet
Dim _ tmpSDA As SqlDataAdapter
Dim returnTmp As Boolean

ObjConn = New SqlConnection (ConnectionString)
_ TmpCmd = New SqlCommand (spName, objConn)
_ TmpCmd. CommandType = CommandType. StoredProcedure

Dim I As Integer
For I = 0 To aryParams. Length-1
_ TmpCmd. Parameters. Add (aryParams (I ))
Next

Try
ObjConn. Open ()
_ TmpSDA = New SqlDataAdapter (_ tmpCmd)
_ TmpSDA. Fill (_ tmpDS, "myTable ")
Catch ex As System. Data. SqlClient. SqlException
Throw ex
ReturnTmp = False
Finally
_ TmpSDA. Dispose ()
_ TmpCmd. Dispose ()
_ TmpSDA = Nothing
_ TmpCmd = Nothing
ObjConn. Dispose ()
ObjConn = Nothing
RsTmp = _ tmpDS. Tables ("myTable ")
ReturnTmp = True
End Try

Return returnTmp
End Function

Public Function StatisticStock (ByVal NO As String ,_
ByVal TableName As String ,_
ByVal Action As ActionStatus) As Integer

Dim dbTmp As New DBCls
Dim _ tmpParam () As SqlParameter
ReDim _ tmpParam (3)
Dim returnTmp As Integer
Dim _ tmpTab As DataTable

_ TmpParam (0) = New SqlParameter ("@ NO", SqlDbType. Char, 10)
_ TmpParam (0). Value = NO
_ TmpParam (1) = New SqlParameter ("@ TableName", SqlDbType. Char, 20)
_ TmpParam (1). Value = TableName
_ TmpParam (2) = New SqlParameter ("@ Action", SqlDbType. Char)
_ TmpParam (2). Value = Action. ToString
_ TmpParam (3) = New SqlParameter ("return_value", SqlDbType. Int)
_ TmpParam (3). Direction = ParameterDirection. ReturnValue

If dbTmp. ExecuteDataTable (_ tmpTab, "Storage_StatisticStock", _ tmpParam) Then
ReturnTmp = _ tmpParam (3). Value
_ TmpParam = Nothing
DbTmp = Nothing
Return returnTmp
Else
Return 0
End If
End Function

______________________________________________________________________________________________
Answer 16:
Maybe the reason why I pasted it into the fullwidth is
You can replace "= ".
Namespace reference
Using System. Data. SqlClient;

______________________________________________________________________________________________
Answer 17:
SqlConnection nwindConn = new SqlConnection ("Data Source = localhost; Integrated Security = SSPI; Initial Catalog = northwind ");
// SalesByCategory stored procedure name
SqlCommand salesCMD = new SqlCommand ("SalesByCategory", nwindConn );
SalesCMD. CommandType = CommandType. StoredProcedure;

// Add parameter: name, type, length
SqlParameter myParm = salesCMD. Parameters. Add ("@ CategoryName", SqlDbType. NVarChar, 15 );
// Input parameter value
MyParm. Value = "Beverages ";

NwindConn. Open ();
// Execute
SqlDataReader myReader = salesCMD. ExecuteReader ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.