前端之家收集整理的这篇文章主要介绍了
SQLite.Net使用入门(三)【增删改查】,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
生命之长短殊不重要,只要你活得快乐,在有生之年做些有意义的事,便已足够。
实体类:
public class clsModel
{
protected sqliteConnection conn;
public clsModel(sqliteConnection @conn)
{
this.conn = @conn;
if (this.conn != null &&
this.conn.State.ToString() == "Closed")
{
conn.Open();
}
}
}
public class clsStudents : clsModel
{
public clsStudents(sqliteConnection @conn) : base(@conn) { }
public DataTable getAllStudents()
{
DataTable dt = new DataTable();
sqliteCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM Students";
sqliteDataAdapter db = new sqliteDataAdapter(command);
db.Fill(dt);
return dt;
}
public DataTable getStudent(String @studentID)
{
DataTable dt = new DataTable();
sqliteCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM Students WHERE studentID = @studentID";
command.Parameters.Add(new sqliteParameter("@studentID",@studentID));
sqliteDataAdapter db = new sqliteDataAdapter(command);
db.Fill(dt);
return dt;
}
public void insertStudent(String @firstName,String @lastName)
{
sqliteCommand command = conn.CreateCommand();
command.CommandText = "INSERT into Students (firstName,lastName) VALUES(@firstName,@lastName)";
command.Parameters.Add(new sqliteParameter("@firstName",@firstName));
command.Parameters.Add(new sqliteParameter("@lastName",@lastName));
command.ExecuteNonQuery();
}
public void updateStudent(String @studentID,String @firstName,String @lastName)
{
sqliteCommand command = conn.CreateCommand();
command.CommandText = "UPDATE Students SET firstName = @firstName,lastName = @lastName WHERE studentID = @studentID";
command.Parameters.Add(new sqliteParameter("@studentID",@studentID));
command.Parameters.Add(new sqliteParameter("@firstName",@lastName));
command.ExecuteNonQuery();
}
public void deleteStudent(String @studentID)
{
sqliteCommand command = conn.CreateCommand();
command.CommandText = "DELETE FROM Students WHERE studentID = @studentID";
command.Parameters.Add(new sqliteParameter("@studentID",@studentID));
command.ExecuteNonQuery();
}
}
ublic partial class Form1 : Form
{
sqliteConnection conn;
clsStudents students;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
conn = new sqliteConnection(@"data source = ..\\..\\testdb.db");
students = new clsStudents(conn);
dataGridView1.DataSource = students.getAllStudents();
}
private void btnAdd_Click(object sender,EventArgs e)
{
new Form2(students).ShowDialog();
loadGridView();
}
private void btnEdit_Click(object sender,EventArgs e)
{
int index = dataGridView1.CurrentCell.RowIndex;
if (index > -1)
{
String studentID = dataGridView1.Rows[index].Cells["studentID"].Value.ToString();
new Form2(students,studentID).ShowDialog();
loadGridView();
}
}
private void btnDelete_Click(object sender,EventArgs e)
{
int index = dataGridView1.CurrentCell.RowIndex;
if (index > -1)
{
String studentID = dataGridView1.Rows[index].Cells["studentID"].Value.ToString();
var results = MessageBox.Show("你确定要删除此记录?","删除学生",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (results == DialogResult.Yes)
{
students.deleteStudent(studentID);
loadGridView();
}
}
}
private void loadGridView()
{
dataGridView1.DataSource = students.getAllStudents();
}
private void btnExit_Click(object sender,EventArgs e)
{
this.Close();
}
}
public partial class Form2 : Form
{
clsStudents students;
String studentID = "";
public Form2(clsStudents @students)
{
init(@students);
lblStudentID.Text = "新的学生实体";
}
public Form2(clsStudents @students,String studentID)
{
init(@students);
this.studentID = studentID;
lblStudentID.Text = this.studentID;
DataTable dt = students.getStudent(studentID);
txtFirstName.Text = dt.Rows[0]["firstName"].ToString();
txtLastName.Text = dt.Rows[0]["lastName"].ToString();
}
private void init(clsStudents students)
{
InitializeComponent();
this.students = students;
}
private void btnOK_Click(object sender,EventArgs e)
{
if (studentID == String.Empty)
{
students.insertStudent(txtFirstName.Text.Trim(),txtLastName.Text.Trim());
}
else
{
students.updateStudent(this.studentID,txtFirstName.Text.Trim(),txtLastName.Text.Trim());
}
this.Close();
}
private void btnCancel_Click(object sender,EventArgs e)
{
this.Close();
}
}
运行结果如图:
编辑