【问题标题】:Posting a message box when database records are added, or when an exception is generated添加数据库记录或生成异常时发布消息框
【发布时间】:2018-06-08 23:39:05
【问题描述】:

对不起,如果这是一个愚蠢的问题,但你能帮我添加一些代码到我的应用程序中,当有人成功添加到表中时生成一个 MessageBox,并且每当生成一个阻止所需信息的异常时生成另一个 MessageBox被添加到表中?我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace WindowsFormsApp1
{
public partial class AddJob : Form
{
    public AddJob()
    {
        InitializeComponent();
    }

    private void AddJob_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;
        con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\richard.schade\Desktop\IKJobs\WindowsFormsApp1\ikData.mdf;Integrated Security=True");
        con.Open();
        cmd = new SqlCommand("INSERT INTO openJobs (jobTitle, jobDescription, dateOpened, jobsiteLink, jobLocation) VALUES (@jobTitle, @jobDescription, @dateOpened, @jobsiteLink, @jobLocation)", con);
        cmd.Parameters.AddWithValue("@jobTitle", textBox1.Text);
        cmd.Parameters.AddWithValue("@jobDescription", textBox5.Text);
        cmd.Parameters.AddWithValue("@dateOpened", textBox4.Text);
        cmd.Parameters.AddWithValue("@jobsiteLink", textBox3.Text);
        cmd.Parameters.AddWithValue("@jobLocation", textBox2.Text);
        cmd.ExecuteNonQuery();


    }
}
}

感谢您的帮助,我已经从这个论坛学到了很多东西。

【问题讨论】:

标签: c# sql winforms


【解决方案1】:

将你方法的内容包裹在try/catch

在 catch 中执行MessageBox.Show(exception msg)

并在最后一行尝试MessageBox.Show(successfully done)

例如

Try{

SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;
        con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\richard.schade\Desktop\IKJobs\WindowsFormsApp1\ikData.mdf;Integrated Security=True");
        con.Open();
        cmd = new SqlCommand("INSERT INTO openJobs (jobTitle, jobDescription, dateOpened, jobsiteLink, jobLocation) VALUES (@jobTitle, @jobDescription, @dateOpened, @jobsiteLink, @jobLocation)", con);
        cmd.Parameters.AddWithValue("@jobTitle", textBox1.Text);
        cmd.Parameters.AddWithValue("@jobDescription", textBox5.Text);
        cmd.Parameters.AddWithValue("@dateOpened", textBox4.Text);
        cmd.Parameters.AddWithValue("@jobsiteLink", textBox3.Text);
        cmd.Parameters.AddWithValue("@jobLocation", textBox2.Text);
        cmd.ExecuteNonQuery();
MessageBox.Show("done") ;

} 
Catch(Exception ezx) {

MessageBox.Show("bad");

} 

【讨论】:

  • trycatch不是TryCatch(第一个字母是小写的)。
  • 是的。小写。如果答案有帮助,请将其标记为将来有相同问题的任何人的答案
  • 我不得不添加几个大括号,但您的解决方案有效。感谢两位抽出宝贵时间!
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多