分享
 
 
 

老外的.net与mysql存储过程编程

王朝mysql·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

created this example because I could not find a simple explanation for using MySQL 5 with ObjectDataSources in ASP.NET 2.0.

Introduction

i created this example because I could not find a simple explanation for using MySQL 5 with ObjectDataSources in ASP.NET 2.0.

let me say, I am really impressed with MySQL. I was able to install it easily on my Windows XP machine and get it running in about an hour. I am a long time MS SQL user, and was very frustrated with trying to use Oracle and Firebird. I realize, the problem is that I am spoiled from MS SQL Server, but hey I'm busy and I like easy to use tools :)

if you're getting started with MySQL and ASP.NET, then I recommend these steps:

Go to the MySQL website, download and install “Current Release (recommended)”.

Download and install: MySQL Administrator (to administer your MySQL server, the first download just installs only the server).

Download and install: Connector/Net 1.0 (you need this to get your ASP.NET pages to talk to your MySQL server).

You can also download: MySQL Query Browser – (a graphical client to work with your MySQL databases and run queries).

Read and follow this guide: A Step-by-Step Guide to Using MySQL with ASP.NET.

To install the code:

You must have MySQL 5 up and running.

Install MySQL Connector/Net 1.0.

Create a MySQL 5 database named Test.

Create a table in that database called Message:

CREATE TABLE test.message ( Entry_ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, Name VARCHAR(45), Email VARCHAR(45), Message VARCHAR(200), PRIMARY KEY (Entry_ID) ) AUTO_INCREMENT=32 CHARACTER SET latin1 COLLATE latin1_swedish_ci;

Create these four MySQL stored procedures in the Test database:

PROCEDURE `test`.`DeleteMessage`(IN param1 INT)BEGINDelete From test.messageWHERE Entry_ID = param1;END

PROCEDURE `test`.`InsertMessage`(IN param1 VARCHAR(50), IN param2 VARCHAR(50), IN param3 VARCHAR(200))BEGININSERT INTO message(Name, Email, Message)VALUES(param1,param2,param3);END

PROCEDURE `test`.`ShowAll`()BEGINSELECT message.Entry_ID, message.Name, message.Email, message.MessageFROM test.message;END

PROCEDURE `test`.`UpdateMessage`(IN paramkey INT, IN param1 VARCHAR(50), IN param2 VARCHAR(50), IN param3 VARCHAR(200))BEGINUPDATE messageSET Name = param1, Email = param2, Message = param3WHERE (message.Entry_ID = paramkey);END

Unzip "MySQL" and configure IIS to point to it. Make sure you configure the web server to use ASP.NET 2.0.

Open "web.config" and change the line:

to connect to your MySQL database.

Browse to the default.aspx page through IIS.

this is the class that uses Generics to supply the data that is consumed by the ObjectDataSource control:

using System;using System.Collections.Generic;using System.Data;using MySql.Data.MySqlClient;using System.Configuration;using System.ComponentModel;[DataObject(true)]public static class MessagesDB{ private static string GetConnectionString() { return ConfigurationManager.ConnectionStrings ["MySQLConnectionString"].ConnectionString; } [DataObjectMethod(DataObjectMethodType.Select)] public static List GetMessages() { MySqlCommand cmd = new MySqlCommand("ShowAll", new MySqlConnection(GetConnectionString())); cmd.CommandType = CommandType.StoredProcedure; cmd.Connection.Open(); MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); List MessageItemlist = new List(); while (dr.Read()) { MessageItem MessageItem = new MessageItem(); MessageItem.Entry_ID = Convert.ToInt32(dr["Entry_ID"]); MessageItem.Message = Convert.ToString(dr["Message"]); MessageItem.Name = Convert.ToString(dr["Name"]); MessageItem.Email = Convert.ToString(dr["Email"]); MessageItemlist.Add(MessageItem); } dr.Close(); return MessageItemlist; } [DataObjectMethod(DataObjectMethodType.Insert)] public static void InsertMessage(MessageItem MessageItem) { MySqlCommand cmd = new MySqlCommand("InsertMessage", new MySqlConnection(GetConnectionString())); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new MySqlParameter("param1", MessageItem.Name)); cmd.Parameters.Add(new MySqlParameter("param2", MessageItem.Email)); cmd.Parameters.Add(new MySqlParameter("param3", MessageItem.Message)); cmd.Connection.Open(); cmd.ExecuteNonQuery(); cmd.Connection.Close(); } [DataObjectMethod(DataObjectMethodType.Update)] public static int UpdateMessage(MessageItem MessageItem) { MySqlCommand cmd = new MySqlCommand("UpdateMessage", new MySqlConnection(GetConnectionString())); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new MySqlParameter("paramkey", MessageItem.Entry_ID)); cmd.Parameters.Add(new MySqlParameter("param1", MessageItem.Name)); cmd.Parameters.Add(new MySqlParameter("param2", MessageItem.Email)); cmd.Parameters.Add(new MySqlParameter("param3", MessageItem.Message)); cmd.Connection.Open(); int i = cmd.ExecuteNonQuery(); cmd.Connection.Close(); return i; } [DataObjectMethod(DataObjectMethodType.Delete)] public static int DeleteMessage(MessageItem MessageItem) { MySqlCommand cmd = new MySqlCommand("DeleteMessage", new MySqlConnection(GetConnectionString())); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new MySqlParameter("param1", MessageItem.Entry_ID)); cmd.Connection.Open(); int i = cmd.ExecuteNonQuery(); cmd.Connection.Close(); return i; }

the class above uses the class "MessageItem" to pass the parameters to and from the ObjectDataSource control:

using System;public class MessageItem{ int _Entry_ID; string _Message; string _Name; string _Email; public MessageItem() { } public int Entry_ID { get { return _Entry_ID; } set { _Entry_ID = value; } } public string Message { get { return _Message; } set { _Message = value; } } public string Name { get { return _Name; } set { _Name = value; } } public string Email { get { return _Email; } set { _Email = value; } }}

this is the .aspx file that contains the ObjectDataSource control as well as a GridView for editing data and a DetailsView for inserting a record:

Download source - 65.3 Kb

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有