using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class FormMannage : Form
{
// 实例字段(非静态)
public int Pid;
public string Pname;
public string Stype;
public string Savedate;
public string Content;
public byte[] ImageData;
public FormMannage()
{
InitializeComponent();
}
private void LoadPoint()
{
dgv.Rows.Clear();
using (Dao2 dao = new Dao2())
{
dao.Connect();
string sql = "SELECT * FROM T_Point";
using (SqlDataReader reader = dao.Read(sql))
{
while (reader.Read())
{
// 处理图片数据
byte[] imageData = null;
if (!reader.IsDBNull(5)) // 假设Image是第6列
{
imageData = (byte[])reader[5];
}
dgv.Rows.Add(
reader[0].ToString(),
reader[1].ToString(),
reader[2].ToString(),
reader[3].ToString(),
reader[4].ToString(),
imageData != null ? "有图片" : "无图片"
);
}
}
}
}
//刷新数据
private void button4_Click(object sender, EventArgs e)
{
LoadPoint();
}
private void lbName_Click(object sender, EventArgs e)
{
}
private void txtKey_TextChanged(object sender, EventArgs e)
{
}
//进行关键字搜索查询
private void btnSearch_Click(object sender, EventArgs e)
{
//拿到关键字
string key = txtKey.Text.Trim();
//以关键字进行模糊查询
Dao2 dao = new Dao2();
dao.Connect();
string sql = $"select * from T_Point where Pname like '%{key}%'or Stype like '%{key}%'";
SqlDataReader reader = dao.Read(sql);
//清空表格
dgv.Rows.Clear();
//结果显示到表格控件中
while (reader.Read())
{
dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(),
reader[3].ToString(), reader[4].ToString(),reader[5].ToString());
}
reader.Close();
dao.Close();
}
//删除知识点
private void button3_Click(object sender, EventArgs e)
{
//获取到当前选中的知识点编号 key
if (lbid.Text == "NULL")
{
MessageBox.Show("未选中知识点", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//删除对应的数据
Dao2 dao = new Dao2();
dao.Connect();
string sql = $"delete T_Point where Pid='{int.Parse(lbid.Text)}'";
if (dao.Execute(sql) > 0)
{
//删除成功
//两个对应的lb数据的修改 null
lbid.Text = "NULL";
lbName.Text = "NULL";
//更新表格数据
LoadPoint();
dao.Close();
MessageBox.Show("删除成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
dao.Close();
MessageBox.Show("删除失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//查看简介
private void button5_Click(object sender, EventArgs e)
{
if (lbName.Text == "NULL")
{
MessageBox.Show("未选中知识点!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 确保 lbid 是正确的知识点编号
if (string.IsNullOrEmpty(lbid.Text) || lbid.Text == "NULL")
{
MessageBox.Show("知识点编号无效!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 获取知识点的编号
int pointId;
if (!int.TryParse(lbid.Text, out pointId))
{
MessageBox.Show("知识点编号格式错误!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Dao2 dao = new Dao2();
dao.Connect();
try
{
string sql = "SELECT [Content] FROM T_Point WHERE Pid = @Pid";
// 使用dao.Connection替代未定义的_connection
using (SqlCommand cmd = new SqlCommand(sql, dao.Connection)) // 关键修改点
{
cmd.Parameters.AddWithValue("@Pid", pointId);
object result = cmd.ExecuteScalar();
if (result != null)
{
string introduce = result.ToString();
MessageBox.Show(introduce, $"{lbName.Text}的简介", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("未找到该知识点的简介!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
catch (SqlException ex)
{
MessageBox.Show($"获取简介时发生错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
dao.Close();
}
}
//选中数据
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || dgv.Rows[e.RowIndex].Cells[0].Value == null)
return;
DataGridViewRow row = dgv.Rows[e.RowIndex];
// 获取选中行的数据
lbid.Text = row.Cells[0].Value.ToString();
lbName.Text = row.Cells[1].Value.ToString();
// 更新类字段(去掉static关键字)
Pid = int.Parse(row.Cells[0].Value.ToString());
Pname = row.Cells[1].Value.ToString();
Stype = row.Cells[2].Value.ToString();
Savedate = row.Cells[3].Value.ToString();
Content = row.Cells[4].Value.ToString();
// 获取图片数据(从数据库重新获取)
using (Dao2 dao = new Dao2())
{
dao.Connect();
string sql = $"SELECT Image FROM T_Point WHERE Pid={Pid}";
using (SqlDataReader reader = dao.Read(sql))
{
if (reader.Read() && !reader.IsDBNull(0))
{
ImageData = (byte[])reader[0];
}
else
{
ImageData = null;
}
}
}
}
//修改知识点
private void button2_Click(object sender, EventArgs e)
{
// 确保有选中的知识点
if (lbid.Text == "NULL")
{
MessageBox.Show("未选中知识点!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
FormUpdatePoint form = new FormUpdatePoint();
form.Pid = this.Pid; // 使用当前实例的值
form.Pname = this.Pname;
form.Stype = this.Stype;
form.Savedate = this.Savedate;
form.Content = this.Content;
form.ImageData = this.ImageData;
form.ShowDialog();
}
private void FormMannage_Load(object sender, EventArgs e)
{
LoadPoint(); // 加载知识点数据
lbid.Text = "NULL";
lbName.Text = "NULL"; // 初始化标签
}
}
}
怎么实现知识点导出
最新发布