求一个C#对数据库的增删改查代码和ListView的代码。
2个回答
2013-05-11
展开全部
这段代码太实用了: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace T5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FillListView();
}
private void FillListView()
{
List<Student> list = GetStudents();
//填充listview
foreach (Student s in list)
{
//创建listviewitem对象
ListViewItem item = new ListViewItem(s.Id);
//给item其它三个列添加数据
item.SubItems.AddRange(new string[] {s.Name,s.Age,s.Address });
//将item对象添加到listview的items集合中
lstStudents.Items.Add(item);
}
} private List<Student> GetStudents()
{
//清空listview
lstStudents.Items.Clear();
//创建学生集合
List<Student> list = new List<Student>();
//创建xml文档对象
XmlDocument doc = new XmlDocument();
//加载文档内容
doc.Load("../../students.xml");
//获取根节点
//XmlNode root = doc.DocumentElement;
XmlNode root = doc.SelectSingleNode("students");
foreach (XmlNode stu in root.ChildNodes)
{
Student s = new Student();
//获取id属性的值
s.Id = stu.Attributes["id"].Value;
//不用循环的做法
s.Name = stu.ChildNodes[0].InnerText;
s.Age = stu.ChildNodes[1].InnerText;
s.Address = stu.ChildNodes[2].InnerText;
//用循环的做法
//foreach (XmlNode node in stu.ChildNodes)
//{
// switch (node.Name)
// {
// case "name":
// s.Name = node.InnerText;
// break;
// case "age":
// s.Age = node.InnerText;
// break;
// case "address":
// s.Address = node.InnerText;
// break;
// }
//}
//将学生对象添加到集合中
list.Add(s);
}
//返回学生集合
return list;
} //删除
private void button4_Click(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode stu in root.ChildNodes)
{
if (stu.Attributes["id"].Value == lstStudents.SelectedItems[0].Text)
{
//删除选中的学生节点
root.RemoveChild(stu);
//更新xml文档
doc.Save("../../students.xml");
//刷新listview
FillListView();
MessageBox.Show("删除成功");
return;
}
}
MessageBox.Show("删除失败");
}
else
{
MessageBox.Show("请选中要删除的记录");
}
} private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
//将选中的记录各个字段的值显示到文本框中
txtId.Text = lstStudents.SelectedItems[0].Text;
txtName.Text = lstStudents.SelectedItems[0].SubItems[1].Text;
txtAge.Text = lstStudents.SelectedItems[0].SubItems[2].Text;
txtAddress.Text = lstStudents.SelectedItems[0].SubItems[3].Text;
}
} //修改
private void button3_Click(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode stu in root.ChildNodes)
{
if (stu.Attributes["id"].Value == lstStudents.SelectedItems[0].Text)
{
//更新当前学生各个节点的innertext
stu.ChildNodes[0].InnerText = txtName.Text;
stu.ChildNodes[1].InnerText = txtAge.Text;
stu.ChildNodes[2].InnerText = txtAddress.Text; //更新xml文档
doc.Save("../../students.xml");
//刷新listview
FillListView();
MessageBox.Show("修改成功");
return;
}
}
MessageBox.Show("修改失败");
}
else
{
MessageBox.Show("请选中要修改的记录");
}
} //添加
private void button2_Click(object sender, EventArgs e)
{
//1.取出xml文件中最后一个学生的id
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
//通过计算字节点的个数来获取最后一个字节点对应的下标
//int index = root.ChildNodes.Count -1;
//string oldId = root.ChildNodes[index].Attributes["id"].Value;
//直接获取最后一个字节点的id
string oldId = root.LastChild.Attributes["id"].Value;
//2.得到新学生的id
string newId = Convert.ToString(int.Parse(oldId) + 1);
//3.创建各个节点和属性对象
//创建student节点对象
XmlElement student = doc.CreateElement("student");
//创建子节点对象
XmlNode name = doc.CreateElement("name");
XmlNode age = doc.CreateElement("age");
XmlNode address = doc.CreateElement("address");
//给字节点赋值
name.InnerText = txtName.Text;
age.InnerText = txtAge.Text;
address.InnerText = txtAddress.Text;
//创建id属性
XmlAttribute id = doc.CreateAttribute("id");
//给id属性赋值
id.Value = newId;
//建立节点之间的关系
student.AppendChild(name);
student.AppendChild(age);
student.AppendChild(address);
//将id属性添加到student节点下
student.Attributes.Append(id);
//将student节点追加到根节点下
root.AppendChild(student);
//保存
doc.Save("../../students.xml");
//刷新
FillListView();
MessageBox.Show("添加成功");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace T5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
FillListView();
}
private void FillListView()
{
List<Student> list = GetStudents();
//填充listview
foreach (Student s in list)
{
//创建listviewitem对象
ListViewItem item = new ListViewItem(s.Id);
//给item其它三个列添加数据
item.SubItems.AddRange(new string[] {s.Name,s.Age,s.Address });
//将item对象添加到listview的items集合中
lstStudents.Items.Add(item);
}
} private List<Student> GetStudents()
{
//清空listview
lstStudents.Items.Clear();
//创建学生集合
List<Student> list = new List<Student>();
//创建xml文档对象
XmlDocument doc = new XmlDocument();
//加载文档内容
doc.Load("../../students.xml");
//获取根节点
//XmlNode root = doc.DocumentElement;
XmlNode root = doc.SelectSingleNode("students");
foreach (XmlNode stu in root.ChildNodes)
{
Student s = new Student();
//获取id属性的值
s.Id = stu.Attributes["id"].Value;
//不用循环的做法
s.Name = stu.ChildNodes[0].InnerText;
s.Age = stu.ChildNodes[1].InnerText;
s.Address = stu.ChildNodes[2].InnerText;
//用循环的做法
//foreach (XmlNode node in stu.ChildNodes)
//{
// switch (node.Name)
// {
// case "name":
// s.Name = node.InnerText;
// break;
// case "age":
// s.Age = node.InnerText;
// break;
// case "address":
// s.Address = node.InnerText;
// break;
// }
//}
//将学生对象添加到集合中
list.Add(s);
}
//返回学生集合
return list;
} //删除
private void button4_Click(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode stu in root.ChildNodes)
{
if (stu.Attributes["id"].Value == lstStudents.SelectedItems[0].Text)
{
//删除选中的学生节点
root.RemoveChild(stu);
//更新xml文档
doc.Save("../../students.xml");
//刷新listview
FillListView();
MessageBox.Show("删除成功");
return;
}
}
MessageBox.Show("删除失败");
}
else
{
MessageBox.Show("请选中要删除的记录");
}
} private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
//将选中的记录各个字段的值显示到文本框中
txtId.Text = lstStudents.SelectedItems[0].Text;
txtName.Text = lstStudents.SelectedItems[0].SubItems[1].Text;
txtAge.Text = lstStudents.SelectedItems[0].SubItems[2].Text;
txtAddress.Text = lstStudents.SelectedItems[0].SubItems[3].Text;
}
} //修改
private void button3_Click(object sender, EventArgs e)
{
if (lstStudents.SelectedItems.Count == 1)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode stu in root.ChildNodes)
{
if (stu.Attributes["id"].Value == lstStudents.SelectedItems[0].Text)
{
//更新当前学生各个节点的innertext
stu.ChildNodes[0].InnerText = txtName.Text;
stu.ChildNodes[1].InnerText = txtAge.Text;
stu.ChildNodes[2].InnerText = txtAddress.Text; //更新xml文档
doc.Save("../../students.xml");
//刷新listview
FillListView();
MessageBox.Show("修改成功");
return;
}
}
MessageBox.Show("修改失败");
}
else
{
MessageBox.Show("请选中要修改的记录");
}
} //添加
private void button2_Click(object sender, EventArgs e)
{
//1.取出xml文件中最后一个学生的id
XmlDocument doc = new XmlDocument();
doc.Load("../../students.xml");
XmlNode root = doc.DocumentElement;
//通过计算字节点的个数来获取最后一个字节点对应的下标
//int index = root.ChildNodes.Count -1;
//string oldId = root.ChildNodes[index].Attributes["id"].Value;
//直接获取最后一个字节点的id
string oldId = root.LastChild.Attributes["id"].Value;
//2.得到新学生的id
string newId = Convert.ToString(int.Parse(oldId) + 1);
//3.创建各个节点和属性对象
//创建student节点对象
XmlElement student = doc.CreateElement("student");
//创建子节点对象
XmlNode name = doc.CreateElement("name");
XmlNode age = doc.CreateElement("age");
XmlNode address = doc.CreateElement("address");
//给字节点赋值
name.InnerText = txtName.Text;
age.InnerText = txtAge.Text;
address.InnerText = txtAddress.Text;
//创建id属性
XmlAttribute id = doc.CreateAttribute("id");
//给id属性赋值
id.Value = newId;
//建立节点之间的关系
student.AppendChild(name);
student.AppendChild(age);
student.AppendChild(address);
//将id属性添加到student节点下
student.Attributes.Append(id);
//将student节点追加到根节点下
root.AppendChild(student);
//保存
doc.Save("../../students.xml");
//刷新
FillListView();
MessageBox.Show("添加成功");
}
}
}
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
2013-05-11
展开全部
首先你是用DataGridView来显示数据的话就很简单了````同时你那数据库表里的ID最好设为标识列``不设也可以```我下面是ID为标识列的情况下的1,添加信息的方法:SqlConnection conn=new SqlConnection();cnn.ConnectionString="server=.;integrated security=true;database=yun";SqlCommand com=new SqlCommand(); try
{
com = new SqlCommand("insert into yun(Name,age) values('"+添加的Name的内容+"','"+添加的age的内容+"')", conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加资料成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}2.删除:SqlConnection conn=new SqlConnection();cnn.ConnectionString="server=.;integrated security=true;database=yun";SqlCommand com=new SqlCommand(); if (MessageBox.Show("确定要删除该系信息吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
com = new SqlCommand("delete from yun where ID=" + Convert.ToString(dgvDepartment[0, dgvDepartment.CurrentCell.RowIndex].Value).Trim(),conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery(); conn.Close();
MessageBox.Show("删除数据成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}3.修改:SqlConnection conn=new SqlConnection();cnn.ConnectionString="server=.;integrated security=true;database=yun";SqlCommand com=new SqlCommand(); try
{
com = new SqlCommand("update yun set Name ='"+修改的Name的内容+"',age='"+修改的age的内容+"'where ID=" + Convert.ToString(dgvDepartment[0,dgvDepartment.CurrentCell.RowIndex].Value).Trim(), conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("修改数据成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} 方法就上面这样```如果还是不懂的话再联系我吧````其实```很简单就用到Command类而已```添加就insert into....删除就delect....修改就update`````
{
com = new SqlCommand("insert into yun(Name,age) values('"+添加的Name的内容+"','"+添加的age的内容+"')", conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加资料成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}2.删除:SqlConnection conn=new SqlConnection();cnn.ConnectionString="server=.;integrated security=true;database=yun";SqlCommand com=new SqlCommand(); if (MessageBox.Show("确定要删除该系信息吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
com = new SqlCommand("delete from yun where ID=" + Convert.ToString(dgvDepartment[0, dgvDepartment.CurrentCell.RowIndex].Value).Trim(),conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery(); conn.Close();
MessageBox.Show("删除数据成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}3.修改:SqlConnection conn=new SqlConnection();cnn.ConnectionString="server=.;integrated security=true;database=yun";SqlCommand com=new SqlCommand(); try
{
com = new SqlCommand("update yun set Name ='"+修改的Name的内容+"',age='"+修改的age的内容+"'where ID=" + Convert.ToString(dgvDepartment[0,dgvDepartment.CurrentCell.RowIndex].Value).Trim(), conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
com.ExecuteNonQuery();
conn.Close();
MessageBox.Show("修改数据成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} 方法就上面这样```如果还是不懂的话再联系我吧````其实```很简单就用到Command类而已```添加就insert into....删除就delect....修改就update`````
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询