无法访问存储过程

时间:2012-08-05 10:29:12

标签: sql-server tsql stored-procedures

无法执行存储过程。任何人都可以告诉我,并指出我的愚蠢错误?

我收到的错误消息是

  

无效的操作。连接已关闭

代码:

public void Update(RepliesBAL RPBAL)
{
    using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa"))
    {
        SqlCommand command = new SqlCommand ();
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "dbo.fax_UpdateFaxReply";
        command.Parameters.Add("@uid", SqlDbType.VarChar, 50).Value = RPBAL.UlyssesID ;

        SqlTransaction transaction;

        transaction = connection.BeginTransaction("SampleTransaction");

        command.Connection = connection;
        command.Transaction = transaction;

        try
        {
            connection.Open();
            command.ExecuteNonQuery();
            Console.WriteLine("OK");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex2)
            {
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
                throw new Exception(ex.Message);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

要拨打.BeginTransaction(),您的连接需要打开 - 所以请将代码更改为:

using (SqlConnection connection = new SqlConnection(@"Data Source=19NNZP;Initial Catalog=ivr;Persist Security Info=True;User ID=sa;Password=sa"))
{
        // set up the SqlCommand 
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "dbo.fax_UpdateFaxReply";

        // SqlDbType should be *NVarChar* to exactly match the stored procedure parameter's type! 
        // Otherwise you'll have an implicit conversion happening....
        command.Parameters.Add("@uid", SqlDbType.NVarChar, 50).Value = RPBAL.UlyssesID ;

        SqlTransaction transaction;

        try
        {
            // open connection, start transaction
            connection.Open();

            transaction = connection.BeginTransaction("SampleTransaction");

            // assign transaction to SqlCommand and execute it  
            command.Transaction = transaction;
            command.ExecuteNonQuery();

            // if successful - commit the transaction!
            transaction.Commit();
            connection.Close();

            Console.WriteLine("OK");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);
            try
            {
                transaction.Rollback();
            }
            catch (Exception ex2)
            {
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);
                throw new Exception(ex.Message);
            }
        }
    }

在更改之后,希望这段代码可以正常工作。