如何使用c#确定SQL Server CE表中是否存在列?

本文关键字:是否 存在 CE 何使用 确定 SQL Server | 更新日期: 2023-09-27 18:06:29

遗留代码是这样做的:

public bool isValidField(string tableName, string fieldName)
{
    bool retVal;
    string tblQuery = string.Format("SELECT {0} FROM {1}", fieldName, tableName);
    checkConnection();
    try
    {
        SqlCeCommand cmd = objCon.CreateCommand();
        cmd.CommandText = tblQuery;
        object objvalid = cmd.ExecuteScalar();
        retVal = (null != objvalid);
    }
    catch
    {
        retVal = false; 
    }
    return retVal;
}

…但我发现它并不总是有效。在调用该方法并获得false后,一些代码更改表以添加一些列,但我得到:

一个列ID在规格中出现了不止一次。

我在StackOverflow上发现了一些有前途的SQL语句:

if exists(select * from sys.columns 
        where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))  

IF COL_LENGTH('table_name','column_name') IS NULL

…但是我不确定如何在c#/.NET 1.1中实现这个

我是否需要使用ExecuteScalar并将返回值转换为bool值?还是别的什么?

更新

把它改成这个并没有解决问题:

public bool isValidField(string tableName, string columnName)
{
    bool retVal;
    string tblQuery = string.Format(
        "COL_LENGTH({0},{1}) IS NULL",
        tableName,
        columnName);
    checkConnection();
    try
    {
        SqlCeCommand cmd = objCon.CreateCommand();
        cmd.CommandText = tblQuery;
        object objvalid = cmd.ExecuteScalar();
        retVal = Convert.ToBoolean(objvalid);
    }
    catch
    {
        retVal = false; 
    }
    return retVal;
}

更新2

奇怪的是,我在新代码中看到了更多这样的错误消息。

3

更新

当我这样修改代码时没有什么区别:

string tblQuery = "SELECT COUNT(*) 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);
    int i = (int)cmd.ExecuteScalar();
    retVal = i > 0;
}

…所以我不知道哪种方式更好;这对我来说似乎更直接…有什么想法吗?

如何使用c#确定SQL Server CE表中是否存在列?

您可以直接查询信息模式表来获取您想要的信息:

public bool isValidField(string tableName, string columnName)
{
    var tblQuery = "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS"
                   + " WHERE TABLE_NAME = @tableName AND"
                   + " COLUMN_NAME = @columnName";
    SqlCeCommand cmd = objCon.CreateCommand();
    cmd.CommandText = tblQuery;
    var tblNameParam = new SqlCeParameter(
        "@tableName",
        SqlDbType.NVarChar,
        128);
    tblNameParam.Value = tableName
    cmd.Parameters.Add(tblNameParam);
    var colNameParam = new SqlCeParameter(
        "@columnName",
        SqlDbType.NVarChar,
        128);
    colNameParam.Value = columnName
    cmd.Parameters.Add(colNameParam);
    object objvalid = cmd.ExecuteScalar(); // will return 1 or null
    return objvalid != null;
}
    public bool IsValidField(SqlCeConnection objCon, string tableName, string columnName)
    {
        const string query = "SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS"
                                + " WHERE TABLE_NAME = @TABLENAME AND"
                                + " COLUMN_NAME = @COLUMNNAME";
        using (var cmd = new SqlCeCommand(query, objCon))
        {
            cmd.Parameters.Add("@TABLENAME", SqlDbType.NVarChar, 128).Value = tableName;
            cmd.Parameters.Add("@COLUMNNAME", SqlDbType.NVarChar, 128).Value = columnName;
            var objvalid = cmd.ExecuteScalar(); // will return 1 or null
            return objvalid != null;
        }
    }