【问题标题】:Update table in firebird stored procedure called from c#从 c# 调用的 firebird 存储过程中的更新表
【发布时间】:2018-06-11 23:01:53
【问题描述】:

我在 firebird 中有这个存储过程:

create or alter procedure "LKS_CambiaEstadoAgenda" (
    "param_id" integer)
as
begin
    update
        "Agenda"
    set
        "Agenda".estatus = 2
    where
        "Agenda"."idAgenda" = :"param_id";
end

还有这个 C# 代码:

public bool cambioEstadoAgenda(int idAgenda)
{
        ConectarBD();

        try
        {
            comando.CommandType = CommandType.StoredProcedure;
            comando.CommandText = "\"LKS_CambiaEstadoAgenda\"";

            comando.Parameters.Clear();
            comando.Parameters.AddWithValue("@param_id", idAgenda);

            comando.Connection = conexion;

            if (Convert.ToInt32(comando.ExecuteScalar()) > 0)
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            MessageBox.Show("ups. un error\n" + ex.Message);
            return false;
        }
        finally
        {
            comando.Parameters.Clear();
            DesconectarBD();
        }
}

当我执行它时,它不会在数据库中进行更改。我的代码有什么错误或者你能给我什么建议?

【问题讨论】:

    标签: c# stored-procedures firebird


    【解决方案1】:

    您需要提交事务。例子可以看here under 2. point (UPDATE)

    2. Update a text blob field
    
    public static void Main(string[] args)
    {
    // Set the ServerType to 1 for connect to the embedded server
    string connectionString =
    "User=SYSDBA;" +
    "Password=masterkey;" +
    "Database=SampleDatabase.fdb;" +
    "DataSource=localhost;" +
    "Port=3050;" +
    "Dialect=3;" +
    "Charset=NONE;" +
    "Role=;" +
    "Connection lifetime=15;" +
    "Pooling=true;" +
    "Packet Size=8192;" +
    "ServerType=0";
    
    FbConnection myConnection = new FbConnection(connectionString);
    myConnection.Open();
    
    FbTransaction myTransaction = myConnection.BeginTransaction();
    
    FbCommand myCommand = new FbCommand();
    
    myCommand.CommandText =
    "UPDATE TEST_TABLE_01 SET CLOB_FIELD = @CLOB_FIELD WHERE INT_FIELD = @INT_FIELD";
    myCommand.Connection = myConnection;
    myCommand.Transaction = myTransaction;
    
    myCommand.Parameters.Add("@INT_FIELD", FbType.Integer, "INT_FIELD");
    myCommand.Parameters.Add("@CLOB_FIELD", FbType.Text, "CLOB_FIELD");
    
    myCommand.Parameters[0].Value = 1;
    myCommand.Parameters[1].Value = GetFileContents(@"GDS.CS");
    
    // Execute Update
    myCommand.ExecuteNonQuery();
    
    // Commit changes
    myTransaction.Commit();
    
    // Free command resources in Firebird Server
    myCommand.Dispose();
    
    // Close connection
    myConnection.Close();
    }
    
    public static string GetFileContents(string fileName)
    {
      StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open));
      string contents = reader.ReadToEnd();
      reader.Close();
      return contents;
    } 
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多