本文介绍了班级&在C#中的方法,这是一个很好的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试构建一个与我的asp.net Web应用程序中的数据库交互的类。我需要你对如何设计它的看法,这是我心中的一个例子 public class 用户 { public int UserID { get ; set ; } public string 名称{ get ; set ; } public string 姓氏{ get ; set ; } } public class UserData { public UserData(){} public object 插入(用户用户) { object obj = 空; 使用(SqlConnection conn = new SqlConnection(Data.GetConn())) { 使用(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = INSERT INTO Users(Name,Surname)VALUES(@ Name,@ Surname)SELECT SCOPE_IDENTITY()as UserID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.Parameters.AddWithValue( @ Name,user.Name ); cmd.Parameters.AddWithValue( @ Surname,user.Surname); obj = cmd.ExecuteScalar(); } } return obj; } } // Onclick按钮 用户user = 新用户(); user.Name = Name.Text; user.Surname = Surname.Text; UserData userD = new UserData(); object obj = userD.Insert(user); 解决方案 我个人不会拆分它。 我会构建一个包含属性的用户(或人)类,并且还有一些方法:加载,保存,删除等。你还可以预见一个返回用户对象列表的静态方法。 我做了类似的事情,为此我创建了一个抽象类来处理部分代码和拥有一些类似的属性(比如唯一的id和系统列)以及从该抽象类派生的所有对象,迫使每个类的工作方式变成类似的设计。 这并不意味着你的设计是错误的,我以前见过它,但恕我直言,只有你的应用程序趋于增长,非常大,你可以将Business Objects和Business Logic放入单独的程序集中。 希望这会有所帮助。 顺便提一下你的格式是正确的。 你需要你的按钮代码方法或按钮点击事件。 public submitData() { // Onclick按钮用户user =新用户( ); user.Name = Name.Text; user.Surname = Surname.Text; UserData userD = new UserData(); object obj = userD.Insert(user); } protected button_OnClick(object sender,EventArgs e) { submitData(); } 否则以你的格式说出来 我不会将返回类型用作对象 public static bool 插入(用户用户,输出 int userID) { userID = -1; 使用(SqlConnection conn = new SqlConnection(Data.GetConn())) { 使用(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = INSERT INTO Users(Name,Surname)VALUES(@ Name,@ Surname)SELECT SCOPE_IDENTITY()as UserID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.Parameters.AddWithValue( @ Name,user.Name ); cmd.Parameters.AddWithValue( @ Surname,user.Surname); var obj = cmd.ExecuteScalar(); if (obj!= null && int .TryParse(obj.ToString(), out userID) { 返回 true ; } } } 返回 false ; } int id; if ( !UserData.Insert(user, out id)) // 因为我已将方法标记为静态,所以您无需创建实例来调用方法 { // 插入用户数据时出错,显示要使用的警报r } else { // 插入成功,使用用户ID执行某些操作 } I am trying to build a class that interacts with the database in my asp.net web application. I need your opinion on how to design it, here's an example of what I have in mindpublic class User{ public int UserID { get; set; } public string Name { get; set; } public string Surname { get; set; } }public class UserData{ public UserData() { } public object Insert(User user) { object obj = null; using (SqlConnection conn = new SqlConnection(Data.GetConn())) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO Users (Name,Surname) VALUES (@Name,@Surname) SELECT SCOPE_IDENTITY() as UserID"; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.Parameters.AddWithValue("@Name", user.Name); cmd.Parameters.AddWithValue("@Surname", user.Surname); obj = cmd.ExecuteScalar(); } } return obj; } }//Onclick buttonUser user = new User();user.Name = Name.Text;user.Surname = Surname.Text;UserData userD = new UserData();object obj = userD.Insert(user); 解决方案 Personally I wouldn't split this.I would build a user (or person) class that holds the properties and also has a few methods: Load, Save, Delete eg. You can also foresee a static method returning a list of user objects.I did something similar and to do that I created an abstract class that handled parts of the code and holds some similar properties (like the unique id and system columns) and all objects derived from that abstract class forcing the way of working for each class into a similar design.That doesn't mean your design is wrong, I have seen it before, but IMHO it only makes sense if your applications tends to grow very, very large and you can put the Business Objects and Business Logic into seperate assemblies.Hope this helps.By the way your format is right.you have to your button code in a method or inside your button click event.public submitData(){//Onclick buttonUser user = new User();user.Name = Name.Text;user.Surname = Surname.Text; UserData userD = new UserData();object obj = userD.Insert(user); }protected button_OnClick(object sender, EventArgs e){ submitData();}Otherwise it right to say in your formatI would not use return type as objectpublic static bool Insert(User user, out int userID){ userID =-1; using (SqlConnection conn = new SqlConnection(Data.GetConn())) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO Users (Name,Surname) VALUES (@Name,@Surname) SELECT SCOPE_IDENTITY() as UserID"; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.Parameters.AddWithValue("@Name", user.Name); cmd.Parameters.AddWithValue("@Surname", user.Surname); var obj = cmd.ExecuteScalar(); if(obj!=null && int.TryParse(obj.ToString(), out userID) { return true; } } } return false;}int id;if(!UserData.Insert(user, out id)) // since i have mark the method as static you don't need to create instance to call the method{ // error inserting user data, show alert to user }else{ //insert success , do something with user id } 这篇关于班级&在C#中的方法,这是一个很好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 16:41