Silverlight与MySQL数据库的互操作过程

发布时间:2021-08-19 18:24:16 作者:chen
来源:亿速云 阅读:111

本篇内容介绍了“Silverlight与MySQL数据库的互操作过程”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

准备工作

1)建立起测试项目

细节详情请见强大的DataGrid组件[2]_数据交互之ADO.NET Entity Framework——Silverlight学习笔记[10]。

2)创建测试用数据库

如下图所示,创建一个名为employees的MySQL数据库,建立数据表名称为Employee。

Silverlight与MySQL数据库的互操作过程

3)安装MySQL Connector Net 6.1.1 ★

为了能让.NET操作MySQL数据库,请务必安装。

建立数据模型

EmployeeModel.cs文件(放置在服务端项目文件夹下)

using System;  using System.Collections.Generic;  using System.Linq;  namespace dataformnmysqldb  {      public class EmployeeModel      {          public int EmployeeID { get; set; }          public string EmployeeName { get; set; }          public int EmployeeAge { get; set; }      }  }

建立服务端Web Service★

右击服务端项目文件夹,选择Add->New Item....,按下图所示建立一个名为EmployeesInfoWebService.asmx的Web Service,作为Silverlight与MySQL数据库互操作的桥梁。

Silverlight与MySQL数据库的互操作过程

在Silverlight客户端应用程序文件夹下,右击References文件夹,添加名为MySql.Data的命名空间。之后,双击EmployeesInfoWebService.asmx打开该文件,将里面的内容修改如下:

using System;  using System.Collections.Generic;  using System.Linq;  using System.Web;  using System.Web.Services;  using System.Data;  using MySql.Data.MySqlClient;//引入该命名空间是为了操作MySQL数据库  namespace dataformnmysqldb  {      ///      /// Summary description for EmployeesInfoWebService      ///     [WebService(Namespace = "http://tempuri.org/")]      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]      [System.ComponentModel.ToolboxItem(false)]      // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.       // [System.Web.Script.Services.ScriptService]      public class EmployeesInfoWebService : System.Web.Services.WebService      {          [WebMethod]//获取雇员信息          public List GetEmployeesInfo()          {              List returnedValue = new List();              MySqlCommand Cmd = new MySqlCommand();              SQLExcute("SELECT * FROM Employee", Cmd);              MySqlDataAdapter EmployeeAdapter = new MySqlDataAdapter();              EmployeeAdapter.SelectCommand = Cmd;              DataSet EmployeeDataSet = new DataSet();              EmployeeAdapter.Fill(EmployeeDataSet);              foreach (DataRow dr in EmployeeDataSet.Tables[0].Rows)              {                  EmployeeModel tmp = new EmployeeModel();                  tmp.EmployeeID = Convert.ToInt32(dr[0]);                  tmp.EmployeeName = Convert.ToString(dr[1]);                  tmp.EmployeeAge = Convert.ToInt32(dr[2]);                  returnedValue.Add(tmp);              }             return returnedValue;          }          [WebMethod] //添加雇员信息          public void Insert(List employee)          {              employee.ForEach(x =>              {                  string CmdText = "INSERT INTO Employee(EmployeeName,EmployeeAge) VALUES('" + x.EmployeeName + "'," + x.EmployeeAge.ToString() + ")";                  SQLExcute(CmdText);              });          }          [WebMethod] //更新雇员信息          public void Update(List employee)          {              employee.ForEach(x =>              {                  string CmdText = "UPDATE Employee SET EmployeeName='" + x.EmployeeName + "',EmployeeAge=" + x.EmployeeAge.ToString();                  CmdText += " WHERE EmployeeID=" + x.EmployeeID.ToString();                  SQLExcute(CmdText);              });          }          [WebMethod] //删除雇员信息          public void Delete(List employee)          {              employee.ForEach(x =>              {                  string CmdText = "DELETE FROM Employee WHERE EmployeeID=" + x.EmployeeID.ToString();                  SQLExcute(CmdText);              });          }          //执行SQL命令文本,重载1          private void SQLExcute(string SQLCmd)          {              string ConnectionString = "server=localhost;user id=root;password=yourpassword;database=employees";              MySqlConnection Conn = new MySqlConnection(ConnectionString);              Conn.Open();              MySqlCommand Cmd = new MySqlCommand();              Cmd.Connection = Conn;              Cmd.CommandTimeout = 15;              Cmd.CommandType = System.Data.CommandType.Text;              Cmd.CommandText = SQLCmd;              Cmd.ExecuteNonQuery();              Conn.Close();          }          //执行SQL命令文本,重载2          private void SQLExcute(string SQLCmd, MySqlCommand Cmd)          {              string ConnectionString = "server=localhost;user id=root;password= yourpassword;database=employees";              MySqlConnection Conn = new MySqlConnection(ConnectionString);              Conn.Open();              Cmd.Connection = Conn;              Cmd.CommandTimeout = 15;              Cmd.CommandType = System.Data.CommandType.Text;              Cmd.CommandText = SQLCmd;              Cmd.ExecuteNonQuery();         }      }  }

之后,在Silverlight客户端应用程序文件夹下,右击References文件夹,选择菜单选项Add Service Reference...。如下图所示,引入刚才我们创建的Web Service(别忘了按Discover按钮进行查找)。

Silverlight与MySQL数据库的互操作过程

创建Silverlight客户端应用程序

详情参见我的[原创]Silverlight与Access数据库的互操作(CURD完全解析)。

Silverlight与MySQL数据库互操作最终效果图

Silverlight与MySQL数据库的互操作过程 

“Silverlight与MySQL数据库的互操作过程”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Kotlin与Java互操作
  2. SilverLight Demo

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql silverlight 数据库

上一篇:SQL Server是什么

下一篇:php怎么修改上传图片尺寸

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》