0

I'm having an open OdbcConnection, I'm trying to write to a database using OdbcCommand and I would like to verify if the database i read-only before inserting data.

I can use try and catch, but I hope there is a better solution.

Thanks in advance.

OdbcConnection con = new OdbcConnection("DSN=SERVER1;UID=User;PWD=User");
try
{    
    con.Open();
    OdbcCommand cmd = con.CreateCommand();
    cmd.CommandText = "INSERT INTO table1 (NAME, AGE) VALUES ('Test', 100)";
    cmd.ExecuteNonQuery();
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}
finally
{
    con.Close()
}

Here's the output when the database is read-only:

ERROR [HY000] SOLID Database Error 10013: Transaction is read-only

Jegadeesh
  • 335
  • 2
  • 16
amkleist
  • 181
  • 1
  • 2
  • 11

1 Answers1

0

For sql server you can use : The information is stored in sys.databases.

SELECT name, is_read_only 
FROM sys.databases 
WHERE name = 'MyDBNAme'
GO

--returns 1 in is_read_only when database is set to read-only mode.

for oracle db you can use OPEN_MODE property to check.