C#学习笔记(6)——大项目增删改查

说明(2017-5-28 11:35:39):

1. 需要注意的地方很多

2. 首先是连接字符串str要直接写在类里面,不然每个按钮里面都要写一遍。

3. 查询用到sqlDataReader等三个方法,其他增删改只用到sqlCommond和sqlConnection。

4. sqlConnection记得要open!

5. 其他的记不起来了,总之要经常练习!

运行结果:

C#学习笔记(6)——大项目增删改查

代码:

 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 System.Data.SqlClient; namespace _03大项目查找
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); }
string str = "Data Source=.; Initial Catalog=mysql; Integrated Security=True;"; private void Form1_Load(object sender, EventArgs e)
{ loadStudent();
}
/// <summary>
/// 加载学生信息
/// </summary>
/// <param name="p">p是删除标识,0删除,1未删除</param>
public void loadStudent(int p)
{
List<Student> list = new List<Student>(); using (SqlConnection con = new SqlConnection(str))
{
con.Open();
string sql = "select id, name, chinese, math, flag from myclass where flag = " + p;
using (SqlCommand cmd = new SqlCommand(sql, con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Student st = new Student();
st.Id = Convert.ToInt32(reader["id"]);
st.Name = Convert.ToString(reader["name"]);
st.Chinese = Convert.ToString(reader["chinese"]);
st.Math = Convert.ToString(reader["math"]);
st.Flag = Convert.ToInt32(reader["flag"]);
list.Add(st);
}
}
}
}
} dgv.DataSource = list;
dgv.SelectedRows[].Selected = false;
}
/// <summary>
/// 添加学生信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int n = -;
string name = txtName.Text;
string chinese = txtChinese.Text;
string math = txtMath.Text;
string sql = string.Format("insert into myclass(name,chinese,math,flag) values('{0}','{1}','{2}',0)", name, chinese, math);
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
n = cmd.ExecuteNonQuery();
MessageBox.Show(n > ? "操作成功!" : "操作失败!");
}
}
loadStudent(); //MessageBox.Show("123");
}
/// <summary>
/// 删除学生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDel_Click(object sender, EventArgs e)
{
int n = -;
string id = dgv.SelectedRows[].Cells[].Value.ToString();
string name = dgv.SelectedRows[].Cells[].ToString();
string chinese = dgv.SelectedRows[].Cells[].ToString(); string sql = string.Format("update myclass set flag = 1 where id ={0}", id);
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
n = cmd.ExecuteNonQuery();
MessageBox.Show(n > ? "操作成功!" : "操作失败!s");
}
}
loadStudent();
}
/// <summary>
/// 选中一行,显示在修改框中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
lbId.Text = dgv.SelectedRows[].Cells[].Value.ToString();
txtUname.Text = dgv.SelectedRows[].Cells[].Value.ToString();
txtUchinese.Text = dgv.SelectedRows[].Cells[].Value.ToString();
txtUmath.Text = dgv.SelectedRows[].Cells[].Value.ToString(); } private void btnUpdate_Click(object sender, EventArgs e)
{
int n = -;
if (dgv.SelectedRows.Count>)
{
string sql = string.Format("update myclass set name = {0}, chinese = {1}, math = {2} where id = {3}", txtUname.Text,txtUchinese.Text,txtUmath.Text,lbId.Text);
using (SqlConnection con = new SqlConnection(str))
{
using (SqlCommand cmd = new SqlCommand(sql,con))
{
con.Open();
n = cmd.ExecuteNonQuery();
MessageBox.Show(n>?"操作成功!":"操作失败!");
}
}
}
else
{
MessageBox.Show("请输入数据!");
}
loadStudent();
}
}
}
上一篇:Spring学习笔记:声明式事务管理增删改查业务


下一篇:vue-cli 3.0 axios 跨域请求代理配置及生产环境 baseUrl 配置