none
Why is my SQLServer CE code failing?

    Question


  • 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:

    if (! TableExists("table42"))
    {
    	DBUtils.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 > 0;
    		}
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show("TableExists " + ex.Message);
    		return false;
    	}
    }
    
    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);
    		}
    	}
    }


    ...but I'm getting err msgs with that. Or, it's failing, anyway, but not telling me much*. I can't step through it, so I rely on those calls to MessageBox.Show(). 

    * I only see "TableExists ", then "CreateTable42 " (there's nothing in ex.Message in either case, although it's obviously throwing an exception).
    Wednesday, July 31, 2013 7:36 PM

Answers

All replies