Microsoft SQL Serverにテーブルが存在するかどうかを確認しようとしていますが、どういうわけか、使用する関数は常にテーブルが存在しないことを返し、出力は例外がスローされたことを指定していません。
これは、テーブルを作成する前(予想される)と、テーブルを作成した後(予想されない)の両方で発生します。
これは私が使用している関数です:
/// <summary>
/// Checks if a certain Sql Server Table exists
/// </summary>
/// <param name="_databasename">The name of the database</param>
/// <param name="_password">The password of the database</param>
/// <param name="_tablename">The name of the table to check</param>
/// <returns>
/// 'true' if table exists
/// 'false' if table not exists or if an exception was thrown
/// </returns>
public Boolean TableExists(String _databasename, String _password, String _tablename)
{
if (!_databasename.Contains(".sdf")) { _databasename = _databasename + ".sdf"; }
try
{
String connectionString = "DataSource=" + _databasename + "; Password=" + _password;
SqlCeConnection conn = new SqlCeConnection(connectionString);
if (conn.State==ConnectionState.Closed) { conn.Open(); }
using (SqlCeCommand command = conn.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM Information_Schema.Tables WHERE TABLE_NAME = '" + _tablename + "'";
Int32 count = Convert.ToInt32(command.ExecuteScalar());
if (count == 0)
{
Debug.WriteLine("Table " + _tablename + " does not exist.");
return false;
}
else
{
Debug.WriteLine("Table " + _tablename + " exists.");
return true;
}
}
}
catch(Exception _ex)
{
Debug.WriteLine("Failed to determine if table " + _tablename + " exists: " + _ex.Message);
return false;
}
}
ここに欠けているものがあるのは明らかですが、それが何なのかわからないようです。