rollback写在messagebox

本文介绍了一种在遇到SQL错误时进行回滚并提示用户的处理方法。通过在用户确认前执行回滚操作避免了数据库锁死的问题,并通过消息框通知用户具体的错误信息,包括SQL错误文本及邮件发送的状态。
//rollback写在messagebox前面,避免未按下‘确定’按钮时的数据库锁死
   rollback using sqlca;
   MessageBox("SQL error", SQLCA.SQLErrText+'~r~n'+'这封邮件'+"(采购订单号"+ls_cvouchid+")"+'发送成功,但是在数据库中标记失败!'+'~r~n'+'请检查数据库连接是否正确 或者 导入正确数据库。')
private bool DeleteUserAccount() { // 确保username不为空 if (string.IsNullOrWhiteSpace(username)) { MessageBox.Show("用户名无效,无法执行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); string query = "DELETE FROM T_User WHERE Uname = @Username"; using (SqlCommand cmd = new SqlCommand(query, conn)) { // 显式指定参数类型(重要!) cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show($"账号 {username} 已成功注销", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; // 返回成功状态 } else { MessageBox.Show($"未找到用户 {username}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } catch (SqlException sqlEx) { MessageBox.Show($"数据库错误: {sqlEx.Message}", "SQL错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ShowLoginForm() { // 创建新的登录窗体实例 Form1 loginForm = new Form1(); // 关闭当前窗体 this.Close(); // 显示登录窗体(在新的应用程序上下文中) loginForm.Show(); } private bool DeleteUserAccount() { // 确保username不为空 if (string.IsNullOrWhiteSpace(username)) { MessageBox.Show("用户名无效,无法执行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); string query = "DELETE FROM T_User WHERE Uname = @Username"; using (SqlCommand cmd = new SqlCommand(query, conn)) { // 显式指定参数类型(重要!) cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show($"账号 {username} 已成功注销", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; // 返回成功状态 } else { MessageBox.Show($"未找到用户 {username}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } catch (SqlException sqlEx) { MessageBox.Show($"数据库错误: {sqlEx.Message}", "SQL错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ShowLoginForm() { // 创建新的登录窗体实例 Form1 loginForm = new Form1(); // 关闭当前窗体 this.Close(); // 显示登录窗体(在新的应用程序上下文中) loginForm.Show(); }private bool DeleteUserAccount() { // 确保username不为空 if (string.IsNullOrWhiteSpace(username)) { MessageBox.Show("用户名无效,无法执行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); string query = "DELETE FROM T_User WHERE Uname = @Username"; using (SqlCommand cmd = new SqlCommand(query, conn)) { // 显式指定参数类型(重要!) cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show($"账号 {username} 已成功注销", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; // 返回成功状态 } else { MessageBox.Show($"未找到用户 {username}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } catch (SqlException sqlEx) { MessageBox.Show($"数据库错误: {sqlEx.Message}", "SQL错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ShowLoginForm() { // 创建新的登录窗体实例 Form1 loginForm = new Form1(); // 关闭当前窗体 this.Close(); // 显示登录窗体(在新的应用程序上下文中) loginForm.Show(); } private bool DeleteUserAccount()报错,怎么改
07-02
db = pymysql.connect(host="localhost", user="root", passwd="123456", db="room_db") if self.student_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="123456", db="room_db") cursor = db.cursor() sql = "SELECT * FROM 学生住宿信息表 WHERE 学号 = '%s'" % (self.student_id.get()) # SQL 插入语句 try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.id = '学号:' + row[0] self.name = '姓名:' + row[1] self.gender = '性别:' + row[2] self.age = '年龄:' + row[3] self.num ='电话:' + row[4] self.build = '公寓号:' + row[5] self.room = '宿舍号:' + row db.commit() label = tk.Label(self.window, text='学生信息查看', bg='SkyBlue', font=('楷体', 20), width=50, height=2) label.pack(pady=40) Label(self.window, text=self.id, font=('楷体', 17)).pack() Label(self.window, text=self.name, font=('楷体', 17)).pack() Label(self.window, text=self.gender, font=('楷体', 17)).pack() Label(self.window, text=self.age, font=('楷体', 17)).pack() Label(self.window, text=self.num, font=('楷体', 17)).pack() Label(self.window, text=self.build, font=('楷体', 17)).pack() Label(self.window, text=self.room, font=('楷体', 17)).pack() Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=120) self.window.protocol("WM_DELETE_WINDOW", self.back_1) self.window.mainloop() except: db.rollback() messagebox.showinfo('提示', '数据库连接失败!') db.close() else: messagebox.showinfo('提示', '请填学生信息!') 为什么运行就显示数据库连接失败
07-16
namespace _2 { public partial class Form2 : Form { private string username; private string connectionString = "Data Source=localhost;Initial Catalog=学习管理助手;Integrated Security=True;"; private OpenFileDialog openFileDialog1 = new OpenFileDialog(); private WebBrowser webBrowser1 = new WebBrowser(); // 统一使用带参构造函数 public Form2(string username) { InitializeComponent(); this.username = username; // 初始化控件 openFileDialog1.Filter = "所有文件 (*.*)|*.*"; webBrowser1.Dock = DockStyle.Fill; panel1.Controls.Add(webBrowser1); // 显示当前用户名(可选) this.Text = $"用户管理 - {username}"; } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e) { if (DialogResult.Yes == MessageBox.Show("确认退出吗", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { //退出 this.Close(); } } private void 注销账号ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult confirm = MessageBox.Show($"确定要永久删除账号 {username} 吗?", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (confirm == DialogResult.Yes) { DeleteUserAccount(); } } private void DeleteUserAccount() { // 确保username不为空 if (string.IsNullOrWhiteSpace(username)) { MessageBox.Show("用户名无效,无法执行操作", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); string query = "DELETE FROM T_User WHERE Uname = @Username"; using (SqlCommand cmd = new SqlCommand(query, conn)) { // 显式指定参数类型(重要!) cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = username; int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show($"账号 {username} 已成功注销", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } else { MessageBox.Show($"未找到用户 {username}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (SqlException sqlEx) { MessageBox.Show($"数据库错误: {sqlEx.Message}", "SQL错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"操作失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void 打开ToolStripMenuItem1_Click(object sender, EventArgs e) { // 显示打开文件对话框 if (openFileDialog1.ShowDialog() == DialogResult.OK) { // 获取选中的文件路径 string filePath = openFileDialog1.FileName; try { // 在 WebBrowser 控件中显示文件 webBrowser1.Navigate(filePath); } catch (Exception ex) { MessageBox.Show("打开文件时出错:" + ex.Message); } } } private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e) { FormUpdatePwd_User form = new FormUpdatePwd_User(); form.ShowDialog(); } private void panel1_Paint(object sender, PaintEventArgs e) { } private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { } } }注销后窗体跳回Form1
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值