原文:How to get data from Oracle DB in silverlight via WCF ? 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 ?
http://hi.baidu.com/%C7%A7%C0%EF%BA%AE%C9%AB/blog/item/f18b7cc46cfe29dc38db4945.html
-----------------------------------------------------------------------------------------------------------
第一步:新建Silverlight应用程序;
第二步:在.web类型项目上右键,添加“新建项”。选择Silverlight——启用了Silverlight的WCF服务;
然后就多了这个东西:
(图MB)
第三步:在解决方案上右键,添加“新项目”。选择Window——类库;
此类库为调用查询的数据表的属性集合,因此要写上数据表中需要用到字段以及GET、SET方法。
注意此处要在CS文件中添加using System.Runtime.Serialization引用,在类库的引用中添加System.Runtime.Serialization.dll!
这个类库个人理解为一个数据结构,用来存储返回数据表的数据。
第四步:改写第二步中生成的.svc文件下的.svc.cs文件——在.Web类型项目下的(图MB所示)
将:
public void DoWork()
{
// 在此处添加操作实现
return;
}
改写为:
public ListGetDatabyName(Int32 pInParam)
{
String oracleSql;
Listreturnlist = new List();
//用LIST来获取DATASET
//创建ORACLE连接
String oracleConnString = "Data Source=testDB;User Id=TEST;Password=test;";
OracleConnection cnn = new OracleConnection(oracleConnString);
cnn.Open();
oracleSql = "SELECT * FROM TBL_TEST WHERE MYID=" + pInParam;
OracleCommand cmd = new OracleCommand(oracleSql, cnn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TBL_TEST");
foreach (DataRow dr in ds.Tables["TBL_TEST"].Rows)
{
returnlist.Add(new Class1
{
MYID = Convert.ToInt32(dr["MYID"]),
MYRECORD = dr["MYRECORD"].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 = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public ListGetDatabyName(Int32 pInParam)
{
String oracleSql;
Listreturnlist = new List();
//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 = "Data Source=testDB;User Id=TEST;Password=test;";
OracleConnection cnn = new OracleConnection(oracleConnString);
cnn.Open();
//pass your SQL filter paramenter here
oracleSql = "SELECT * FROM TBL_TEST WHERE MYID=" + pInParam;
//oracleSql = "SELECT * FROM TBL_TEST";
OracleCommand cmd = new OracleCommand(oracleSql, cnn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TBL_TEST");
foreach (DataRow dr in ds.Tables["TBL_TEST"].Rows)
{
returnlist.Add(new Class1
{
MYID = Convert.ToInt32(dr["MYID"]),
MYRECORD = dr["MYRECORD"].ToString()
});
}
return returnlist;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
}
}
-------------------------------------------------------------------------
第五步:快成功了!在C#文件(就是除了.web还有类库以外的那个文件头那里,右键,添加“服务引用”);
按发现;不行对吧?
很好,因为你要先按F5编译一次程序。再来!
行了!(不行的同学留言吧~为你们默哀)
第六步:在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 "Ray"
client.GetDatabyNameAsync(Convert.ToInt32(myText.Text.ToString()));
client.GetDatabyNameCompleted += new EventHandler(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.ObservableCollectiontemp =
new ObservableCollection();
temp = e.Result;
for (int i = 0; i < temp.Count; i++)
{
MessageBox.Show(temp[i].MYID.ToString() + " and " + temp[i].MYRECORD.ToString());
}
}
}
}
最后总观~
-------------------------------------------------------
对了,你还要有一个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事件——如最后所示!
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉 本文系统来源:php中文网