详解Silverlight与MySQL数据库互操作

开发 后端
这里将谈到Silverlight与MySQL数据库互操作,首先将从建立测试项目,创建测试用数据库开始,然后是对数据库的操作。
Silverlight与MySQL数据库互操作,也是因为MySQL在平时开发中应用得很多,适合大多数开发环境与要求。本文用到的MySQL环境是MySQL Connector Net 6.1.1,需要大家提前安装。

准备工作

1)建立起测试项目

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

2)创建测试用数据库

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

创建测试用数据库

3)安装MySQL Connector Net 6.1.1 ★

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

建立数据模型

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. namespace dataformnmysqldb  
  5. {  
  6.     public class EmployeeModel  
  7.     {  
  8.         public int EmployeeID { get; set; }  
  9.         public string EmployeeName { get; set; }  
  10.         public int EmployeeAge { get; set; }  
  11.     }  

建立服务端Web Service★

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

建立服务端Web Service 

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.Data;  
  7. using MySql.Data.MySqlClient;//引入该命名空间是为了操作MySQL数据库  
  8. namespace dataformnmysqldb  
  9. {  
  10.     ///   
  11.     /// Summary description for EmployeesInfoWebService  
  12.     /// 
  13.  
  14.    [WebService(Namespace = "http://tempuri.org/")]  
  15.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  16.     [System.ComponentModel.ToolboxItem(false)]  
  17.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  18.     // [System.Web.Script.Services.ScriptService]  
  19.     public class EmployeesInfoWebService : System.Web.Services.WebService  
  20.     {  
  21.         [WebMethod]//获取雇员信息  
  22.         public List GetEmployeesInfo()  
  23.         {  
  24.             List returnedValue = new List();  
  25.             MySqlCommand Cmd = new MySqlCommand();  
  26.             SQLExcute("SELECT * FROM Employee", Cmd);  
  27.             MySqlDataAdapter EmployeeAdapter = new MySqlDataAdapter();  
  28.             EmployeeAdapter.SelectCommand = Cmd;  
  29.             DataSet EmployeeDataSet = new DataSet();  
  30.             EmployeeAdapter.Fill(EmployeeDataSet);  
  31.             foreach (DataRow dr in EmployeeDataSet.Tables[0].Rows)  
  32.             {  
  33.                 EmployeeModel tmp = new EmployeeModel();  
  34.                 tmp.EmployeeID = Convert.ToInt32(dr[0]);  
  35.                 tmp.EmployeeName = Convert.ToString(dr[1]);  
  36.                 tmp.EmployeeAge = Convert.ToInt32(dr[2]);  
  37.                 returnedValue.Add(tmp);  
  38.             }  
  39.            return returnedValue;  
  40.         }  
  41.         [WebMethod] //添加雇员信息  
  42.         public void Insert(List employee)  
  43.         {  
  44.             employee.ForEach(x =>  
  45.             {  
  46.                 string CmdText = "INSERT INTO Employee(EmployeeName,EmployeeAge) VALUES('" + x.EmployeeName + "'," + x.EmployeeAge.ToString() + ")";  
  47.                 SQLExcute(CmdText);  
  48.             });  
  49.         }  
  50.         [WebMethod] //更新雇员信息  
  51.         public void Update(List employee)  
  52.         {  
  53.             employee.ForEach(x =>  
  54.             {  
  55.                 string CmdText = "UPDATE Employee SET EmployeeName='" + x.EmployeeName + "',EmployeeAge=" + x.EmployeeAge.ToString();  
  56.                 CmdText += " WHERE EmployeeID=" + x.EmployeeID.ToString();  
  57.                 SQLExcute(CmdText);  
  58.             });  
  59.         }  
  60.         [WebMethod] //删除雇员信息  
  61.         public void Delete(List employee)  
  62.         {  
  63.             employee.ForEach(x =>  
  64.             {  
  65.                 string CmdText = "DELETE FROM Employee WHERE EmployeeID=" + x.EmployeeID.ToString();  
  66.                 SQLExcute(CmdText);  
  67.             });  
  68.         }  
  69.         //执行SQL命令文本,重载1  
  70.         private void SQLExcute(string SQLCmd)  
  71.         {  
  72.             string ConnectionString = "server=localhost;user id=root;password=yourpassword;database=employees";  
  73.             MySqlConnection Conn = new MySqlConnection(ConnectionString);  
  74.             Conn.Open();  
  75.             MySqlCommand Cmd = new MySqlCommand();  
  76.             Cmd.Connection = Conn;  
  77.             Cmd.CommandTimeout = 15;  
  78.             Cmd.CommandType = System.Data.CommandType.Text;  
  79.             Cmd.CommandText = SQLCmd;  
  80.             Cmd.ExecuteNonQuery();  
  81.             Conn.Close();  
  82.         }  
  83.         //执行SQL命令文本,重载2  
  84.         private void SQLExcute(string SQLCmd, MySqlCommand Cmd)  
  85.         {  
  86.             string ConnectionString = "server=localhost;user id=root;password= yourpassword;database=employees";  
  87.             MySqlConnection Conn = new MySqlConnection(ConnectionString);  
  88.             Conn.Open();  
  89.             Cmd.Connection = Conn;  
  90.             Cmd.CommandTimeout = 15;  
  91.             Cmd.CommandType = System.Data.CommandType.Text;  
  92.             Cmd.CommandText = SQLCmd;  
  93.             Cmd.ExecuteNonQuery();  
  94.        }  
  95.     }  

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

创建的Web Service 

创建Silverlight客户端应用程序

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

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

最终效果图 
 

原文标题:Silverlight与MySQL数据库的互操作(CURD完全解析)

链接:http://www.cnblogs.com/Kinglee/archive/2009/09/06/1561452.html

【编辑推荐】

  1. Office 2010将使用Silverlight改善用户体验
  2. 微软.NET平台主管谈Silverlight企业级开发
  3. Flash与Silverlight多领域实测对比
  4. 微软宣称Silverlight装机量超过三亿
  5. 图解Silverlight 3的7个新功能
责任编辑:彭凡 来源: 博客园
相关推荐

2009-09-07 13:25:56

Silverlight

2009-12-31 11:10:01

2009-04-24 14:38:09

SunMySQL开源

2009-04-24 09:03:59

SunMySQL互操作性

2010-07-05 10:44:35

SQL Server数

2010-04-30 13:26:50

Oracle数据库

2011-08-30 13:40:28

MySQL线程

2011-04-19 11:02:57

数据库分页

2009-07-28 14:16:31

ASP.NET与MyS

2024-04-03 00:06:03

2010-05-20 16:35:12

2010-05-28 09:15:50

配置MySQL

2011-08-30 14:25:06

QT数据库

2010-05-31 17:18:39

Cassandra数据

2022-03-29 10:52:08

MySQL数据库

2010-03-29 10:19:24

2010-06-01 12:51:23

MySQL数据库

2019-10-21 13:52:14

MySQL数据库命令

2010-06-12 17:48:45

MySQL数据库表

2010-06-01 10:47:21

连接MySQL数据库
点赞
收藏

51CTO技术栈公众号