SQL: how can we read database version from a SQL database and does not modify it.

Answered SQL: how can we read database version from a SQL database and does not modify it.

  • 15 марта 2012 г. 1:44
     
      С кодом

    We need to read SQL server compact database database version (in one of table store database version). We use the following code. However, it modify the file date.

    string connnectionString = "Data Source=" + pathFile + ";Password=" + DatabasePassword + ";Persist Security Info=True";
            //connnectionString += ";File Mode='Read Only';";
            // get the database schema version
            using (SqlCeConnection con = new SqlCeConnection(connnectionString))
            {
              con.Open();
              using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM DatabaseVersion ORDER BY VersionID DESC", con))
              {
                SqlCeDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                  return string.Format("{0}.{1}", rdr.GetInt32(1), rdr.GetInt32(2));
              }
            }

    If we do not use "using" statement, and close the connection sepdicifically.
    we see the database file (~.sdf) date gets changed when calling con.Close();
    Is it normally behanvor that reading data from database will change database file date?
    If it it not, is there a way we can fix this problem? thx!


    SQLCeConnection.ConnectionString property

    http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/ede6c732-12ce-42af-b510-e3fd77c29d0e

    • Изменено JJChen 15 марта 2012 г. 18:50
    •  

Все ответы

  • 15 марта 2012 г. 17:53
    Модератор
     
     Отвечено

    You can open the database file in read-only mode, remember to specify a temp path as well.

    Ie:

    string connnectionString = "Data Source=" + pathFile + ";Password=" + DatabasePassword + ";File Mode=Read Only;Temp path=C:\temp";


    Please mark as answer, if this was it. Visit my SQL Server Compact blog

    • Помечено в качестве ответа JJChen 15 марта 2012 г. 18:37
    •