Connect to the ORACLEDB database through WCF in silverlight

Source: Internet
Author: User
Tags oracleconnection
Original article: HowtogetdatafromOracleDBinsilverlightviaWCF? Zookeeper --------------------------------------------------------------------------------------------

Original article: 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 lightning --------------------------------------------------------------------------------------------

Original article: 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

Bytes -----------------------------------------------------------------------------------------------------------

Step 1: Create a Silverlight application;

Step 2: Right-click a. web project and add "new project ". Select Silverlight -- enable the Silverlight WCF Service;

Then, this thing is added:

(Figure MB)

Step 3: Right-click the solution and add "new project ". Select Window -- class library;

This type of database is a SET of properties of the data table called for query. Therefore, you need to write fields and the GET and SET methods in the data table.

Note that you must add using System. Runtime. Serialization reference to the CS file. Add System. Runtime. Serialization. dll to the reference of the class library!

This class library is a data structure used to store the data returned from the data table.

Step 4: rewrite the. svc. cs file under the. svc file generated in step 2-under the. Web project (Figure MB)

Set:

Public void DoWork ()

{

// Add operation implementation here

Return;

}

Rewrite:

Public List GetDatabyName (Int32 pInParam)

{

String oracleSql;

List Returnlist = new List ();

// Use LIST to obtain DATASET

// Create an ORACLE connection

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 ()

});

}

// Return DATASET as a LIST

Return returnlist;

}

Note: To Add a reference:

Using System. Collections. Generic; -- used to describe the List

Using ClassLibrary1;

Using System. Data. OracleClient;

Using System. Data; -- used to describe DATASET

Add System. Data. OracleClient. dll to references under. web -- to explain ORACLE statements; and add your class library! Add -- to the referenced project to explain the Data Type in List <>!

---------------------------------- Final code ---------------------------------------

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;

}

// Add more operations here and mark them with [OperationContract]

}

}

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

Step 5: It's almost done! In the C # file (that is, the file header other than. web and class library, right-click and add "service reference ");

By discovery; no, right?

Very good, because you need to press F5 firstCompile a program. Come back!

All right! (Leave a message if you cannot ~ Sorry for you)

Step 6: Make a little modification in MainPage. xaml. First, add a BUTTON named myButton and a TextBox named myText.

Add reference using System. Collections. ObjectModel; using SilverlightApplication7.ServiceReference1 in MainPage. xaml. cs;

The complete MainPage. xaml. cs code is as follows:

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 <temp. Count; I ++)

{

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

}

}

}

}

Overview ~

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

By the way, you also need an ORACLE database. In this example, the database name is testDB, the username is TEST, and the password is test. Therefore, the corresponding statement is Data Source = testDB; user Id = TEST; Password = test;

Then generate a table -- in oracle SQL PLUS or something, solve it by yourself:

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 ');

Table Name: TBL_TEST

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

The last reminder is to add the Click event in the Butto statement in MainPage. xaml-as shown at the end!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.