使用WCFService帮助将项目转换为N层 [英] Help Convert Project to N-tier with WCFService
问题描述
嗨
不久前,我请求一个问题的帮助,此问题涉及在将条目插入表中并从sql server获取自动生成的编号以在表单上查看后,如何生成发票编号.我设法创建了如下所示的解决方案:
SQL Server存储过程:
Hi
A shortwhile ago I requested assistance with a problem I was having concerning the generation of an invoice number after inserting an entry into a table, and getting the autogenerated number from sql server to view on the form. I have managed to create the solution as given below:
SQL-Server Stored Procedure:
CREATE PROCEDURE SpMainTestReceiptTbl
@ReceiptNumber int,
@OwnerID varchar(16),
@ReceiptDate datetime,
@NewReceiptNumber int Output
AS
Declare @count as int
SELECT @count = COUNT(*) FROM Receipts WHERE ReceiptNumber = @ReceiptNumber
IF @count >0
UPDATE Receipts
SET OwnerID = @OwnerID, ReceiptDate = @ReceiptDate
WHERE ReceiptDate = @ReceiptDate
ELSE
INSERT INTO Receipts (OwnerID,ReceiptDate)
VALUES (@OwnerID,@ReceiptDate)
SET @NewReceiptNumber = SCOPE_IDENTITY()
GO
</pre>
My table has three columns one for the ReceiptNumber which is int IDENTITY(100,1)
The Script for creating the table is given below:
<pre>USE [ReceiptsDatabase]
GO
/****** Object: Table [dbo].[Receipts] Script Date: 06/11/2011 15:45:51 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Receipts]') AND type in (N'U'))
DROP TABLE [dbo].[Receipts]
GO
USE [ReceiptsDatabase]
GO
/****** Object: Table [dbo].[Receipts] Script Date: 06/11/2011 15:45:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Receipts](
[ReceiptNumber] [int] IDENTITY(100,1) NOT NULL,
[OwnerID] [varchar](16) NOT NULL,
[ReceiptDate] [date] NOT NULL,
CONSTRAINT [PK_Receipts] PRIMARY KEY CLUSTERED
(
[ReceiptNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO/</pre>
Having created the above two that is into a database named ReceiptsDatabase using the Script given below:
<pre><pre lang="sql">USE [master]
GO
/****** Object: Database [ReceiptsDatabase] Script Date: 06/11/2011 15:57:26 ******/
CREATE DATABASE [ReceiptsDatabase] ON PRIMARY
( NAME = N'ReceiptsDatabase', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.ServerInstance\MSSQL\DATA\ReceiptsDatabase.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ReceiptsDatabase_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.ServerInstance\MSSQL\DATA\ReceiptsDatabase_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [ReceiptsDatabase] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ReceiptsDatabase].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [ReceiptsDatabase] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET ANSI_NULLS OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET ANSI_PADDING OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET ARITHABORT OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [ReceiptsDatabase] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [ReceiptsDatabase] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [ReceiptsDatabase] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET DISABLE_BROKER
GO
ALTER DATABASE [ReceiptsDatabase] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [ReceiptsDatabase] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [ReceiptsDatabase] SET READ_WRITE
GO
ALTER DATABASE [ReceiptsDatabase] SET RECOVERY FULL
GO
ALTER DATABASE [ReceiptsDatabase] SET MULTI_USER
GO
ALTER DATABASE [ReceiptsDatabase] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [ReceiptsDatabase] SET DB_CHAINING OFF
GO
然后,我在VS 2010中使用以下控件创建了Visual Basic窗体:
文本框名为:txtOwnerID
文本框名为:txtReceiptDate
文本框名为:txtReceiptNumber
单选按钮名为:rbNew
单选按钮名为:rbEdit
CommandButton名为:btnSubmit
我对以下形式的btnSubmit_Click事件使用了以下代码:
I then created a Visual Basic Form in VS 2010 with the following controls:
Textbox named: txtOwnerID
Textbox named: txtReceiptDate
Textbox named: txtReceiptNumber
RadioButton named: rbNew
RadioButton named: rbEdit
CommandButton named: btnSubmit
I used the following code for the btnSubmit_Click Event of the form
Dim Saved = False
'Either update or Create a new record depending on Radio Button Edit / New
Dim sqlConn As New SqlConnection
sqlConn.ConnectionString = "Data Source=Locahost;Initial Catalog=ReceiptsDatabase;Integrated Security=True"
Dim sqlcmd As New SqlCommand
sqlcmd.Connection = sqlConn
With sqlcmd
.CommandText = "SpMainTestReceiptTbl"
.CommandType = CommandType.StoredProcedure
End With
Try
'create parameter
With sqlcmd
'---Input Parameter Direction ---
.Parameters.Add("@ReceiptNumber", SqlDbType.Int, 4).Direction = ParameterDirection.Input
.Parameters.Add("@OwnerID", SqlDbType.VarChar, 16).Direction = ParameterDirection.Input
.Parameters.Add("@ReceiptDate", SqlDbType.Date, 9).Direction = ParameterDirection.Input
.Parameters.Add("@NewReceiptNumber", SqlDbType.Int, 4).Direction = ParameterDirection.Output
'--- parameter value ---
.Parameters("@ReceiptNumber").Value = Convert.ToInt32(intRecID)
.Parameters("@OwnerID").Value = Me.txtOwnerID.Text
.Parameters("@ReceiptDate").Value = IIf(Me.txtReceiptDate.Text.Length = 0, "", Date.Parse(Me.txtReceiptDate.Text))
.Parameters("@NewReceiptNumber").Value = IIf(Me.txtReceiptDate.Text.Length <> 0, "", Me.txtReceiptNumber.Text)
sqlConn.Open()
.ExecuteScalar()
If rbNew.Checked = True Then
Me.txtReceiptNumber.Text = Convert.ToInt32(sqlcmd.Parameters("@NewReceiptNumber").Value)
End If
sqlConn.Close()
End With
Catch ex As Exception
MsgBox(ex.Message.ToString, , "Receipt Not Created")
End Try
Return Saved
End Function
在运行程序时,我将OwnerID和OwnerID输入到OwnerID文本框中,例如12345
并在ReceiptDate文本框中输入一个有效日期,例如4月2日,然后单击提交"按钮.
我在ReceiptNumber文本框中显示了交易的收据号.
现在我的问题是:
有人可以为我提供一个在N层应用程序中重做相同项目的好策略吗?我想达到相同的目的,但使用的是使用WCF服务的N层体系结构.
On running the program I provide and OwnerID into the OwnerID textbox such as 12345
and a valid date into the ReceiptDate textbox such as 2 April then click on the submit button.
I get the receipt number for my transaction displayed in the ReceiptNumber textbox.
Now to my question:
Could someone help me with a good strategy for redoing the same project in an N-Tier application? I would like to achieve the same but using an N-Tier architecture which uses a WCF Service.
推荐答案
请仔细阅读Windows Communication Foundation-入门教程 [我需要有关WCF和n层的建议 [ ^ ]
Have a read through this Windows Communication Foundation - Getting Started Tutorial[^]
which I got from reading this I need advice regarding WCF and n-tier[^]
这篇关于使用WCFService帮助将项目转换为N层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!