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;
}
}
}