为什么这种方法不仅不编译,而且抛出各种无意义的错误?

时间:2013-07-30 23:40:15

标签: c# visual-studio compact-framework windows-ce .net-1.1

此方法是Windows CE / CF / .NET1.1项目的一部分:

public void createSettingsTable()
{
    public string filename = "\\my documents\\CCRDB.SDF";
    string conStr = "Data Source = " + filename;
    try
    {
        using (SqlCeConnection con = new SqlCeConnection(conStr)
        {
            con.Open();
            using (SqlCeCommand com =  new SqlCeCommand("create table ccr_settings (setting_id INT IDENTITY NOT NULL PRIMARY KEY,  setting_name varchar(40) not null, setting_value(63) varchar not null)", con))
            {
                    com.ExecuteNonQuery();
            }
            con.Close();
        }
    }
    catch (Exception ex)
    {
        CCR.ExceptionHandler(ex, "createSettingsTable");
    }
}

......似乎被编译器视为完全陌生的东西。以下是粘贴它时导致的错误信息列表:

enter image description here

2 个答案:

答案 0 :(得分:6)

public从第3行中取出,并将父级添加到using的末尾:

public void createSettingsTable()
{
    string filename = "\\my documents\\CCRDB.SDF"; // <- Here
    string conStr = "Data Source = " + filename;
    try
    {
        using (SqlCeConnection con = new SqlCeConnection(conStr)) // <- Here
        {
            con.Open();
            using (SqlCeCommand com =  new SqlCeCommand("create table ccr_settings (setting_id INT IDENTITY NOT NULL PRIMARY KEY,  setting_name varchar(40) not null, setting_value(63) varchar not null)", con))
            {
                    com.ExecuteNonQuery();
            }
            //con.Close(); // this is not needed
        }
    }
    catch (Exception ex)
    {
        CCR.ExceptionHandler(ex, "createSettingsTable");
    }
}

您不需要con.Close来电。

答案 1 :(得分:3)

您在using声明结尾处错过了结束语。应该是这样的:

using (SqlCeConnection con = new SqlCeConnection(conStr)) {
  //...
}