当前上下文中不存在名称“_con” [英] The name '_con' does not exist in the current context

查看:197
本文介绍了当前上下文中不存在名称“_con”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASK当前上下文中不存在名称''_con''



我希望将条形码扫描器的数据与数据库sql server 2008同步..

我输入这样的代码..

ASK "The name ''_con'' does not exist in the current context"

I want to synchronize the data from barcode scanner with database sql server 2008 ..
and I input the code like this ..

public List<string> FindExistingValues()
        {
            List<string> results = new List<string>();

            string getStringsCmd = "Table_Mahasiswa1BindingSource FROM dbo.Mahasiswa";

            using (SqlConnection _con = new SqlConnection(""))
            using (SqlCommand _cmd = new SqlCommand(getStringsCmd, _con))

                _con.Open();

            using (SqlDataReader rdr =_con.ExecuteReader())
            {
                while (rdr.Read())
                {
                    results.Add(rdr.GetString(0));
                }
                rdr.Close();
            }

            _con.Close();
            {

            }

            return results;
        }

    }
}





,结果发现有错误

上的代码当前上下文中不存在名称''_con''

什么是解决方案。?



and it turns out there is an error code on
The name ''_con'' does not exist in the current context
what''s the solution.?

推荐答案

你真的需要使用PROPERLY INDENTED花括号,而不是依赖于使用语句的的one line缩写版本。这将有助于你保持正确,因为你的大括号不匹配,代码在错误的地方因为它:

You REALLY need to use PROPERLY INDENTED curly braces instead of relying on the "one line" abbreviated version of the using statement. This will help you keep things straight because you''ve got braces mismatched and code in the wrong places because of it:
public List<string> FindExistingValues()
{
    List<string> results = new List<string>();

    string getStringsCmd = "Table_Mahasiswa1BindingSource FROM dbo.Mahasiswa";

    using (SqlConnection _con = new SqlConnection(""))
    {
        using (SqlCommand _cmd = new SqlCommand(getStringsCmd, _con))
        {
            _con.Open();
 
            using (SqlDataReader rdr =_con.ExecuteReader())
            {
                while (rdr.Read())
                {
                    results.Add(rdr.GetString(0));
                }
            }
        }
    }

    return results;
}



哦!你的 getStringsCmd SQL命令看起来有点......时髦。如果SQL Server对你大吼大叫,我不会感到惊讶。





删除了额外的< / string>< / string>< / string>标签

[/ Edit]


Oh! Your getStringsCmd SQL command looks a bit .... funky. I wouldn''t be surprised if the SQL Server yelled at you about it.


Removed extra </string></string></string> tags
[/Edit]


这篇关于当前上下文中不存在名称“_con”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆