如何使用C#在Windows窗体应用程序中使用3层架构在Datagirdview中使用卷曲操作 [英] How Can I Used Curd Operation In Datagirdview Using 3 Layer Architecture In Windows Forms Application Using C#

查看:73
本文介绍了如何使用C#在Windows窗体应用程序中使用3层架构在Datagirdview中使用卷曲操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是datalayer中的代码



公共班级学生

{



SqlConnection连接=新的SqlConnection(数据源........... ....;;

DataTable dt = new DataTable();

DataSet ds = new DataSet();

SqlCommand cmd = new SqlCommand();

public DataTable Read()

{

connection.Open();

cmd .CommandText =SELECT * from Student;

SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText,connection);



da.Fill (ds);

//cmd.ExecuteReader();

connection.Close();

dt = ds.Tables [0] ;

返回dt;

}

public void Add(string studentName,string FatherName,string ClassName)

{

cmd.CommandText =INSERT INTO Student(StudentName,FatherName,ClassName)VALUES(@StudentName,@ FatherName,@ ClassName);



cmd.Parameters.AddWithValue(@ StudentName,studentName);

cmd.Parameters.AddWithValue(@ FatherName,FatherName);

// cmd .Parameters.AddWithValue(@ DateOfBirth,DateofBirth);

cmd.Parameters.AddWithValue(@ ClassName,ClassName);



cmd.CommandType = CommandType.Text;

cmd.Connection = connection;

connection.Open();

cmd.ExecuteNonQuery() ;



}





这里我不想使用参数请替代??????



public void Update(string studentName,string FatherName,string ClassName)

{

cmd.CommandT ext =更新学生SET FatherName = @ FatherName,ClassName = @ ClassName;

cmd.Parameters.AddWithValue(@ StudentName,studentName);

cmd.Parameters .AddWithValue(@ FatherName,FatherName);

cmd.Parameters.AddWithValue(@ ClassName,ClassName);

cmd.CommandType = CommandType.Text;

cmd.Connection = connection;



connection.Open();

cmd.ExecuteNonQuery(); < br $>


}





公共DataTable清除()

{

SqlCommand cmd = new SqlCommand(DELETE FROM Student,连接);

connection.Open();

cmd .ExecuteNonQuery();

connection.Close();

返回dt;

}





t他的代码在bussinesslayer

这里我也想要使用参数plese告诉我另类







公共类BClass

{

公共DataTable GetStudentAccount()

{

{

学生stu =新学生();

返回stu.Read();

< br $>
}

}



公共DataTable RemoveStudent()

{

{

学生stu =新学生();

返回stu.Clear();



}

}



公共布尔保存()

{

学生stu =新学生();



stu.Add(asim,ali,5th);

返回true;





}



public bool UpdateStudent()

{

Student stu = new Student();



stu.Update(asim,fazal,6th);

返回true;

< br $>


}

}



<< big> b>这是behine的表示层

请告诉我替代的方式我想要使用参数而不是entityframework并且也只使用3层架构


< br $>


公共部分类Form1:表格

{

public Form1()

{

InitializeComponent();

}













private void button2_Click(object sender,EventArgs e)

{

BClass p = new BClass();

thi s.dataGridView2.DataSource = p.GetStudentAccount();

}



private void Form1_Load_1(object sender,EventArgs e)

}



private void dataGridView2_CellContentClick br $>
{



}



private void button3_Click(object sender,EventArgs e )

{

BClass a = new BClass();

this.dataGridView2.DataSource = a.save();

}



private void button4_Click(object sender,EventArgs e)

{

BClass u = new BClass();

this.dataGridView2.DataSource = u.UpdateStudent();



}



private void button5_Click(object sender,EventArgs e)

{

BClass d = new BClass();

this.dataGridView2.DataSource = d.RemoveStudent();

}









}

this is code in datalayer

public class Student
{

SqlConnection connection = new SqlConnection("Data Source...............");
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
public DataTable Read()
{
connection.Open();
cmd.CommandText = "SELECT * from Student";
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, connection);

da.Fill(ds);
//cmd.ExecuteReader();
connection.Close();
dt = ds.Tables[0];
return dt;
}
public void Add(string studentName, string FatherName, string ClassName )
{
cmd.CommandText = "INSERT INTO Student (StudentName,FatherName,ClassName) VALUES (@StudentName, @FatherName,@ClassName )";

cmd.Parameters.AddWithValue("@StudentName", studentName);
cmd.Parameters.AddWithValue("@FatherName", FatherName);
//cmd.Parameters.AddWithValue("@DateOfBirth", DateofBirth);
cmd.Parameters.AddWithValue("@ClassName", ClassName);

cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();

}


here i dont wana to use parameter please alternative??????

public void Update(string studentName, string FatherName, string ClassName)
{
cmd.CommandText = "Update Student SET FatherName=@FatherName,ClassName=@ClassName";
cmd.Parameters.AddWithValue("@StudentName", studentName);
cmd.Parameters.AddWithValue("@FatherName", FatherName);
cmd.Parameters.AddWithValue("@ClassName", ClassName);
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;

connection.Open();
cmd.ExecuteNonQuery();

}


public DataTable Clear()
{
SqlCommand cmd = new SqlCommand("DELETE FROM Student", connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
return dt;
}


this code in bussinesslayer
here i also dnt wanna use parameter plese tell me alternative



public class BClass
{
public DataTable GetStudentAccount()
{
{
Student stu = new Student();
return stu.Read();

}
}

public DataTable RemoveStudent()
{
{
Student stu = new Student();
return stu.Clear();

}
}

public bool save()
{
Student stu = new Student();

stu.Add("asim", "ali", "5th");
return true;


}

public bool UpdateStudent()
{
Student stu = new Student();

stu.Update("asim", "fazal", "6th");
return true;


}
}

<<big>b>this is behine the presentation layer
kindly tell me alternative way where i dnt wanna use parameter and not entityframework and also use just 3 layer architecture



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}






private void button2_Click(object sender, EventArgs e)
{
BClass p = new BClass();
this.dataGridView2.DataSource = p.GetStudentAccount();
}

private void Form1_Load_1(object sender, EventArgs e)
{

}

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
BClass a = new BClass();
this.dataGridView2.DataSource = a.save();
}

private void button4_Click(object sender, EventArgs e)
{
BClass u = new BClass();
this.dataGridView2.DataSource = u.UpdateStudent();

}

private void button5_Click(object sender, EventArgs e)
{
BClass d = new BClass();
this.dataGridView2.DataSource = d.RemoveStudent();
}




}

推荐答案

在创建按钮上单击显示弹出,然后用户填写详细信息,然后单击确定单击调用将获取所有值的函数,然后在数据库上更新它,然后重新加载网格。

同样所有的功能。

我不知道你为什么问这个问题。

http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm [ ^ ]
On Create Button Click show pop up and then the user fill the details and on OK click call the function which will take all the values and then update it on the database and then reload the grid.
Do same for all the functionality.
I dont know why you asked this question.
http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm[^]


参考:

1. 在没有任何向导的情况下用C#编写N层应用程序 - 第一部分 [ ^ ]

2. 编码没有任何向导的C#中的N层应用程序:第二部分 [ ^ ]
Refer:
1. Coding an N-Tier Application in C# without any Wizard - Part I[^]
2. Coding an N-Tier Application in C# Without Any Wizard: Part II[^]


这篇关于如何使用C#在Windows窗体应用程序中使用3层架构在Datagirdview中使用卷曲操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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