从c#调用的firebird存储过程中的更新表

时间:2018-06-11 23:01:54

标签: c# stored-procedures firebird

我在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();
        }
}

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

1 个答案:

答案 0 :(得分: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;
}