C#可视化小项目

c#连接数据库进行增删改查。

DBHelper:(内含连接数据库,获取DataReader、DataSet等)

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo1
{
    class DBHelper
    {
        //数据库连接字符串
        public static string ConnStr = "server=.;database=SharedBikeDB;Integrated Security=True";
        //数据库连接对象
        private static SqlConnection Conn = null;

        //初始化数据库连接
        private static void InitConnection()
        {
            if (Conn == null)
            {
                Conn = new SqlConnection(ConnStr);
            }
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
            if (Conn.State == ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }

        //执行查询操作获取dataReader
        public static SqlDataReader GetDataReader(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr,Conn);
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        //执行查询操作获取dataSet
        public static DataSet GetDataSet(string sqlStr)
        {
            InitConnection();
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(sqlStr, Conn);
            dap.Fill(ds);
            Conn.Close();
            return ds;
        }

        //执行查询操作获取dataTable
        public static DataTable GetDataTable(string sqlStr)
        {
            return GetDataSet(sqlStr).Tables[0];
        }

        //执行增删改操作
        public static bool ExecuteNonQuery(string sqlStr)
        {
            InitConnection();
            SqlCommand sqlCommand = new SqlCommand(sqlStr,Conn);
            int result = sqlCommand.ExecuteNonQuery();
            Conn.Close();
            return result>0;
        }

        //执行集合函数操作
        public static object ExecuteScalar(string sqlStr)
        {
            InitConnection();
            SqlCommand sqlCommand = new SqlCommand(sqlStr,Conn);
            object result = sqlCommand.ExecuteScalar();
            Conn.Close();
            return result;
        }
    }
}

Login页面(非空验证/是否为管理员验证)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace demo1
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //登录事件
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
            {
                //判断是否为空
                MessageBox.Show("登录账号或密码为空", "提示");
            }
            else
            {
                string sql = string.Format("select * from Customers where LoginId = '{0}' and LoginPwd = '{1}'", textBox1.Text, textBox2.Text);
                DataTable dt = DBHelper.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    int isAdmin = Convert.ToInt32(dt.Rows[0]["IsAdmin"]);
                    Program.CustomerID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    if (isAdmin == 1)
                    {
                        MainForm mainForm = new MainForm();
                        mainForm.Show();
                    }
                    else
                    {
                        UserBikeForm userBike = new UserBikeForm(Program.CustomerID.ToString());
                        userBike.Show();
                    }
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录账号和密码有误!");
                }
            }
            
        }
    }
}

列表页面(自动显示)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace demo1
{
    public partial class ViewCustomerForm : Form
    {
        public ViewCustomerForm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ViewCustomerForm_Load(object sender, EventArgs e)
        {
            //自动显示列表
            string sql = "select * from Customers";
            DataTable dt = DBHelper.GetDataTable(sql);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值