Sqlexception未得到处理 [英] Sqlexception was unhandled

查看:57
本文介绍了Sqlexception未得到处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
     {
         SqlConnection con = new SqlConnection("data source=SAMIRAN-PC; Initial catalog = DB2; Integrated security= true");
         SqlDataAdapter dat = new SqlDataAdapter();
         dat.InsertCommand = new SqlCommand(" INSERT INTO TAB2 VALUES(@NAME,@AGE)", con);
         dat.InsertCommand.Parameters.Add("@NAME",SqlDbType.VarChar).Value = textBox1.Text;
         dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.Text;
         con.Open();
         dat.InsertCommand.ExecuteNonQuery();
         con.Close();
     }
 }



执行后,在con.Open();处显示错误通过说SqlException未处理。然后它在运行期间停止



我尝试过:



执行表单后,需要输入。但点击提交按钮后,它会停止一段时间并说出来。

请帮帮我.. !!!


After executing it, shows error at "con.Open();" by saying SqlException was unhandled. Then it stops during runtime

What I have tried:

After executing the form, it takes input. But after clicking submit button , it stops for a while and says such.
Please Help Me .. !!!

推荐答案

包裹代码在

Wrap the code in a
try
{
    // your code here
}
catch(SqlException ex)
{
    // do something useful with the error Number
    MessageBox.Show(ex.Number.ToString());
    foreach(SqlError s in ex.Errors)
    {
        // do something useful with its properties
        MessageBox.Show(s.ToString());
    }
}


这篇关于Sqlexception未得到处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆