插入后没有启动ConnectionString!

时间:2011-07-04 15:04:01

标签: c# asp.net

我遇到了这个小问题:每次我的代码执行下面的InsertOrder()例程时,都会收到以下异常消息:“连接字符串尚未启动”。

这是InsertOrder()方法的代码:

  private void InsertOrder()
{
    string insertSQL = "";

    insertSQL += "INSERT INTO Orders (";
    insertSQL += "UserName, DateCreated, LastUpdate, Description, PaymentType, Delivery, Total) ";
    insertSQL += "VALUES (@UserName, @DateCreated, @LastUpdate, @Description, @PaymentType, @Delivery, @Total)";

    SqlCommand cmd0 = new SqlCommand(insertSQL, connection);

    // Adds the parameters
    cmd0.Parameters.AddWithValue("@UserName", Profile.UserName.ToString());
    cmd0.Parameters.AddWithValue("@DateCreated", Profile.Orders.SCart.DateCreated());
    cmd0.Parameters.AddWithValue("@LastUpdate", Profile.Orders.SCart.LastUpdate());
    cmd0.Parameters.AddWithValue("@Description", Profile.Orders.SCart.GetCartDescription());
    cmd0.Parameters.Add("@PaymentType", SqlDbType.Bit).Value = Profile.Orders.SCart.PaymentType;
    cmd0.Parameters.Add("@Delivery", SqlDbType.Bit).Value = Profile.Orders.SCart.Delivery;
    cmd0.Parameters.AddWithValue("@Total", Profile.Orders.SCart.Total);

    try
    {
        using (connection)
        {
            connection.Open();
            cmd0.ExecuteNonQuery();
        }

        string selectSQL = "SELECT OrderID FROM Orders WHERE UserName=@UserName AND DateCreated=@DateCreated";
        SqlCommand cmd1 = new SqlCommand(selectSQL, connection);

        cmd1.Parameters.AddWithValue("@UserName", Profile.UserName);
        cmd1.Parameters.AddWithValue("@DateCreated", Profile.Orders.SCart.DateCreated());
        SqlDataReader reader;

        using (connection)
        {
            connection.Open();
            reader = cmd1.ExecuteReader();
            reader.Read();
            OrderID = (int)reader["OrderID"];
            reader.Close();
        }

        // Store registered customer information for later usage..
        if (!Profile.IsAnonymous)
        {
            string insertSQL2 = "";

            insertSQL2 += "INSERT INTO CategoriesInAnOrder (";
            insertSQL2 += "OrderID, CategoryID, Quantity) VALUES (@OrderID, @CategoryID, @Quantity)";

            foreach (CartItem item in Profile.Orders.SCart.Items)
            {
                SqlCommand cmd2 = new SqlCommand(insertSQL2, connection);
                cmd2.Parameters.AddWithValue("@OrderID", OrderID);
                cmd2.Parameters.AddWithValue("@CategoryID", item.CategoryID);
                cmd2.Parameters.AddWithValue("@Quantity", item.Quantity);
                using (connection)
                {
                    connection.Open();
                    cmd2.ExecuteNonQuery();
                }
            }
        }
    }
    catch (Exception err)
    {
        pnlWizard.Visible = false;
        lblError.Text = err.Message;
    }
}  

值得一提的是,我已经在我的SuperClass中放置了一个SqlConnection对象,因此每个子类(如包含上述方法的子类)都会继承此属性。

共享SqlConnection对象设置如下:

    protected SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);

对不起我的英文...我是意大利人

2 个答案:

答案 0 :(得分:3)

我认为

using(connection)

可能是您问题的根源。

当调用完成时,using()将导致在连接上调用Dispose(),但是你的连接只被实例化一次。

下次你来使用它时,它不会处于健康状态!

每次都考虑创建一个新连接:

protected SqlConnection connection 
{
    get
    {
        return new SqlConnection
          (System.Web.Configuration.WebConfigurationManager.ConnectionStrings 
          ["DefaultConnectionString"].ConnectionString); 
    }
}

答案 1 :(得分:3)

我怀疑问题是你是using字段的值。这将工作一次,但在Dispose() 之后,您注定了该连接

对于这种情况,我使用自定义EnsureOpen()方法,如果它尚未打开则返回IDisposable;用法:

using(connection.EnsureOpen())
{
    // use it
}

这样,连接不会过早地处理,而是在需要时打开。无论它是否已经打开,它都能正常工作。

如果你愿意,我可以稍后分享EnsureOpen。​​