如何使用3轮胎架构在数据库sql server 2008中插入数据 [英] how to insert data in database sql server 2008 with 3 tire Architecture

查看:94
本文介绍了如何使用3轮胎架构在数据库sql server 2008中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello先生我用户三层架构和sql server 2008数据库中插入数据的代码是什么





快速回复谢谢

解决方案

3层体系结构是软件开发领域非常知名的热门词汇,无论是基于Web还是基于桌面。

使用C#的ASP.NET中的3层架构 [ ^ ]

使用ASP.NET构建3层应用程序

[ ^ ]



3层申请是一个组织的程序在层上分为三个主要的析取层。在这里,您可以看到这些层如何提高代码的可重用性。

ASP.NET中的三层架构 [ ^ ]



关于C#中 3层架构的背景,优点和用法的CP文章。

C#中的3层架构 [ ^ ]



详细说明:

3 - 使用c#在asp.net中的层架构[ ^ ]



更多:

C#中的3层体系结构 [ ^ ]

3层体系结构示例 [ ^ ]

ASP.NET中的3层体系结构使用C# [ ^ ]


假设您正在使用三层体系结构,那么只需创建两个类文件,如Class1.cs和Class2.cs,第一个Class1.cs包含表的属性,如:





Class1.cs中的代码



namespace _c lass1 //也创建命名空间

{

公共类Class1

{

public Class1()

{



}



//创建一个属性,因为你的表有字段

public int Id {get;组; }

公共字符串名称{get;组; }

}

}





现在在Class2.cs中编写男女同校

首先声明命名空间并使用属性声明类文件的命名空间:



使用System.Data.SqlClient;

使用_class1;



命名空间_class2

{

公共类Class2

{

Class1 c1 = new Class1();

public Class2()

{



}



public void InsertData()

{

SqlConnection cnn = new SqlConnection(数据源= hoth;初始目录= Chintan;用户ID = sa);

cnn.Open();

SqlCommand cmd = new SqlCommand(插入到tbl_data值中(@name),cnn);

cmd.Parameters.AddWithValue(@ name,c1.name); //c1.name从属性类文件访问

cmd.ExecuteNonQuery();

cnn.Close();

}

}

}





现在在代码隐藏文件后创建两个类文件的对象如下代码所示:



命名空间_class1;

命名空间_class2;



public partial class Default7:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{< br $>


}



protected void Button1_Click(对象发送者,EventArgs e)

{

Class1 c1 = new Class1();

Class2 c2 = new Class2();



c1 .name = txtname.text;



c2.InsertData(c1);



lblmessage.text = 记录插入成功...... !!!;

}

}





我希望它对你有帮助


示例:



bal_Attendance obj = new bal_Attendance(); //公共//



按钮()

{



< pre lang =cs> obj.branch_id = int .Parse(ddlbranch.SelectedItem.Value);
obj.date = dt.ToShortDateString();
obj.attendance = status.ToString();
obj.employee_id = int .Parse(e_id.Text);
obj.employeename = e_name.Text;







}


hello sir i user three tier Architecture and what is the code of insert data in sql server 2008 database


quick reply to me thanks

解决方案

3-Tier architecture is a very well know buzz word in the world of software development whether it web based or desktop based.
3-Tier Architecture in ASP.NET with C#[^]
Building a 3-Tier Application using ASP.NET
[^]

3-tier application is a program which is organized into three major disjunctive tiers on layers. Here you can see that how these layers increase the reusability of codes.
Three Tier Architecture in ASP.NET[^]

A CP article on the background, advantages and usage of 3-tier architectures in C#.
3-tier architecture in C#[^]

Detailed explanation:
3-Tier Architecture in asp.net using c#[^]

Some more:
3-tier architecture in C#[^]
3-Tier Architecture Examples[^]
3-Tier Architecture in ASP.NET with C#[^]


Suppose you are working with three tier Architecture then just create Two Class file as like Class1.cs and Class2.cs first one Class1.cs contains with Properties of table as like:


Code in Class1.cs

namespace _class1 //also create namespace
{
public class Class1
{
public Class1()
{

}

//create a properties as your table have field
public int Id { get; set; }
public string name { get; set; }
}
}


now write coed in Class2.cs
first declare namespace and used namespace of properties declaration class file:

using System.Data.SqlClient;
using _class1;

namespace _class2
{
public class Class2
{
Class1 c1 = new Class1();
public Class2()
{

}

public void InsertData()
{
SqlConnection cnn = new SqlConnection("Data Source=hoth;Initial Catalog=Chintan;User ID=sa");
cnn.Open();
SqlCommand cmd = new SqlCommand("Insert into tbl_data values(@name)",cnn);
cmd.Parameters.AddWithValue("@name",c1.name); //c1.name access from property class file
cmd.ExecuteNonQuery();
cnn.Close();
}
}
}


Now create object of both class file in code behind file as below given code:

namespace _class1;
namespace _class2;

public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();

c1.name = txtname.text;

c2.InsertData(c1);

lblmessage.text = "Record Inserted Successfully....!!!";
}
}


I hope its helpful to you


Example:

bal_Attendance obj = new bal_Attendance(); //public//

button()
{

obj.branch_id = int.Parse(ddlbranch.SelectedItem.Value);
                   obj.date = dt.ToShortDateString();
                   obj.attendance = status.ToString();
                   obj.employee_id = int.Parse(e_id.Text);
                   obj.employeename = e_name.Text;




}


这篇关于如何使用3轮胎架构在数据库sql server 2008中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆