如何使用ASP.NET处理插入代码中的异常?

时间:2014-03-12 15:03:07

标签: asp.net sql

我在SQL Server Management Express中有一个名为" Table_1"的表。我已成功将我的ASP程序与数据库连接。我编写了以下编码用于在数据库中插入数据。

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    namespace k2
    {
    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection conn = new SqlConnection("Data Source=ARTHY/SQLEXPRESS;Initial                  Catalog=k2;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {           
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    conn.Open();          
    }
    catch
    {
    SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text +    "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "'),conn");
    hi.ExecuteNonQuery();
    conn.Close();
    }    
    }
    }
    } 

当我尝试插入任何数据时,我得到如下例外:

    ExecuteNonQuery: Connection property has not been initialized.
    Description: An unhandled exception occurred during the execution of the current web     request. Please review the stack trace for more information about the error and where it  originated in the code. 
    Exception Details: System.InvalidOperationException: ExecuteNonQuery: Connection  property has not been initialized.
    Source Error: 
    Line 34:             {
    Line 35:                 SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" +  TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "'),conn");
    Line 36:                 hi.ExecuteNonQuery();
    Line 37:                 conn.Close();
    Line 38:             }

请帮我澄清一下例外情况。

2 个答案:

答案 0 :(得分:0)

您的conn.Open()行位于try{}块中,而其余代码位于catch{}块中。因此,conn.Open()行会抛出异常,触发现在没有catch{}引用的conn。移动你的代码:

try
{
  conn.Open();          
  SqlCommand hi = new SqlCommand("insert into Table_1 values('" + TextBox1.Text +    "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", conn);
  hi.ExecuteNonQuery();
  conn.Close();
}
catch
{
  //do something else if there is an exception
}

答案 1 :(得分:0)

除了Matt的回答,我建议将连接包装到using语句中以确保连接始终正确关闭。这是我的实现(重构一点)

try
{
    using (var connection = new SqlConnection("Data Source=ARTHY/SQLEXPRESS;Initial Catalog=k2;Integrated Security=True"))
    {
        connection.Open();
        var commandText = string.Format("insert into Table_1 values('{0}','{1}','{2}','{3}','{4}','{5}')",
                                        TextBox1.Text,
                                        TextBox2.Text,
                                        TextBox3.Text,
                                        TextBox4.Text,
                                        TextBox5.Text,
                                        TextBox6.Text);

        var command = new SqlCommand(commandText, connection);
        command.ExecuteNonQuery();
    }
}
catch (Exception exception)
{
    // do something with exception
}