How can I check whether a table exists in SQL Server CE 3.5
You can query the schema views in SQL CE 3.5 take a look here.
Here is a simple extension method that you could use.
public static class SqlCeExtentions
{
public static bool TableExists(this SqlCeConnection connection, string tableName)
{
if (tableName == null) throw new ArgumentNullException("tableName");
if (string.IsNullOrWhiteSpace(tableName)) throw new ArgumentException("Invalid table name");
if (connection == null) throw new ArgumentNullException("connection");
if (connection.State != ConnectionState.Open)
{
throw new InvalidOperationException("TableExists requires an open and available Connection. The connection's current state is " + connection.State);
}
using (SqlCeCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "SELECT 1 FROM Information_Schema.Tables WHERE TABLE_NAME = @tableName";
command.Parameters.AddWithValue("tableName", tableName);
object result = command.ExecuteScalar();
return result != null;
}
}
}
You can use the above as follows
using (SqlCeConnection connection = new SqlCeConnection(@"Data Source=MyDatabase1.sdf"))
{
connection.Open();
if (connection.TableExists("MyTable"))
{
// The table exists
}
else
{
// The table does not exist
}
}
http://msdn.microsoft.com/en-us/library/aa226134(v=sql.80).aspx
댓글 없음:
댓글 쓰기