我在下面的代码中的con.close()点上,在Visual Studio中收到“检测不到代码”消息。您能发现我做错了什么吗?

private int chek1(String insert)
{
    OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb");
    OleDbCommand com = new OleDbCommand("select count(*) from sn where sn='" + insert + "\'", con);
    con.Open();

    int po = (int)com.ExecuteScalar();
    if (po > 0)
        return 1;
    else
        return 0;
    con.Close();
}

最佳答案

您的代码可能像这样:

private int check(string sn)
{
    using (OleDbConnection connection = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=d:\\sdb.mdb"))
    using (OleDbCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT COUNT(*) FROM sn WHERE sn=?";
        command.Parameters.Add("@sn", sn));
        con.Open();
        return ((int)com.ExecuteScalar() > 0) ? 1 : 0;
    }
}

关于c# - 检测到无法访问的代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2898694/

10-09 14:27