title: Why is my SQL Server CE code failing?
body: <p>In my WindowsCE / Compact Framework (.NET1.1) project, I need to create a new table in code. I thought I could do it this way:</p>
  
  <pre><code>if (! TableExists("table42"))
  {
      CreateTable42();
  }
  
  public static bool TableExists(string tableName)
  {
      try
      {
          using (SqlCeConnection sqlConn = new SqlCeConnection(@"Data Source=\my documents\Platypus.SDF"))
          {
              sqlConn.Open();
              string qryStr = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?";
              SqlCeCommand cmd = new SqlCeCommand(qryStr, sqlConn);
              cmd.Parameters[0].Value = tableName;
              cmd.CommandType = CommandType.Text;
              int retCount = (int)cmd.ExecuteScalar();
              return retCount &gt; 0;
          }
      }
      catch (Exception ex)
      {
          MessageBox.Show("TableExists ex.Message == " + ex.Message);
          MessageBox.Show("TableExists ex.ToString() == " + ex.ToString());
          MessageBox.Show("TableExists ex.GetBaseException() == " + ex.GetBaseException());
          return false;
      }
  }
  </code></pre>
  
  <p>...but the call to TableExists() fails; and shows me:</p>
  
  <pre><code>TableExists ex.Message ==
  TableExists ex.ToString() == System.Data.SqlServerCe.SqlCeException at System.Data.SqlServerCe.SqlConnection.ProcessResults(Int32 hr) at ...at Open(boolean silent) ...
  TableExists ex.GetBaseException() == [same as ex.ToString() above]
  </code></pre>
  
  <p>"Int32 hr" ... ??? What the Hec Ramsey is that?</p>
  
  <p>As documented previously in these environs, I can't step through this projct, so I rely on those calls to MessageBox.Show(). </p>
  
  <p>The rest of the related code, if it may be of interest, is:</p>
  
  <pre><code>public static void CreateTable42()
  {
      try
      {
          using (SqlCeConnection con = new SqlCeConnection(@"Data Source=\my documents\Platypus.SDF"))
          {
              con.Open();
              using (SqlCeCommand com =  new SqlCeCommand(
  
  
  "create table table42 (setting_id INT IDENTITY NOT NULL PRIMARY KEY,  setting_name varchar(40) not null, setting_value(63) varchar not null)", con))
                  {
                      com.ExecuteNonQuery();
                      WriteSettingsVal("table42settingname","table42settingval");
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("CreateTable42 " + ex.Message);
          }
      }
  
  public static void WriteSettingsVal(string settingName, string settingVal)
  {
      using (SqlCeConnection sqlConn = new SqlCeConnection(@"Data Source=\my documents\Platypus.SDF"))
      {
          sqlConn.Open();
          string dmlStr = "insert into tabld42 (setting_name, setting_value) values(?, ?)";
          SqlCeCommand cmd = new SqlCeCommand(dmlStr, sqlConn);
          cmd.CommandType = CommandType.Text; 
          cmd.Parameters[0].Value = settingName;
          cmd.Parameters[1].Value = settingVal;
          try
          {
              cmd.ExecuteNonQuery();
          }
          catch (Exception ex)
          {
              MessageBox.Show("WriteSettingsVal " + ex.Message);
          }
      }
  }
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>Answer to Brad Rem's comment:</p>
  
  <p>I don't think it's necessary to encase the param in quotes, as other working code is like:</p>
  
  <pre><code>cmd.Parameters.Add("@account_id", Dept.AccountID);
  </code></pre>
  
  <p>-and:</p>
  
  <pre><code>cmd.Parameters[0].Value = Dept.AccountID;
  </code></pre>
  
  <p>(it does it one way the first time when in a loop, and the other way thereafter (don't ask me why).</p>
  
  <p>Anyway, just for grins, I did change the TableExists() parameter code from this:</p>
  
  <pre><code>cmd.Parameters[0].Value = tableName;
  </code></pre>
  
  <p>...to this:</p>
  
  <pre><code>cmd.Parameters.Add("@TABLE_NAME", tableName);
  </code></pre>
  
  <p>...but I still get the exact same result.</p>
  
  <h2>UPDATE 2</h2>
  
  <p>Here (<a href="http://msdn.microsoft.com/en-us/library/aa237891(v=SQL.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa237891(v=SQL.80).aspx</a>) I found this: "Caution  You must specify the SQL Server CE provider string when you open a SQL Server CE database."</p>
  
  <p>They give this example: </p>
  
  <pre><code>cn.ConnectionString = "Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0; data source=\Northwind.sdf"
  </code></pre>
  
  <p>I'm not doing that; my conn str is:</p>
  
  <pre><code>using (SqlCeConnection sqlConn = new SqlCeConnection(@"Data Source=\my documents\CCRDB.SDF"))
  </code></pre>
  
  <p>Could that be my problem?</p>
  
  <h2>UPDATE 3</h2>
  
  <p>I took this gent's advice (<a href="http://www.codeproject.com/Answers/629613/Why-is-my-SQLServer-CE-code-failing?cmt=487657#answer1" rel="nofollow">http://www.codeproject.com/Answers/629613/Why-is-my-SQLServer-CE-code-failing?cmt=487657#answer1</a>) and added a catch for SqlCeExcpetions so that it is now:</p>
  
  <pre><code>public static bool TableExists(string tableName)
  {
      try
      {
          using (SqlCeConnection sqlConn = new SqlCeConnection(@"Data Source=\my documents\CCRDB.SDF"))
          {
              sqlConn.Open();
              string qryStr = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @TABLE_NAME";
              SqlCeCommand cmd = new SqlCeCommand(qryStr, sqlConn);
              cmd.Parameters.Add("@TABLE_NAME", tableName);
              cmd.CommandType = CommandType.Text;
              int retCount = (int)cmd.ExecuteScalar();
              return retCount &gt; 0;
          }
      }
      catch (SqlCeException sqlceex)
      {
          MessageBox.Show("TableExists sqlceex.Message == " + sqlceex.Message);
          MessageBox.Show("TableExists sqlceex.ToString() == " + sqlceex.ToString());
          return false;
          . . .
  </code></pre>
  
  <p>The SqlCeException message is: "There is a file sharing violation. A different process might be using the file [,,,,,]" then "...processresults ... open ... getinstance ..."</p>
  
  <h2>UPDATE 4</h2>
  
  <p>Trying to use ctacke's sample code, but: Is Transaction absolutely necessary? I had to change the code to the following for my scenario/milieu, and don't know what Transaction should be or how to build it:</p>
  
  <pre><code>public static bool TableExists(string tableName)
  {
      string sql = string.Format("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{0}'", tableName);
      try
      {
          using (SqlCeConnection sqlConn = new SqlCeConnection(@"Data Source=\my documents\HHSDB.SDF"))
          {
              SqlCeCommand command = new SqlCeCommand(sql, sqlConn);
              //command.Transaction = CurrentTransaction as SqlCeTransaction;
              command.Connection = sqlConn;
              command.CommandText = sql;
              int count = Convert.ToInt32(command.ExecuteScalar());
              return (count &gt; 0);
          }
      }
      catch (SqlCeException sqlceex)
      {
          MessageBox.Show("TableExists sqlceex.Message == " + sqlceex.Message);
          return false;
      }
  }
  </code></pre>
  
  <h2>UPDATE 5</h2>
  
  <p>With this code, the err msg I get is, "An err msg is available for this exception but cannot be displayed because these messages are optional and are not currently insallted on this device. Please install ... NETCFv35.Messages.EN.cab"</p>
  
  <h2>UPDATE 6</h2>
  
  <p>All too typically, this legacy, ancient-technology project is giving me headaches. It seems that only one connection is allowed to be open at a time, and the app opens one from the outset; so, I have to use that connection. However, it is a DBConnection, not a SqlCeConnection, so I can't use this code:</p>
  
  <pre><code>using (SqlCeCommand com =  new SqlCeCommand(
         "create table hhs_settings (setting_id int identity (1,1) Primary key,  setting_name varchar(40) not null, setting_value(63) varchar not null)", frmCentral.dbconn))
  {
      com.ExecuteNonQuery();
      WriteSettingsVal("beltprinter", "ZebraQL220");
  }
  </code></pre>
  
  <p>...because the already-open connection type passed as an arg to the SqlCeCommand constructor is DBCommand, not the expected/required SqlCeConneection.</p>
  
  <p>The tentacles of this code are far too wide and entrenched to rip out by the roots and refactor to make it more sensible: a single tentative step in the foothills causes a raging avalanche on Everest.</p>