(方法1)ASP.NET如何調用MySQL的預存程序
由於項目中需要在ASP.NET中如何調用MySQL的預存程序,
百度和Google後發現中文的網站講解也不是很清楚。
沒有辦法,只有自己動手。折騰一下午終於搞定,所以拿出來分享!
調用的預存程序雖然簡單,但是麻雀雖小,五髒俱全.呵呵;)
首先必須下載支援.NET的驅動,有兩種策略: 使用ODBC或Connector/Net
至於這兩種方法中那種快些,查看網上的文章說是使用ODBC連接用時24秒,Connector/Net連接用時17秒
本人並沒有去求證,但使用Connector/Net是肯定要方便的,呵呵。
Mysql的connector/net5.0:
http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-5.0.6.zip/from/pick
安裝好以後,
點屬性,然後點尋找目標,點向上一層目錄,找到Binaries\.NET 2.0,然後將這個檔案複製到你的工程目錄下,一般這樣的DLL檔案會儲存到bin目錄下.
aspx頁面UI層次的顯示等就不講解了,看看所謂的商務邏輯層的串連吧。。。。
procedure.cs類檔案中:
public void Delete_procedure() //“刪除”的預存程序
{
string str_orgid = Client_Str; //獲得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"; //預存程序名
//myComm.Parameters.Add("m_orgid", str_orgid);
myComm.ExecuteNonQuery();
}
catch
{
myComm.Connection.Close();
myComm.Dispose();
}
finally
{
myComm.Connection.Close();
myComm.Dispose();
}
}
資料庫當然是要自己建立的,由於“刪除”的這個預存程序比較簡單,能讓大家容易理解
這個預存程序就是實現統一刪除每一個相關資料表中的一條記錄,只用了傳遞ID這一個參數。
Mysql的簡單預存程序:CREATE PROCEDURE `DELETEDB`(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
(方法2)ASP.NET如何調用MS_SQL的預存程序
今天是國慶假期的第2天(2007.10.2),看見一個不錯的方法,特地轉載過來,讓大家更加瞭解預存程序; )
終於實現了預存程序的添加
後台代碼:
protected void Button1_Click(object sender, EventArgs e)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@title",TextBox1.Text),
new SqlParameter("@content", TextBox2.Text)
};
sdh.RunProcedure("[AddNew]", parameters, out rowsAffected);
Response.Write("OK!");
}
類的調用方法:
/**//// <summary>
/// 執行預存程序,返回影響的行數
/// </summary>
/// <param name="storedProcName">預存程序名</param>
/// <param name="parameters">預存程序參數</param>
/// <param name="rowsAffected">影響的行數</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;
}
}
資料庫中的代碼:create proc AddNew
(
@title varchar(50),
@content varchar(50)
)
as
insert into News values(@title,@content)
本人一向很謙虛,希望大家多多指教!;) QQ:772755070