c#封装类

 以sqlite为例,封装一个类

首先,添加引用

using System.Data;

using System.Windows.Forms;

using System.Data.SQLite;


然后定义类名字,并完善相关代码


         public void FillDataToGridView(string sqlStr, SQLiteConnection connection, DataGridView dataGridView, Label label)//封装成类

        {

            try

            {

                SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqlStr, connection);

                DataTable dataTable = new DataTable();

                dataAdapter.Fill(dataTable);

                dataGridView.DataSource = dataTable;

                int dataNum = dataTable.Rows.Count;

                label.Text = "当前共有【" + dataNum.ToString() + "】条数据";

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            finally

            {

                connection.Close();

            }

        }


最后,引用封装的类

DataHelper dataHelper = new DataHelper(); dataHelper.FillDataGridView(GlobalData.Sql_Str, Conn_Add, dataGridView1, label_Tips);

此博客中的热门博文

onlyoffice添加中文字体和字号

为签到平台添加获取 Cookie 功能 GetCookies

CentOS+宝塔部署seafile教程