跳转到主内容
趣航编程网 - 趣学编程,启航技术之路!

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

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:MySQL(Linux)

MySQL 9.6.0是面向Linux平台的2026年创新版本,核心架构迎来重大革新。其将外键约束与级联操作从InnoDB引擎层上移至SQL层,确保所有数据变更均被完整记录至Binlog,彻底解决了CDC(变更数据捕获)与主从复制中的数据不一致难题。此外,该版本引入container_aware启动选项以原生适配容器环境,并对审计日志进行了组件化重构,为追求极致数据一致性与云原生体验的开发者提供了全新选择。

下载

相关文章