(方法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();
}
}
{
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
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!");
}
类的调用方法:{
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)
(
@title varchar(50),
@content varchar(50)
)
as
insert into News values(@title,@content)
本人一向很谦虚,希望大家多多指教!;) QQ:772755070