设为首页 收藏本站
查看: 622|回复: 0

[经验分享] Silverlight学习笔记[4]

[复制链接]

尚未签到

发表于 2015-11-7 12:48:12 | 显示全部楼层 |阅读模式



http://hi.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/blog/item/f18b7cc46cfe29dc38db4945.html
这不是我的原创,我也是上网学习的~
How to get data from Oracle DB in silverlight
via WCF ?
查看原文可以搜索以上文章名。
这个只是给入门和遇到问题的朋友——毕竟真的有很多小细节没有说明!
而且英文的确看着不舒服。中国进步,还要靠大家的付出啊!!!
-----------------------------------------------------------------------------------------------------------
第一步:新建Silverlight应用程序;
第二步:在.web类型项目上右键,添加“新建项”。选择Silverlight——启用了Silverlight的WCF服务;
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/c3d859dca965e2facc1166c2.jpg
然后就多了这个东西:
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/dcefb8d8de48e96e32fa1cd3.jpg(图MB)
第三步:在解决方案上右键,添加“新项目”。选择Window——类库;
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/85d669957128f510d0135ec8.jpg
              此类库为调用查询的数据表的属性集合,因此要写上数据表中需要用到字段以及GET、SET方法。
              注意此处要在CS文件中添加using System.Runtime.Serialization引用,在类库的引用中添加System.Runtime.Serialization.dll!
              这个类库个人理解为一个数据结构,用来存储返回数据表的数据。
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/17d30daf02adfb977dd92ad5.jpg
第四步:改写第二步中生成的.svc文件下的.svc.cs文件——在.Web类型项目下的(图MB所示)
              将:
        public void DoWork()
{
// 在此处添加操作实现
return;
}
改写为:
public List<Class1> GetDatabyName(Int32 pInParam)
{
String oracleSql;
List<Class1> returnlist = new List<Class1>();
//用LIST来获取DATASET
//创建ORACLE连接
String oracleConnString = &quot;Data Source=testDB;User Id=TEST;Password=test;&quot;;
OracleConnection cnn = new OracleConnection(oracleConnString);
cnn.Open();
oracleSql = &quot;SELECT * FROM TBL_TEST WHERE MYID=&quot; + pInParam;
OracleCommand cmd = new OracleCommand(oracleSql, cnn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, &quot;TBL_TEST&quot;);
foreach (DataRow dr in ds.Tables[&quot;TBL_TEST&quot;].Rows)
{
returnlist.Add(new Class1
{
MYID = Convert.ToInt32(dr[&quot;MYID&quot;]),
MYRECORD = dr[&quot;MYRECORD&quot;].ToString()
});
}
//以LIST形式返回DATASET
return returnlist;
}
注意了,要添加引用:
using System.Collections.Generic;//用来说明List
using ClassLibrary1;
using System.Data.OracleClient;
using System.Data;//用来说明DATASET
在.web下的引用添加System.Data.OracleClient.dll——用来解释ORACLE语句;还有你的类库也要添加!在引用中的项目里添加——用来解释List<>中的数据类型!
----------------------------------最终代码---------------------------------------
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using ClassLibrary1;
using System.Data.OracleClient;
using System.Data;
namespace SilverlightApplication7.Web
{
[ServiceContract(Namespace = &quot;&quot;)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public List<Class1> GetDatabyName(Int32 pInParam)
{
String oracleSql;
List<Class1> returnlist = new List<Class1>();
//Get your Customer Data from Oracle DB, if you use DataSet, get the DataSet,
//Create Customer Object for each row in your DataTable
String oracleConnString = &quot;Data Source=testDB;User Id=TEST;Password=test;&quot;;
OracleConnection cnn = new OracleConnection(oracleConnString);
cnn.Open();
//pass your SQL filter paramenter here
oracleSql = &quot;SELECT * FROM TBL_TEST WHERE MYID=&quot; + pInParam;
//oracleSql = &quot;SELECT * FROM TBL_TEST&quot;;
OracleCommand cmd = new OracleCommand(oracleSql, cnn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, &quot;TBL_TEST&quot;);
foreach (DataRow dr in ds.Tables[&quot;TBL_TEST&quot;].Rows)
{
returnlist.Add(new Class1
{
MYID = Convert.ToInt32(dr[&quot;MYID&quot;]),
MYRECORD = dr[&quot;MYRECORD&quot;].ToString()
});
}
return returnlist;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
}
}

第五步:快成功了!在C#文件(就是除了.web还有类库以外的那个文件头那里,右键,添加“服务引用”);
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/f18b7cc4dc53d99038db49ae.jpg
按发现;不行对吧?
很好,因为你要先按F5编译一次程序。再来!
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/905acd81e09f4183bd3e1eb2.jpg
行了!(不行的同学留言吧~为你们默哀)
第六步:在MainPage.xaml中做一点点修改,首先加一个BUTTON,名为myButton,再加一个TextBox,名为myText。
在MainPage.xaml.cs添加引用using System.Collections.ObjectModel;using SilverlightApplication7.ServiceReference1;


最后MainPage.xaml.cs完整代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SilverlightApplication7.ServiceReference1;
namespace SilverlightApplication7
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
SilverlightApplication7.ServiceReference1.Service1Client client = new SilverlightApplication7.ServiceReference1.Service1Client();
//Pass your parameter , pass id 4 will return string &quot;Ray&quot;
client.GetDatabyNameAsync(Convert.ToInt32(myText.Text.ToString()));
client.GetDatabyNameCompleted += new EventHandler<GetDatabyNameCompletedEventArgs>(client_GetDatabyNameCompleted);
//Close the connection, when you get the error connection timeout , acctually, the connections limited to 10 for each client.
client.CloseAsync();
}
private void client_GetDatabyNameCompleted(object sender, GetDatabyNameCompletedEventArgs e)
{
//We need a collection object to receive the return list form WCF
//We can only use the class defined in Web Service to create client instance
System.Collections.ObjectModel.ObservableCollection<ServiceReference1.Class1> temp =
new ObservableCollection<ServiceReference1.Class1>();
temp = e.Result;
for (int i = 0; i < temp.Count; i++)
{
MessageBox.Show(temp.MYID.ToString() + &quot; and &quot; + temp.MYRECORD.ToString());
}
}
}
}
最后总观~
http://hiphotos.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/pic/item/fafb09d39545196f960a16ff.jpg
-------------------------------------------------------
对了,你还要有一个ORACLE数据库,我这个例子是:数据库名:testDB;用户名:TEST;密码:test;所以对应语句为Data Source=testDB;User Id=TEST;Password=test;
然后生成一个表——在ORACLE的SQL PLUS中或者什么的,自己解决:
CREATE TABLE TBL_TEST
(
MYID NUMBER(20),
MYRECORD VARCHAR2(50 BYTE)
)
Insert into TBL_TEST(MYID, MYRECORD)Values(1, 'Hello');
Insert into TBL_TEST(MYID, MYRECORD)Values(2, 'I');
Insert into TBL_TEST(MYID, MYRECORD)Values(3, 'am');
Insert into TBL_TEST(MYID, MYRECORD)Values(4, 'Ray');
表名为TBL_TEST
--------------------------------------------
最后最后再提醒,记得在MainPage.xaml中的Butto语句中添加Click事件——如最后所示!
<Button Content=&quot;Button&quot; Height=&quot;23&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;205,216,0,0&quot; Name=&quot;myButton&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;75&quot; Click=&quot;myButton_Click&quot; />
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-136213-1-1.html 上篇帖子: Oracle DB 性能优化:概览 下篇帖子: 【翻译自mos文章】Oracle Database In-Memory Advisor
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表