search
HomeDatabaseMysql Tutorial 老外的.net与mysql存储过程编程
老外的.net与mysql存储过程编程Jun 07, 2016 pm 02:52 PM
.netmysqlthestorageprogrammingprocess

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

  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 ssage (    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 ssageWHERE 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  ssage;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 "nfig" and change the line:

  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:


  

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

分享几个.NET开源的AI和LLM相关项目框架分享几个.NET开源的AI和LLM相关项目框架May 06, 2024 pm 04:43 PM

当今人工智能(AI)技术的发展如火如荼,它们在各个领域都展现出了巨大的潜力和影响力。今天大姚给大家分享4个.NET开源的AI模型LLM相关的项目框架,希望能为大家提供一些参考。https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel是一种开源的软件开发工具包(SDK),旨在将大型语言模型(LLM)如OpenAI、Azure

C#的就业前景如何C#的就业前景如何Oct 19, 2023 am 11:02 AM

无论您是初学者还是有经验的专业人士,掌握C#将为您的职业发展铺平道路。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.