silverlight读取mysql_[原创]Silverlight与MySQL数据库的互操作(CURD完全解析)

本文将为大家介绍如何让Silverlight使用MySQL作为后台数据库以及CURD操作。

准备工作

1)建立起测试项目

2)创建测试用数据库

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

6c8402f1cca6629b419bd37e6011efa7.png

3)安装MySQL Connector Net 6.1.1★

为了能让.NET操作MySQL数据库,请务必安装。【点击:】

建立数据模型

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

namespacedataformnmysqldb

{

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数据库互操作的桥梁。

941847efde445e6765691266f87510a6.png

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.Services;

usingSystem.Data;

usingMySql.Data.MySqlClient;//引入该命名空间是为了操作MySQL数据库

namespacedataformnmysqldb

{

///

///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按钮进行查找)。

221ec4ea031f2603ca690282190e9b50.png

创建Silverlight客户端应用程序

最终效果图

9bb1de1e0647d718da8bf7f03a6e4e58.png

作者:Kinglee

文章出处:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)

版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值