Tải bản đầy đủ (.pdf) (31 trang)

Tài liệu Practical Database Programming With Visual C#.NET- P18 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (771.04 KB, 31 trang )


9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

873
9.12.1 Build Web Service Project
Web S ervice O racle U pdate D elete
In this section, we will modify an existing Web Service project WebServiceSQLUpdateDelete
to make it our new Web Service project WebServiceOracleUpdateDelete, and enable it
to update and delete data in our sample Oracle database.
Open Windows Explorer and create a new folder Chapter 9 under the root directory
if you have not done that. Open Internet Explorer and browse to our desired Web Service
project WebServiceSQLUpdateDelete at the folder DBProjects\Chapter 9 located at the
accompanying ftp site (see Chapter 1 ). Copy and paste this project into our new folder
Chapter 9. Rename it WebServiceOracleUpdateDelete. Perform the following modifi ca-
tions to this project in the Windows Explorer window:
1. Change the main Web Service page from WebServiceSQLUpdateDelete.asmx to
WebServiceOracleUpdateDelete.asmx .
2. Open the App_Code folder and change the name of our base class fi le from SQLBase.cs
to OracleBase.cs .
3. Open the App_Code folder and change the name of our code - behind page from
WebServiceSQLUpdateDelete.cs to WebServiceOracleUpdateDelete.cs .
Now open Visual Studio.NET 2008 and our new Web Service project
WebServiceOracleUpdateDelete to perform the associated modifi cations to the contents
of the fi les we renamed above. First, let ’ s perform modifi cations to our main Web Service
page WebServiceOracleUpdateDelete.asmx . Open this page by double - clicking on it
from the Solution Explorer window and perform the following modifi cations:
• Change
CodeBehind= " ~ /App_Code/WebServiceSQLUpdateDelete.cs "
to
CodeBehind= " ~ /App_Code/WebServiceOracleUpdateDelete.cs " .
• Change


Class= " WebServiceSQLUpdateDelete "
to
Class= " WebServiceOrac
leUpdateDelete " .

Second, open the base class fi le OracleBase.cs and perform the following
modifi cations:
• Change the class name and the constructor ’ s name from SQLBase to OracleBase .
• Change the name of the fi rst member data from SQLOK to OracleOK .
• Change the name of the second member data from SQLError to OracleError .
Go to the File|Save All menu item to save these modifi cations.
9.12.2 Modify Connection String
Double - click on our Web confi guration fi le
web.config
from the Solution Explorer
window to open it. Change the content of the connection string that is under the tag
< connectionStrings > to:
< add name= " ora_conn " connectionString= " Server=XE;
User ID=CSE_DEPT;Password=reback; " / >
c09.indd 873c09.indd 873 2/11/2010 3:02:48 PM2/11/2010 3:02:48 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
874
Chapter 9 ASP.NET Web Services
The Oracle database server XE is used for the server name, the user ID is the name
of our sample database CSE_DEPT , and the password is determined by the user when
adding a new account to create a new user database.
9.12.3 Modify Namespace Directories
First, we need to add an Oracle Data Provider Reference to our Web Service project.
To do that, right - click on our new project icon WebServiceOracleUpdateDelete from the
Solution Explorer window, and then select the item Add Reference from the pop - up

menu to open the Add Reference dialog box. Browse down along the list until you fi nd
the item System.Data.OracleClient , click to select it and then click on the OK button
to add it into our project.
Now double - click on our code - behind page WebServiceOracleUpdateDelete.cs to
open it. On the opened page, add the Oracle namespace to the namespace area on this
page.
using System.Data.OracleClient;
Also change the name of our Web Service class, which is located after the accessing
mode public class , from WebServiceSQLUpdateDelete to WebServiceOracle
UpdateDelete . Perform the same modifi cation to the constructor ’ s name of this class.
Next we will perform the necessary modifi cations to four Web methods developed
in this Web Service project combined with those fi ve differences listed above.
9.12.4 Modify Web Method SQLUpdateSP and Related Methods
The following issues are related to this modifi cation:
1. The name of this Web method and the name of the returned data type class
2. The content of the query string used in this Web method
3. The stored procedure used in this Web method
4. The names of the data components used in this Web method
5. The user - defi ned SQLConn() and ReportError() methods
6. The names of the dynamic parameters
Let ’ s perform those modifi cations step by step according to this sequence. Open the
Web method SQLUpdateSP() and perform the modifi cations shown in Figure 9.138 to
this method.
Let ’ s take a closer look at these modifi cations to see how they work.
A. Rename this Web method OracleUpdateSP and change the name of the returned class
to OracleBase .
B. Modify the content of the query string by changing the name of the stored procedure from
dbo.WebUpdateCourseSP to UpdateCourse_SP . The former is an SQL Server stored
procedure and the latter is an Oracle stored procedure that will be developed in the next
section.

c09.indd 874c09.indd 874 2/11/2010 3:02:48 PM2/11/2010 3:02:48 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

875
C. Change the prefi x from Sql to Oracle for all data classes and from
sql
to
ora
for all data
objects. Also change the returned instance name from SQLResult to OracleResult .
D. Change the name of the returned instance from SQLResult to OracleResult and member
data from SQLOK to OracleOK .
E. Change the name of the subroutine from SQLConn to OracleConn .
F. Change the prefi x from
sql
to
ora
for all data objects.
using System.Data.OracleClient;
[WebService(Namespace = "
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService
Oracle
UpdateDelete : System.Web.Services.WebService
{
public WebService

Oracle
UpdateDelete()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public
Oracle
Base
Oracle
UpdateSP(string FacultyName, string CourseID, string Course, string Schedule,
string Classroom, int Credit, int Enroll)
{
string cmdString = "
UpdateCourse_SP
";

Oracle
Connection
ora
Connection = new
Oracle
Connection();

Oracle
Base
Oracle
Result = new
Oracle

Base();

Oracle
Command
ora
Command = new
Oracle
Command();
int intUpdate = 0;

Oracle
Result.
Oracle
OK = true;

ora
Connection =
Oracle
Conn();
if (
ora
Connection == null)
{

Oracle
Result.
Oracle
Error = "Database connection is failed";
ReportError(
Oracle

Result);
return null;
}

ora
Command.Connection =
ora
Connection;

ora
Command.CommandType = CommandType.StoredProcedure;

ora
Command.CommandText = cmdString;

ora
Command.Parameters.Add("
FacultyName
",
Oracle
Type.
VarChar
).Value = FacultyName;

ora
Command.Parameters.Add("
inCourseID
",
Oracle
Type.Char).Value = CourseID;


ora
Command.Parameters.Add("
inCourse
",
Oracle
Type.
VarChar
).Value = Course;

ora
Command.Parameters.Add("
inSchedule
",
Oracle
Type.Char).Value = Schedule;

ora
Command.Parameters.Add("
inClassroom
",
Oracle
Type.
VarChar
).Value = Classroom;

ora
Command.Parameters.Add("
inCredit
",

Oracle
Type.Int
32
).Value = Credit;

ora
Command.Parameters.Add("
inEnroll
",
Oracle
Type.Int
32
).Value = Enroll;
intUpdate =
ora
Command.ExecuteNonQuery();

ora
Connection.Close();

ora
Command.Dispose();
if (intUpdate == 0)
{

Oracle
Result.
Oracle
Error = "Data updating is failed";
ReportError(

Oracle
Result);
}
return
Oracle
Result;
}
A
B
C
D
E
F
G
H
I
WebServiceOracleUpdateDelete OracleUpdateSP()

Figure 9.138
Modifi ed Web method OracleUpdateSP.
c09.indd 875c09.indd 875 2/11/2010 3:02:48 PM2/11/2010 3:02:48 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
876

Chapter 9 ASP.NET Web Services
G. Modify the nominal names for all seven input parameters to the stored procedure by
removing the
@
symbol before each nominal name. Also change the data type of the top
fi ve input parameters from SqlDbType.Text to OracleType.VarChar . Change the data

type for the last two input parameters from SqlDbType.Int to OracleType.Int32 . The
prefi x in for the last six parameters matchs the input parameters used for the stored pro-
cedure UpdateCourse_SP().
H. Change the prefi x from
sql
to
ora
for all data objects.
I. Change the name of the returned instance from SQLResult to OracleResult and change
the second member data from SQLError to OracleError .
Your modifi ed Web method OracleUpdateSP() is shown in Figure 9.138 . All modifi ed
parts have been highlighted in bold.
Next let ’ s perform modifi cations to two related user - defi ned methods SQLConn()
and ReportError().
Perform the following modifi cations to the SQLConn() method:
A. Change the name of this method from SQLConn to OracleConn and return class name
from SqlConnection to OracleConnection . Also change the connection string from
sql_
conn
to
ora_conn
.
B. Change the data type of the returned connection object to OracleConnection .
Perform the following modifi cations to the ReportError() method:
C. Change the data type of the passed argument from SQLBase to OracleBase .
D. Change the name of the fi rst member data from SQLOK to OracleOK .
E. Change the name of the second member data from SQLError to OracleError .
Your modifi ed methods OracleConn() and ReportError() should match that shown
in Figure 9.139 . All modifi ed parts have been highlighted in bold.
Next let ’ s develop the stored procedure UpdateCourseSP to perform the course

updating function.
protected OracleConnection OracleConn()
{
string cmdString = ConfigurationManager.ConnectionStrings["ora_conn"].ConnectionString;
OracleConnection conn = new OracleConnection();
conn.ConnectionString = cmdString;
conn.Open();
if (conn.State != System.Data.ConnectionState.Open)
{
MessageBox.Show("Database Open is failed");
conn = null;
}
return conn;
}
protected void ReportError(OracleBase ErrSource)
{
ErrSource.OracleOK = false;
MessageBox.Show(ErrSource.OracleError);
}
A
B
C
D
E
WebServiceOracleUpdateDelete
OracleConn()

Figure 9.139
Modifi ed methods OracleConn and ReportError.
c09.indd 876c09.indd 876 2/11/2010 3:02:49 PM2/11/2010 3:02:49 PM

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

877
9.12.4.1 Develop Stored Procedure Update C ourse_ SP
A very detailed discussion of creating and manipulating packages and stored procedures
in Oracle database is provided in Section 5.20.3.5 in Chapter 5 . Refer to that section to
get more detailed information for creating Oracle ’ s stored procedures.
The topic we discuss in this section is to update and delete data in the database.
Therefore no returned data is needed for these two data actions. We only need to create
stored procedures in Oracle database, not packages, to perform the data updating and
deleting function.
As discussed in Section 5.20.3.6 in Chapter 5 , different methods can be used to create
Oracle ’ s stored procedures. In this section, we will use the Object Browser page provided
by Oracle Database 10g XE to create our stored procedures.
Open the Oracle Database 10g XE home page by going to Start|All Programs|Oracle
Database 10g Express Edition|Go To Database Home Page items. Finish the login
process by entering the correct username and password such as CSE_DEPT and reback .
Click on the Object Browser and select Create|Procedures item to open the Create
Procedure window. Click on the Create button and select the Procedure icon from the
list to open this window.
Enter UpdateCourse_SP into the Procedure Name box and keep the Include
Argument checkbox checked, and click on the Next button to go to the next page.
The next window is used to allow us to enter all input parameters. For this
stored procedure we need to perform two queries. Therefore we have seven input
parameters. The fi rst query is to get the
faculty_id
from the Faculty table based
on the faculty name that is an input and selected by the user. The second query

is to update a course record that contains six pieces of information related to a
current course in the Course table based on the
faculty_id
that is obtained from
the fi rst query. The seven input parameters are FacultyName, CourseID, CourseTitle,
Credit, Classroom, Schedule, and Enrollment. The fi rst input parameter FacultyName
is used by the fi rst query, and the following six input parameters are used by the
second query.
Enter those input parameters one by one into the argument box. The point is
that the data type of each input parameter must be identical with the data type of
each data column used in the Course table. Refer to Section 2.11.2.3 in Chapter 2 to
get a detailed list of data types used for those data columns in the Course data table.
For the Input/Output parameter defi nitions, select IN for all seven parameters since
no output is needed for this data updating query. Your fi nished argument list should
match that shown in Figure 9.140 .
Click on the Next button to go to the procedure defi ning page. Enter the codes shown
in Figure 9.141 into this new procedure as the body of the procedure using the language
of so - called Procedural Language Extension for SQL or PL - SQL. Then click on the Next
and on the Finish buttons to confi rm creating this procedure. Your fi nished stored pro-
cedure should match that shown in Figure 9.142 .
Seven input parameters are listed at the beginning of this procedure with the keyword
IN to indicate that these parameters are inputs to the procedure. The intermediate
parameter
faculty_id
is obtained from the fi rst query from the Faculty table. The data
type of each parameter is indicated after the keyword IN , and it must be identical with
the data type of the associated data column in the Course table. An IS command is
c09.indd 877c09.indd 877 2/11/2010 3:02:49 PM2/11/2010 3:02:49 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
878


Chapter 9 ASP.NET Web Services
attached after the procedure header to indicate that an intermediate query result,

faculty_id
, will be held by a local variable
facultyID
declared later.
Two queries are included in this procedure. The fi rst query is used to get the

faculty_id
from the Faculty table based on the input parameter FacultyName , and
the second query is to update a course record based on six input parameters in the Course
table. A semicolon must be attached after each PL - SQL statement.
One important issue is that you need to create one local variable
FacultyID
and
attach it after the IS command as shown in line 9 in Figure 9.142 , and this coding line has
been highlighted with shading. Click on the Edit button to add this local variable with its
data type of VARCHAR2(10) . This local variable is used to hold the returned
faculty_
id
from the execution of the fi rst query.
Another important issue in arranging the input parameters or arguments in the
UPDATE command is that the order of those parameters or arguments must be identical
with the order of the columns in the associated data table. For example, in the
Course table, the order of the data columns is course_id, course, credit, classroom,
schedule, enrollment , and faculty_id . Accordingly, the order of input parameters

Figure 9.140

Finished argument list.
SELECT faculty_id INTO FacultyID FROM Faculty
WHERE faculty_name = FacultyName;
UPDATE Course SET course=inCourse, credit=inCredit, classroom=inClassroom,
schedule=inSchedule, enrollment=inEnroll, faculty_id=FacultyID
WHERE course_id=inCourseID;

Figure 9.141
Stored procedure body.
c09.indd 878c09.indd 878 2/11/2010 3:02:49 PM2/11/2010 3:02:49 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

879

Figure 9.142
Completed stored procedure.
placed in the UPDATE argument list must be identical with the data columns ’ order
displayed above.
To make sure that this procedure works properly, we need to compile it fi rst. Click
on the Compile button to compile and check our procedure. A successful compilation
message should be displayed if our procedure is a bug - free stored procedure. Close the
Oracle Database 10g Express Edition by clicking the Close button.
9.12.5 Modify Web Method Get SQLC ourse
and Related Methods
The function of this Web method is to retrieve all
course_id
, which includes the
original and the new inserted

course_id
, from the Course table based on the input
faculty name. This Web method will be called or consumed by a client project later to
get back and display all
course_id
in a listbox control in the client project.
Recall that in Section 5.19.2.5 in Chapter 5 , we developed a joined - table query to
perform the data query from the Course table to get all
course_id
based on the faculty
name. The reason for that is because there is no faculty name column available in the
Course table, and each course or
course_id
is related to a
faculty_id
in the Course
table. In order to get the
faculty_id
that is associated with the selected faculty name,
one must fi rst go to the Faculty table to perform a query to obtain it. In this situation, a
join query is a desired method to complete this function.
Open this Web method and perform the following modifi cations that are shown in
Figure 9.143 to this method. All modifi ed parts have been highlighted in bold.
c09.indd 879c09.indd 879 2/11/2010 3:02:49 PM2/11/2010 3:02:49 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
880

Chapter 9 ASP.NET Web Services
Let ’ s take a closer look at these modifi cations to see how they work.
A. Change the name of this Web method from GetSQLCourse to GetOracleCourse . Also

change the name of the returned instance from SQLBase to OracleBase .
B. Modify the query string to match it to the ANSI 89 standard. Recall that we developed a
join - table query string for SQL Server database using the ANSI 92 standard in Section
5.19.2.5 in Chapter 5 . Since the ANSI 89 standard is still being used in the Oracle database,
we need to modify this joined - table query string by using that standard.
C. Change the prefi x of all data classes from Sql to Oracle and from
sql
to
ora
for all data
objects used in this method. Also change the name of the returned instance from SQLResult
to OracleResult . Change the fi rst member data from SQLOK to OracleOK .
D. Change the name of the user - defi ned method from SQLConn to OracleConn . Change the
second member data from SQLError to OracleError .
E. Change the prefi x of all data objects from
sql
to
ora
. Change the second member data
from SQLError to OracleError .
F. Modify the nominal name for the input parameter to the stored procedure by removing
the
@
symbol before the nominal name
fname
. Also change the data type of this input
parameter from SqlDbType.Text to OracleType.VarChar .
[WebMethod]
public
Oracle

Base Get
Oracle
Course(string FacultyName)
{
string cmdString = "SELECT Course.course_id FROM Course, Faculty " +
"WHERE (Course.faculty_id = Faculty.faculty_id) AND (Faculty.faculty_name =: fname)";

Oracle
Connection
ora
Connection = new
Oracle
Connection();

Oracle
Base
Oracle
Result = new
Oracle
Base();

Oracle
Command
ora
Command = new
Oracle
Command();

Oracle
DataReader

ora
Reader;

Oracle
Result.
Oracle
OK = true;

ora
Connection =
Oracle
Conn();
if (
ora
Connection == null)
{

Oracle
Result.
Oracle
Error = "Database connection is failed";
ReportError(
Oracle
Result);
return null;
}

ora
Command.Connection =
ora

Connection;

ora
Command.CommandType = CommandType.Text;

ora
Command.CommandText = cmdString;

ora
Command.Parameters.Add("
fname
",
Oracle
Type.
VarChar
).Value = FacultyName;

ora
Reader =
ora
Command.ExecuteReader();
if (
ora
Reader.HasRows == true)
FillCourseReader(ref
Oracle
Result,
ora
Reader);
else

{

Oracle
Result.
Oracle
Error = "No matched course found";
ReportError(
Oracle
Result);
}

ora
Reader.Close();

ora
Connection.Close();

ora
Command.Dispose();
return
Oracle
Result;
}
A
B
C
D
E
F
G

H
WebServiceOracleUpdateDelete
GetOracleCourse()

Figure 9.143
Modifi ed Web method GetOracleCourse.
c09.indd 880c09.indd 880 2/11/2010 3:02:50 PM2/11/2010 3:02:50 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

881
G. Change the names of two passed arguments to the method FillCourseReader() from
SQLResult to OracleResult and from sqlReader to oraReader .
H. Change the name of the returned instance from SQLResult to OracleResult , change the
second member data from SQLError to OracleError , and change the prefi x for all data
objects from
sql
to
ora
.
The modifi cations to the related user - defi ned FillCourseReader() method are rela-
tively simple. Perform the following modifi cations to this method:
A. Modify the data types of two passed arguments by changing the data type of the fi rst argu-
ment from SQLBase to OracleBase and changing the data type of the second argument
from SqlDataReader to OracleDataReader .
B. Change the method from GetSQLString(0) to GetOracleString(0) .
Your modifi ed user - defi ned FillCourseReader() method should match that shown in
Figure 9.144 . All modifi ed parts have been highlighted in bold. Next let ’ s modify another
Web method GetSQLCourseDetail().

9.12.6 Modify Web Method Get SQLC ourse D etail and
Related Methods
The function of this Web method is to retrieve the detailed information for a selected

course_id
that works as an input parameter to this method, and store the retrieved
information to an instance that will be returned to the calling procedure.
The following three modifi cations need to be performed for this Web method:
1. Modify the codes of this Web method.
2. Modify the related user - defi ned FillCourseDetail() method.
3. Modify the content of the query string and make it equal to the name of an Oracle Package
WebSelectCourseSP we developed in Section 9.10.7 .
Open this Web method and perform the modifi cations shown in Figure 9.145 to this
Web method.
Let ’ s take a closer look at these modifi cations to see how they work.
A. Change the name of this Web method from GetSQLCourseDetail to
GetOracleCourseDetail . Also change the name of the returned base class from SQLBase
to OracleBase .
protected void FillCourseReader(ref
Oracle
Base sResult,
Oracle
DataReader sReader)
{
int pos = 0;
while (sReader.Read())
{
sResult.CourseID[pos] = Convert.ToString(sReader.
GetOracleString(0)
); //the 1st column is course_id

pos++;
}
}
A
B
WebServiceOracleUpdateDelete FillCourseReader()

Figure 9.144
Modifi ed method FillCourseReader.
c09.indd 881c09.indd 881 2/11/2010 3:02:50 PM2/11/2010 3:02:50 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
882

Chapter 9 ASP.NET Web Services
B. Modify the content of the query string and make it equal to the name of the Package we
developed in the Section 9.10.7 . Change this query string from dbo.WebSelectCourseSP
to WebSelectCourseSP.SelectCourse . The prefi x WebSelectCourseSP is a Package
and the SelectCourse is a stored procedure.
C. Change the prefi x of all data classes from Sql to Oracle and from
sql
to
ora
for all data
objects used in this method. Also change the name of the returned instance from SQLResult
to OracleResult . Change the fi rst member data from SQLOK to OracleOK .
D. Add two OracleParameter objects paramCourseID and paramCourseInfo . Because some
differences exist between the SQL Server and Oracle databases, we need to use different
ways to assign parameters to the Parameters collection of the Command object later.
E. Change the name of the user - defi ned method from SQLConn to OracleConn . Change the
second member data from SQLError to OracleError .

[WebMethod]
public
Oracle
Base Get
Oracle
CourseDetail(string CourseID)
{
string cmdString = "
WebSelectCourseSP.SelectCourse
";

Oracle
Connection
ora
Connection = new
Oracle
Connection();

Oracle
Base
Oracle
Result = new
Oracle
Base();

Oracle
DataReader
ora
Reader;


OracleParameter paramCourseID = new OracleParameter();
OracleParameter paramCourseInfo = new OracleParameter();

Oracle
Result.
Oracle
OK = true;

ora
Connection =
Oracle
Conn();
if (
ora
Connection == null)
{

Oracle
Result.
Oracle
Error = "Database connection is failed";
ReportError(
Oracle
Result);
return null;
}

paramCourseID.ParameterName = "CourseID";
paramCourseID.OracleType = OracleType.VarChar;
paramCourseID.Value = CourseID;

paramCourseInfo.ParameterName = "CourseInfo";
paramCourseInfo.OracleType = OracleType.Cursor;
paramCourseInfo.Direction = ParameterDirection.Output;
//this is very important

Oracle
Command
ora
Command = new
Oracle
Command(cmdString,
ora
Connection);

ora
Command.CommandType = CommandType.StoredProcedure;

oraCommand.Parameters.Add(paramCourseID);
oraCommand.Parameters.Add(paramCourseInfo);

ora
Reader =
ora
Command.ExecuteReader();
if (
ora
Reader.HasRows == true)
FillCourseDetail(ref
Oracle
Result,

ora
Reader);
else
{

Oracle
Result.
Oracle
Error = "No matched course found";
ReportError(
Oracle
Result);
}

ora
Reader.Close();

ora
Connection.Close();

ora
Command.Dispose();
return
Oracle
Result;
}
A
B
C
D

E
F
G
H
I
WebServiceOracleUpdateDelete GetOracleCourseDetail()

Figure 9.145
Modifi ed Web method GetOracleCourseDetail.
c09.indd 882c09.indd 882 2/11/2010 3:02:50 PM2/11/2010 3:02:50 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

9.12 Build ASP.NET Web Service to Update and Delete Data for Oracle Database

883
F. Initialize two OracleParameter objects by assigning them with the appropriate values. The
point is, for the second parameter paramCourseInfo , the data type of this parameter is

Cursor
and the Direction is Output. Both values are very important to this parameter
and must be assigned exactly as we did here. Otherwise a running exception will be encoun-
tered when the project runs.
G. Add two statements to add two OracleParameter objects to the Command object.
H. Change the names of two passed arguments to the FillCourseDetail() method from
SQLResult to OracleResult and from sqlReader to oraReader .
I. Change the name of the returned instance from SQLResult to OracleResult and change
the second member data from SQLError to OracleError .
The modifi cations to the related user - defi ned FillCourseDetail() method are simple.
The only modifi cations are to change the data type of two passed arguments sResult
and sReader. Change the data type of the fi rst argument from SQLBase to OracleBase ,

and change the data type for the second argument from SqlDataReader to
OracleDataReader .
9.12.7 Modify Web Method SQLD elete SP
As we discussed in Section 7.1.1 in Chapter 7 , to delete a record from a relational data-
base, one needs to follow the operation steps listed below:
1. Delete records that are related to the parent table using the foreign keys from child tables.
2. Delete records that are defi ned as primary keys from the parent table.
In other words, to delete one record from the parent table, all records that are related
to that record as foreign keys and located at different child tables must be deleted fi rst.
In our case, in order to delete a record using the
course_id
as the primary key from
the Course table (parent table), one must fi rst delete those records using the
course_id

as a foreign key from the StudentCourse table (child table). Fortunately we have only
one child table related to our parent table in our sample database. Refer to Section 2.5
and Figure 2.5 in Chapter 2 to get a clear relationship description among different data
tables in our sample database.
From this discussion, it can be found that to delete a course record from our sample
database two deleting queries should be performed: The fi rst query is used to delete the
related records from the child table or the StudentCourse table, and the second query is
used to delete the target record from the parent table or the Course table. Of course, we
can place these two queries into a stored procedure WebDeleteCourseSP(), which we
will develop in the following section to perform this deleting action. However, recall that
in Section 2.11.3.5 in Chapter 2 , we set a one - to - many constraint relationship between
the Course and the StudentCourse tables with an On Delete Cascade mode (refer to
Figure 2.65 ). This mode means that all records with a
course_id
that is equal to a


course_id
in the Course (parent) table in the StudentCourse (child) table will be
deleted if that
course_id
is deleted from the Course (parent) table. With this On
Delete Cascade mode, we can use a single statement in this stored procedure to only
delete a
course_id
from the Course (parent) table, and all related records in the
StudentCourse (child) table will also be deleted automatically by the database engine.
c09.indd 883c09.indd 883 2/11/2010 3:02:50 PM2/11/2010 3:02:50 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
884

Chapter 9 ASP.NET Web Services
A single input parameter
course_id
is passed into this stored procedure as the
primary key.
Now open this Web method and perform the modifi cations shown in Figure 9.146 to
this Web method. All modifi ed parts have been highlighted in bold.
Let ’ s take a closer look at this piece of modifi ed code to see how it works.
A. Change the name of this Web method from SQLDeleteSP to OracleDeleteSP and change
the returned data type from SQLBase to OracleBase .
B. The content of the query string is equal to the name of the stored procedure
WebDeleteCourseSP we will develop in the next section. Change the name of the stored
procedure from dbo.WebDeleteCourseSP to WebDeleteCourseSP .
C. Change the prefi x from Sql to Oracle for all data classes and from
sql

to
ora
for all data
objects used in this method. Also change the name of the returned instance from SQLResult
to OracleResult . Change the fi rst member data from SQLOK to OracleOK .
D. Change the name of the user - defi ned method from SQLConn to OracleConn . Change the
second member data from SQLError to OracleError .
E. Modify the nominal name for the input parameter to the stored procedure by removing
the
@
symbol before the nominal name
CourseID
. Also change the data type of this input
parameter from SqlDbType.Text to OracleType.VarChar .
F. Change the name of the returned instance from SQLResult to OracleResult . Also change
the second member data from SQLError to OracleError and the prefi x from
sql
to
ora

for all data objects.
[WebMethod]
public
Oracle
Base
Oracle
DeleteSP(string CourseID)
{
string cmdString = "
WebDeleteCourseSP

";

Oracle
Connection
ora
Connection = new
Oracle
Connection();

Oracle
Base
Oracle
Result = new
Oracle
Base();
int intDelete = 0;

Oracle
Result.OracleOK = true;

ora
Connection =
Oracle
Conn();
if (
ora
Connection == null)
{

Oracle

Result.
Oracle
Error = "Database connection is failed";
ReportError(
Oracle
Result);
return null;
}

Oracle
Command
ora
Command = new
Oracle
Command(cmdString,
ora
Connection);

ora
Command.CommandType = CommandType.StoredProcedure;

ora
Command.Parameters.Add("
CourseID
",
Oracle
Type.
VarChar
).Value = CourseID;
intDelete =

ora
Command.ExecuteNonQuery();
if (intDelete == 0)
{

Oracle
Result.
Oracle
Error = "Data deleting is failed";
ReportError(
Oracle
Result);
}

ora
Connection.Close();
oraCommand.Dispose();
return
Oracle
Result;
}
A
B
C
D
E
F
WebServiceOracleUpdateDelete OracleDeleteSP()

Figure 9.146

Modifi ed Web method OracleDeleteSP.
c09.indd 884c09.indd 884 2/11/2010 3:02:50 PM2/11/2010 3:02:50 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×