程序结构
移动手机开发企业应用,常常会访问远程数据库(云端数据库),往往通过WCF对外提供接口访问。程序结构一般是:Silverlight+WCF+sql Server数据库
下面就是以操作用户User为例,移动终端通过调用WCF实现对数据库的基本操作(增删改查)。
1.数据库
1)创建数据库表
CREATE TABLE [dbo].[User](
[UserID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nchar](100) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserID] ASC
)WITH (PAD_INDEX = OFF,STATISTICS_norECOmpuTE = OFF,IGnorE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
2.WCF
1)定义接口
[ServiceContract]
public interface IService1
{
[OperationContract]
string RetrieveUser();
[OperationContract]
bool createuser(string userName);
[OperationContract]
bool UpdateUser(int userID,string userName);
[OperationContract]
bool DeleteUser(int userID);
}
2)实现接口
public class Service1 : IService1
{
//查询用户
public string RetrieveUser()
{
try
{
sqlConnection _sqlConnection =
new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user id=sa;");
_sqlConnection.open();
sqlDataAdapter da = new sqlDataAdapter();
da.SelectCommand = new sqlCommand("SELECT * FROM [User]",
_sqlConnection);
DataSet ds = new DataSet();
da.Fill(ds);
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.Append("<Users>");
foreach (DaTarow dr in ds.Tables[0].Rows)
{
sb.Append("<User>");
sb.Append("<UserID>");
sb.Append(dr[0].ToString());
sb.Append("</UserID>");
sb.Append("<UserName>");
sb.Append(dr[1].ToString());
sb.Append("</UserName>");
sb.Append("</User>");
}
sb.Append("</Users>");
_sqlConnection.Close();
return sb.ToString();
}
catch (Exception ex)
{
return string.Empty;
}
}
//创建用户
public bool createuser(string userName)
{
try
{
sqlConnection _sqlConnection =
new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user id=sa;");
_sqlConnection.open();
sqlCommand command = new sqlCommand();
command.Connection = _sqlConnection;
command.CommandType = CommandType.Text;
command.CommandText = "INSERT INTO [User] ([UserName]) VALUES ('" +
userName.ToString().Replace("'","''") + "')";
command.ExecuteNonQuery();
_sqlConnection.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
//更新用户
public bool UpdateUser(int userID,string userName)
{
try
{
sqlConnection _sqlConnection =
new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user id=sa;");
_sqlConnection.open();
sqlCommand command = new sqlCommand();
command.Connection = _sqlConnection;
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE [User] " +
"SET [UserName] = '" +
userName.ToString().Replace("'","''") + "'" +
"WHERE [UserID] = " + userID.ToString();
command.ExecuteNonQuery();
_sqlConnection.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
//删除用户
public bool DeleteUser(int userID)
{
try
{
sqlConnection _sqlConnection =
new sqlConnection("Database=test1;Server=lochost;Integrated Security=false;password=1;user id=sa;");
_sqlConnection.open();
sqlCommand command = new sqlCommand();
command.Connection = _sqlConnection;
command.CommandType = CommandType.Text;
command.CommandText = "DELETE [User] WHERE [UserID] = "
+ userID.ToString();
command.ExecuteNonQuery();
_sqlConnection.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
3)允许跨域访问
建立clientaccesspolicy.xml文件,放于WCF项目根目录
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
3.Silverlight
1)添加Web引用
2)异步调用WCF中对象方法
private ServiceReference1.Service1Client userSvcclient;
private void button1_Click(object sender,RoutedEventArgs e)
{
userSvcclient = new ServiceReference1.Service1Client();
//模拟一个用户
string userName = "zhaoyu";
//注册createuserCompleted事件
userSvcclient.createuserCompleted += new EventHandler<ServiceReference1.createuserCompletedEventArgs>(userSvcclient_createuserCompleted);
//调用createuserAsync()方法创建用户
userSvcclient.createuserAsync(userName);
}
void userSvcclient_createuserCompleted(object sender,ServiceReference1.createuserCompletedEventArgs e)
{
//完成createuserAsync()方法后回调.
if (e.Error == null)
{
errMessage.Content = "创建用户成功!";
}
else
{
errMessage.Content = e.Error.ToString();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。