[原创]今天开发第一个三层结构模型的程序。
2005-06-21 15:52
585 查看
今天开发第一个三层结构模型的程序。
类库文件
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Mystore
{
/// <summary>
/// CustomersDB 的摘要说明。
/// </summary>
public class CustomerDetails
{
public String FullName;
public String Email;
public String Password;
}
public class CustomersDB
{
public CustomersDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public String Addcustomer(string fullName,string email,string password)
{
SqlConnection myConnection=new SqlConnection(ConfigurationSettings.AppSettings["connStr"]);
SqlCommand myCommand=new SqlCommand("CustomerAdd",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
//给存储过程添加参数
SqlParameter parameterFullName=new SqlParameter("@FullName",SqlDbType.NVarChar,50);
parameterFullName.Value=fullName;
myCommand.Parameters.Add(parameterFullName);
SqlParameter parameterEmail=new SqlParameter("@Email",SqlDbType.NVarChar,50);
parameterEmail.Value=email;
myCommand.Parameters.Add(parameterEmail);
SqlParameter parameterPassword=new SqlParameter("@Password",SqlDbType.NVarChar,50);
parameterPassword.Value=password;
myCommand.Parameters.Add(parameterPassword);
SqlParameter parameterCustomerID=new SqlParameter("@CustomerID",SqlDbType.Int,4);
parameterCustomerID.Direction=ParameterDirection.Output;
myCommand.Parameters.Add(parameterCustomerID);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
//使用存储过程输出参数计数CustomerID值
int customerID=(int)parameterCustomerID.Value;
return customerID.ToString();
}
}
}
.cs中调用
using Mystore;
private void Page_Load(object sender, System.EventArgs e)
{
Mystore.CustomersDB accountSystem=new Mystore.CustomersDB();
for(int i=1;i<20000;i++)
{
String customerId=accountSystem.Addcustomer("netboyc","netboyc@126.com","123456");
if(customerId!=null)
{
Response.Write("注册成功");
Response.Write(customerId);
}
}
存储过程
CREATE PROCEDURE CustomerAdd
(@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
@CustomerID int OUTPUT
)
AS
INSERT INTO customers
(
FullName,
Email,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT
@CustomerID=@@Identity
GO
类库文件
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Mystore
{
/// <summary>
/// CustomersDB 的摘要说明。
/// </summary>
public class CustomerDetails
{
public String FullName;
public String Email;
public String Password;
}
public class CustomersDB
{
public CustomersDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public String Addcustomer(string fullName,string email,string password)
{
SqlConnection myConnection=new SqlConnection(ConfigurationSettings.AppSettings["connStr"]);
SqlCommand myCommand=new SqlCommand("CustomerAdd",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
//给存储过程添加参数
SqlParameter parameterFullName=new SqlParameter("@FullName",SqlDbType.NVarChar,50);
parameterFullName.Value=fullName;
myCommand.Parameters.Add(parameterFullName);
SqlParameter parameterEmail=new SqlParameter("@Email",SqlDbType.NVarChar,50);
parameterEmail.Value=email;
myCommand.Parameters.Add(parameterEmail);
SqlParameter parameterPassword=new SqlParameter("@Password",SqlDbType.NVarChar,50);
parameterPassword.Value=password;
myCommand.Parameters.Add(parameterPassword);
SqlParameter parameterCustomerID=new SqlParameter("@CustomerID",SqlDbType.Int,4);
parameterCustomerID.Direction=ParameterDirection.Output;
myCommand.Parameters.Add(parameterCustomerID);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
//使用存储过程输出参数计数CustomerID值
int customerID=(int)parameterCustomerID.Value;
return customerID.ToString();
}
}
}
.cs中调用
using Mystore;
private void Page_Load(object sender, System.EventArgs e)
{
Mystore.CustomersDB accountSystem=new Mystore.CustomersDB();
for(int i=1;i<20000;i++)
{
String customerId=accountSystem.Addcustomer("netboyc","netboyc@126.com","123456");
if(customerId!=null)
{
Response.Write("注册成功");
Response.Write(customerId);
}
}
存储过程
CREATE PROCEDURE CustomerAdd
(@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
@CustomerID int OUTPUT
)
AS
INSERT INTO customers
(
FullName,
Email,
Password
)
VALUES
(
@FullName,
@Email,
@Password
)
SELECT
@CustomerID=@@Identity
GO
相关文章推荐
- Atitit.软件开发的三层结构isv金字塔模型
- 大数据Spark “蘑菇云”行动前传第3课:在IDE下开发第一个Scala程序透彻解析及Scala控制结构详解实战
- 哇卡卡卡~今天Qt第一个原创程序调试成功
- Atitit.软件开发的三层结构isv金字塔模型
- 程序开发三层结构
- Atitit.软件开发的三层结构isv金字塔模型
- C# 项目开发三层结构模型
- .NET开发Silverlight程序:界面和对象模型
- 三层结构建立步骤和vss创建步骤【原创】
- Android—开发自学历程(1)-Activity(3)-第一个小程序
- 基于Xcode4开发第一个iPhone程序:“Hello World”
- iBatis开发环境搭建和第一个程序
- Windows mobile 开发入门—第一个程序"hello world",链接调试,模拟器的使用
- Android开发环境搭建及程序结构
- 三、第一个J2ME程序[J2ME入门开发教程]
- 三层结构开发模式
- Symbol开发2---第一个程序
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
- swift语言ios开发 第一个程序hello world!
- 【原创】《Linux高级程序设计》杨宗德著 - 进程管理与程序开发 - 信号量通信机制