Home Database Mysql Tutorial 在silverlight中通过WCF连接ORACLE DB数据库

在silverlight中通过WCF连接ORACLE DB数据库

Jun 07, 2016 pm 03:44 PM
oracle silverlight number connect pass

原文: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服务;

在silverlight中通过WCF连接ORACLE DB数据库

然后就多了这个东西:

在silverlight中通过WCF连接ORACLE DB数据库(图MB)

第三步:在解决方案上右键,添加“新项目”。选择Window——类库;

在silverlight中通过WCF连接ORACLE DB数据库

              此类库为调用查询的数据表的属性集合,因此要写上数据表中需要用到字段以及GET、SET方法。

              注意此处要在CS文件中添加using System.Runtime.Serialization引用,在类库的引用中添加System.Runtime.Serialization.dll!

              这个类库个人理解为一个数据结构,用来存储返回数据表的数据。

在silverlight中通过WCF连接ORACLE DB数据库

第四步:改写第二步中生成的.svc文件下的.svc.cs文件——在.Web类型项目下的(图MB所示)

              将:

        public void DoWork()

        {

            // 在此处添加操作实现

            return;

        }

               改写为:

        public List GetDatabyName(Int32 pInParam)

        {

            String oracleSql;

            List returnlist = 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 List GetDatabyName(Int32 pInParam)

        {

            String oracleSql;

            List returnlist = 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还有类库以外的那个文件头那里,右键,添加“服务引用”);

在silverlight中通过WCF连接ORACLE DB数据库

按发现;不行对吧?

很好,因为你要先按F5编译一次程序。再来!

在silverlight中通过WCF连接ORACLE DB数据库

行了!(不行的同学留言吧~为你们默哀)

第六步:在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.ObservableCollection temp =

                new ObservableCollection();

            temp = e.Result;

            for (int i = 0; i

            {

                MessageBox.Show(temp[i].MYID.ToString() + " and " + temp[i].MYRECORD.ToString());

            }

        }

    }

}

最后总观~

在silverlight中通过WCF连接ORACLE DB数据库

-------------------------------------------------------

对了,你还要有一个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事件——如最后所示!

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

See all articles