DbHelper.cs//简单的数据库连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SimpleTest
{
public class DbHelper
{
public static string strCon = "server=.;database=UserInfo;integrated security=true";
public static SqlConnection con = new SqlConnection(strCon);
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SimpleTest
{
public partial class Form1 : Form
{
//int index;全局变量可以设置使用也可不使用凭自己喜好
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetData();
}
//绑定数据方法
public void GetData()
{
string sql = "select * from UserInfo";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql,DbHelper.con);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
//添加事件
private void button1_Click(object sender, EventArgs e)
{
string sql = "insert into UserInfo values(@name,@pwd)";
SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
try
{
DbHelper.con.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("添加成功!");
GetData();
textBox1.Text = "";
textBox2.Text = "";
}
else
{
MessageBox.Show("添加失败!");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
DbHelper.con.Close();
}
}
//单击单元格内容
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//index = e.RowIndex;
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
//textBox1.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
//textBox2.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
}
//修改事件
private void button2_Click(object sender, EventArgs e)
{
string sql = "update UserInfo set LoginName=@name, LoginPwd=@pwd where UserId=@id";
SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
cmd.Parameters.AddWithValue("@id", int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
try
{
DbHelper.con.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("修改成功!");
GetData();
textBox1.Text = "";
textBox2.Text = "";
}
else
{
MessageBox.Show("修改失败!");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
DbHelper.con.Close();
}
}
//删除事件
private void button3_Click(object sender, EventArgs e)
{
//string sql = "delete UserInfo where userid= " + int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
string sql = "delete UserInfo where userid=@id ";
SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
cmd.Parameters.AddWithValue("@id", int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
try
{
DbHelper.con.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
MessageBox.Show("删除成功!");
GetData();
textBox1.Text = "";
textBox2.Text = "";
}
else
{
MessageBox.Show("删除失败!");
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
DbHelper.con.Close();
}
}
//退出事件
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
设置的界面如下:
运行效果如下:
退出直接退出应用程序