为什么我会收到“在规范中出现过多次列ID”?

时间:2013-03-29 15:48:09

标签: c# sql compact-framework sql-server-ce windows-ce

我有代码创建了一个表,如果它不存在并添加了所有必需的列,但是对于用户具有旧版本表的情况,它会添加一些新的

列。然而,当第二个条件成立并且添加列的DDL运行时,我得到了"列ID在规范中出现了多次#34;

这是代码,以及用于确定表和列存在的辅助函数:

    bool tableExists = dbconn.isValidTable(tablename) != -1;
    if (!tableExists) 
    {
        ddl = "CREATE TABLE Bla (. . . salvationID nvarchar(19), salvation float, discount float)";
        dbconn.DBCommand(ddl, false);
    }
    else // (the table does exist) 
    {
        if(!dbconn.isValidField(tablename,"redemptionID"))
        {
            ddl = string.Format("ALTER TABLE {0} ADD redemptionID nvarchar(19) ", tablename);
            dbconn.DBCommand(ddl,false);
            . . .

        public int isValidTable(string tableName)
        {
            int validTable = -1;
            string tblQuery = string.Format("SELECT COUNT(*) FROM {0}", tableName);
            checkConnection();
            try
            {
                SqlCeCommand cmd = objCon.CreateCommand();
                cmd.CommandText = tblQuery;
                object objcnt = cmd.ExecuteScalar();
                validTable = Int32.Parse(objcnt.ToString());
            }
            catch
            {
                validTable = -1;
            }
            return validTable;
        }

//This has been beautified/elegantized thanks to p.s.w.g at http://stackoverflow.com/questions/15693639/how-can-i-determine-whether-a-column-exists-in-a-sql-server-ce-table-with-c
        public bool isValidField(string tableName, string columnName)
        {
            bool retVal;
            string tblQuery = "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName AND 

COLUMN_NAME = @columnName";
            checkConnection();
            try
            {
                SqlCeCommand cmd = objCon.CreateCommand();
                cmd.CommandText = tblQuery;
                SqlCeParameter tblNameParam = new SqlCeParameter("@tableName", SqlDbType.NVarChar, 128);
                tblNameParam.Value = tableName;
                cmd.Parameters.Add(tblNameParam);
                SqlCeParameter colNameParam = new SqlCeParameter("@columnName", SqlDbType.NVarChar, 128);
                colNameParam.Value = tableName;
                cmd.Parameters.Add(colNameParam);
                object objvalid = cmd.ExecuteScalar();
                retVal = !Convert.IsDBNull(objvalid);
            }
            catch
            {
                retVal = false; 
            }
            return retVal;
        }

2 个答案:

答案 0 :(得分:1)

尝试检查DBNull.Value

try
{
  SqlCeCommand cmd = objCon.CreateCommand();
  cmd.CommandText = tblQuery;
  object objcnt = cmd.ExecuteScalar();
  if ((objcnt != null) && (objcnt != DBNull.Value)) {
    validTable = Int32.Parse(objcnt.ToString());
  } else {
    MessageBox.Show("NULL returned from CreateCommand. Remove this line.");
  }
}
catch

答案 1 :(得分:1)

我怀疑正在发生的事情是isValidField()正在抛出一个例外而你的捕获只是吞下它而说明该字段不存在......当它可能存在时。

我强烈建议您不要只是吞下它,而是实际显示消息,以便您知道发生了什么。

例如:

public bool isValidField(string tableName, string columnName)
{
    bool retVal;
    string tblQuery = "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tableName AND COLUMN_NAME = @columnName";
    checkConnection();
    try
    {
        SqlCeCommand cmd = objCon.CreateCommand();
        cmd.CommandText = tblQuery;
        SqlCeParameter tblNameParam = new SqlCeParameter("@tableName", SqlDbType.NVarChar, 128);
        tblNameParam.Value = tableName;
        cmd.Parameters.Add(tblNameParam);
        SqlCeParameter colNameParam = new SqlCeParameter("@columnName", SqlDbType.NVarChar, 128);
        colNameParam.Value = tableName;
        cmd.Parameters.Add(colNameParam);
        object objvalid = cmd.ExecuteScalar();
        retVal = !Convert.IsDBNull(objvalid);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
        retVal = false; // <-- wrong answer
    }
    return retVal;
}

此外,该函数不应该是布尔值。你有三个条件:1。它存在;它不存在; 3.发生错误。

如果出现错误,您不希望后来的方法认为它无法找到它。此外,我对您验证表存在的位置做同样的事情。