private void btnDelete_Click(object sender, EventArgs e) { using (SQLiteConnection cn = new SQLiteConnection("Data Source=Test.db3;Pooling=true;FailIfMissing=false")) { //在打开数据库时,会判断数据库是否存在,如果不存在,则在当前目录下创建一个 cn.Open(); using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = cn; //建立表,如果表已经存在,则报错 cmd.CommandText = "CREATE TABLE [test] (id int, name nvarchar(20))"; cmd.ExecuteNonQuery(); //插入测试数据 for (int i = 2; i < 5; i++) { cmd.CommandText = string.Format("INSERT INTO [test] VALUES ({0}, '中文测试')", i); cmd.ExecuteNonQuery(); } for (int i = 5; i < 10; i++) { cmd.CommandText = string.Format("INSERT INTO [test] VALUES ({0}, 'English Test')", i); cmd.ExecuteNonQuery(); } //读取数据 cmd.CommandText = "SELECT * FROM [test]"; using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while (dr.Read()) { Console.WriteLine("第{0} 条:{1}", dr.GetValue(0), dr.GetString(1)); } } } } User usr; if (this.cbUsers.Text == "") { return; } if (MessageBox.Show(this, "Do you want to delete the user ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { usr = UserList.FindByName(this.cbUsers.Text); if (usr != null) { UserList.Delete(usr.Id); } LoadUserList(); } }
private void delUserToolStripMenuItem1_Click(object sender, EventArgs e) { if (MessageBox.Show(this, "Do you want to delete the user ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { User usr; usr = UserList.FindByName(this.cbUsers.Text); if (usr != null) { UserList.Delete(usr.Id); } } }