您的位置:首页 > 其它

[原创]今天开发第一个三层结构模型的程序。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: