(Method 1) how to call ASP. NETMySQLStored Procedures
Because the project needs to call the MySQL stored procedure in ASP. NET,
Baidu and Google found that the explanation of Chinese websites is not very clear.
No way. You only need to do it yourself. I finally finished it all afternoon, so I will share it with you!
The stored procedure of the call is simple, but the sparrow is small and dirty. Haha ;)
First, you must download a driver that supports. net. There are two policies: ODBC or connector/net.
For the two methods, viewArticleIt is 24 seconds for ODBC connection and 17 seconds for ctor/net connection.
I did not verify it, but it must be convenient to use connector/net.
MySQL connector/net5.0:
Http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-5.0.6.zip/from/pick
After installation,
Click properties, then find the target, click a directory to the upper level, and find binaries \. NET 2.0, and then copy this file to your project directory. Generally, such DLL files will be saved to the bin directory.
I will not explain the display of the UI Layer of the ASPX page. Let's look at the connection of the so-called business logic layer ....
Procedure. CS files:
Public Void Delete_procedure () // "Delete" Stored Procedure
{
String Str_orgid = Client_str; // Obtain orgid
String Myconn_str = Webconfigurationmanager. connectionstrings [ " Mysqlconnectionstring " ]. Connectionstring;
Mysqlconnection myconn = New Mysqlconnection (myconn_str );
Mysqlcommand mycomm = New Mysqlcommand ( " Deletedb " , Myconn ); // (Client_str );
// Mycomm. Connection = myconn;
Try
{
Mycomm. Connection. open ();
Mycomm. commandtype = Commandtype. storedprocedure;
Mysqlparameter myparameter;
Myparameter = New Mysqlparameter ( " ? M_orgid " , Mysqldbtype. String );
Myparameter. Value = Str_orgid;
Myparameter. Direction = Parameterdirection. input;
Mycomm. Parameters. Add (myparameter );
// Mycomm. commandtext = "deletedb "; // Stored Procedure name
// Mycomm. Parameters. Add ("m_orgid", str_orgid );
Mycomm. executenonquery ();
}
Catch
{
Mycomm. Connection. Close ();
Mycomm. Dispose ();
}
Finally
{
Mycomm. Connection. Close ();
Mycomm. Dispose ();
}
}
Of course, databases must be built on their own. Because the "delete" stored procedure is relatively simple, it is easy for everyone to understand.
This stored procedure is to delete a record in each related data table in a unified manner. Only the parameter ID is used.
Simple MySQL stored procedure:Create Procedure 'Deleteb '( In M_orgid Char ( 12 ))
Begin
Delete From Hardwareinfo Where Orgid = M_orgid;
Delete From Addressinfo Where Orgid = M_orgid;
Delete From Netspeedinfo Where Orgid = M_orgid;
Delete From Memoryinfo Where Orgid = M_orgid;
Delete From Urlinfo Where Orgid = M_orgid;
Delete From Urluserinfo Where Orgid = M_orgid;
Delete From Orderinfo Where Orgid = M_orgid;
Delete From Softinfo Where Orgid = M_orgid;
Delete From Orginfo Where Orgid = M_orgid;
Delete From Harddiskinfo Where Orgid = M_orgid;
Delete From Hotfixinfo Where Orgid = M_orgid;
Delete From Userinfo Where Orgid = M_orgid;
Delete From Softuserinfo Where Orgid = M_orgid;
Delete From Systeminfo Where Orgid = M_orgid;
End
(Method 2) how to call ASP. NETMs_ SQLStored Procedures
Today is the 2nd day of the National Day holiday (2007.10.2). I saw a good method and specially reproduced it to help you better understand the stored procedure ;)
Finally, the storage process is added.
BackgroundCode:
Protected Void Button#click ( Object Sender, eventargs E)
{
Int Rowsaffected;
Sqlparameter [] Parameters = {< br> New sqlparameter ( " @ title " , textbox1.text),
New sqlparameter ( " @ content " , textbox2.text)
} ;
SDH. runprocedure ( " [Addnew] " , Parameters, Out Rowsaffected );
Response. Write ( " OK! " );
}
Class call method:
/**/ /// <Summary>
/// Executes the stored procedure and returns the number of affected rows.
/// </Summary>
/// <Param name = "storedprocname"> Stored Procedure name </Param>
/// <Param name = "Parameters"> Stored Procedure Parameters </Param>
/// <Param name = "rowsaffected"> Number of affected rows </Param>
/// <Returns> </returns>
Public Int Runprocedure ( String Storedprocname, idataparameter [] parameters, Out Int Rowsaffected)
{
Using (Sqlconnection connection = New Sqlconnection (strconn ))
{
Int Result;
Connection. open ();
Sqlcommand command = Buildintcommand (connection, storedprocname, parameters );
Rowsaffected = Command. executenonquery ();
Result = ( Int ) Command. Parameters [ " Returnvalue " ]. Value;
// Connection. Close ();
Return Result;
}
}
Code in the database: Create Proc Addnew
(
@ Title Varchar ( 50 ),
@ Content Varchar ( 50 )
)
As
Insert Into News Values ( @ Title , @ Content )
I have always been humble. I hope you can give me more advice !;) QQ: 772755070