0% found this document useful (0 votes)
47 views

Create These Four Mysql Stored Procedures in The Test Database

The document describes how to create a MySQL database table and stored procedures to manage messages. It then provides ASP.NET code to connect to the database and allow inserting, updating, and deleting messages through a web interface using a GridView and DetailsView control bound to an ObjectDataSource. The code handles passing parameters between the controls and stored procedures.

Uploaded by

akosiairish
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Create These Four Mysql Stored Procedures in The Test Database

The document describes how to create a MySQL database table and stored procedures to manage messages. It then provides ASP.NET code to connect to the database and allow inserting, updating, and deleting messages through a web interface using a GridView and DetailsView control bound to an ObjectDataSource. The code handles passing parameters between the controls and stored procedures.

Uploaded by

akosiairish
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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;

1. Create these four MySQL stored procedures in the Test database:


Collapse | Copy Code

PROCEDURE `test`.`DeleteMessage`(IN param1 INT) BEGIN Delete From test.message WHERE Entry_ID = param1; END
Collapse | Copy Code

PROCEDURE `test`.`InsertMessage`(IN param1 VARCHAR(50), IN param2 VARCHAR(50), IN param3 VARCHAR(200)) BEGIN INSERT INTO message(Name, Email, Message) VALUES(param1,param2,param3); END
Collapse | Copy Code

PROCEDURE `test`.`ShowAll`() BEGIN SELECT message.Entry_ID, message.Name, message.Email, message.Message FROM test.message; END
Collapse | Copy Code

PROCEDURE `test`.`UpdateMessage`(IN paramkey INT, IN param1 VARCHAR(50), IN param2 VARCHAR(50), IN param3 VARCHAR(200)) BEGIN UPDATE message SET Name = param1, Email = param2, Message = param3 WHERE (message.Entry_ID = paramkey); END

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

3. Open "web.config" and change the line:


Collapse | Copy Code

<add name="MySQLConnectionString" connectionString="server=localhost; user id=myuser; password=mypass; database=test; pooling=false;" providerName="MySql.Data.MySqlClient"/>

to connect to your MySQL database. 4. 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:
Collapse | Copy Code

using using using using using using

System; System.Collections.Generic; System.Data; MySql.Data.MySqlClient; System.Configuration; System.ComponentModel;

[DataObject(true)] public static class MessagesDB { private static string GetConnectionString() { return ConfigurationManager.ConnectionStrings ["MySQLConnectionString"].ConnectionString; } [DataObjectMethod(DataObjectMethodType.Select)] public static List<MessageItem> GetMessages() { MySqlCommand cmd = new MySqlCommand("ShowAll", new MySqlConnection(GetConnectionString())); cmd.CommandType = CommandType.StoredProcedure; cmd.Connection.Open(); MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); List<MessageItem> MessageItemlist = new List<MessageItem>(); 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 ObjectDataSourcecontrol:
Collapse | Copy Code

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 aDetailsView for inserting a record:
Collapse | Copy Code

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="MessagesDB" OldValuesParameterFormatString="original_{0}" SelectMethod="GetMessages" DataObjectTypeName="MessageItem" DeleteMethod="DeleteMessage" InsertMethod="InsertMessage" UpdateMethod="UpdateMessage"> </asp:ObjectDataSource> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" DataKeyNames="Entry_ID"> <Columns>

<asp:BoundField DataField="Entry_ID" HeaderText="Entry_ID" SortExpression="Entry_ID" Visible="False" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" /> </Columns> </asp:GridView> <br /> <strong><span style="text-decoration: underline"> Insert New Record:</span></strong><br /> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BorderStyle="None" CellSpacing="5" DataSourceID="ObjectDataSource1" DefaultMode="Insert" GridLines="None" Height="50px" Width="300px"> <Fields> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" /> <asp:BoundField DataField="Message" HeaderText="Message" /> <asp:CommandField ButtonType="Button" ShowInsertButton="True" ShowCancelButton="False" /> </Fields> </asp:DetailsView>

You might also like