3层架构中的网格视图填充 [英] grid view population in 3-tier architecture

查看:60
本文介绍了3层架构中的网格视图填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试在c#windows应用程序中实现3层架构。以下是我编码的3层架构层。



我想要的是btnListtrans_Click事件,我希望数据网格使用dataaccesslayer中的查询来填充表中的数据。



以下是我的3层应用程序的代码。我在代码中缺少什么来在网格视图中填充数据?你的帮助会非常明显。



另外,请对我的代码进行审核,并建议我一般的改进。



DB连接



Hi,

I am trying to implement 3-tier architecture in a c# windows application. Below are the layers of 3-tier architecture I coded.

What I want is on btnListtrans_Click event, I want a datagrid to populate data from a table using the query in the dataaccesslayer.

Below is the code of my 3-tier application. What am I missing in my code to populate data in grid view? Your help would be greatly appreciable.

Also, please have a review on my code and suggest me any improvements in general.

DB Connection

public class DBConnection
    {
        BindingSource bsource = new BindingSource();
        
        public static SqlConnection Getconnection()
        {
            string[] lines = File.ReadAllLines(@"C:\package.txt");
            string server;
            server = null;
            foreach (string line in lines)
            {
                if (line.StartsWith("servername :="))
                {
                    server = line.Substring(13);
                    break;
                }
            }
            SqlConnection cnn = new SqlConnection("user id=admin;password=pwd;server=" + server + ";" + "Trusted_Connection=no;database=db;connection timeout=30");
            try
            {
                cnn.Open();
                return cnn;
            }

            catch (Exception ex)
            {
                string ErrorMessage = "An error occurred while trying to connect to the server.";
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Environment.NewLine;
                ErrorMessage += ex.Message;
                MessageBox.Show(ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
        public DataTable executeSelectQuery(String _query, SqlParameter[] sqlParameter)
        {
            SqlCommand myCommand = new SqlCommand();
            DataTable dataTable = new DataTable();
            //dataTable = null;
            DataSet ds = new DataSet();
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            try
            {
                myCommand.Connection = Getconnection();
                myCommand.CommandText = _query;
                myCommand.Parameters.AddRange(sqlParameter);
                myCommand.ExecuteNonQuery();
                myAdapter.SelectCommand = myCommand;                
                myAdapter.Fill(ds);                
                dataTable = ds.Tables[0];
            }
            catch (NullReferenceException n)
            {
                string ErrorMessage = "An error occurred while trying to connect to the server.";
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Environment.NewLine;
                ErrorMessage += n.Message;
                MessageBox.Show(ErrorMessage, "Other error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            catch (SqlException e)
            {
                string ErrorMessage = "An error occurred while trying to connect to the server.";
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Environment.NewLine;
                ErrorMessage += e.Message;
                MessageBox.Show(ErrorMessage, "Other error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            finally
            {
            }
            return dataTable;
        }
    }







DataAccessLayer








DataAccessLayer


public class DataAccessLayer
    {
        private DBConnection DBConn;
        public DataAccessLayer()
        {
            DBConn = new DBConnection();
        }
        public DataTable ListData(string _transdate)
        {
            try
            {                
                string query = string.Format("select * from tablename where billdate>= @billdatedate");
                SqlParameter[] sqlParameters = new SqlParameter[1];
                sqlParameters[0] = new SqlParameter("@billdate", SqlDbType.Date);
                sqlParameters[0].Value = Convert.ToString(_transdate);                
                return DBConn.executeSelectQuery(query, sqlParameters);                
            }
            catch (Exception e)
            {
                string ErrorMessage = "An error occurred while trying to connect to the server.";
                ErrorMessage += Environment.NewLine;
                ErrorMessage += Environment.NewLine;
                ErrorMessage += e.Message;
                MessageBox.Show(ErrorMessage, "Other error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            finally
            {
                if (DBConnection.Getconnection() != null)
                    DBConnection.Getconnection().Dispose();
            }            
        }
    }







BusinessLogicsLayer





BusinessLogicsLayer

public class BusinessLogicsLayer
    {
        DataAccessLayer.DataAccessLayer objDAL = new DataAccessLayer.DataAccessLayer();
        public void ListData(string _transdate)
        {
            try
            {
                objDAL.ListData(_transdate);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }







PresentationLayer









PresentationLayer



public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        BusinessLogicsLayer.BusinessLogicsLayer objBLL = new BusinessLogicsLayer.BusinessLogicsLayer();        

        private void btnListtrans_Click(object sender, EventArgs e)
        {
            objBLL.ListData(dateTimePicker1.Text);
        }
    }

推荐答案

如何使用3层架构在gridview中填充 [ ^ ]



希望它会有所帮助..
how to populate in gridview using 3 tier architecture[^]

Hope it will help..






我做了一点点后得到了解决方案改变我的代码。感谢您的帮助..



BusinessLogicsLayer



Hi,

I got the solution after making a slight change in my code. Thanks for the help..

BusinessLogicsLayer

public class BusinessLogicsLayer
    {
        DataAccessLayer.DataAccessLayer objDAL = new DataAccessLayer.DataAccessLayer();
        public DataTable ListData(string _transdate)
        {
            try
            {
                return objDAL.ListData(_transdate);
            }
            catch(Exception ex)
            {
                throw ex;
            }            
        }
    }





PresentationLayer







PresentationLayer


<pre lang="c#">public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        BusinessLogicsLayer.BusinessLogicsLayer objBLL = new BusinessLogicsLayer.BusinessLogicsLayer();

        private void btnUnconfirm_Click(object sender, EventArgs e)
        {
            
        }

        private void btnListtrans_Click(object sender, EventArgs e)
        {
            //objBLL.ListData(dateTimePicker1.Text);
            dtgrdUnconfirm.DataSource = objBLL.ListData(dateTimePicker1.Text);            
        }
    }


这篇关于3层架构中的网格视图填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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