“CS1026: ) expected”

折月煮酒 提交于 2020-08-27 08:06:08

问题


using (SqlCommand cmd = new SqlCommand("ReportViewTable", cnx) 
  { CommandType = CommandType.StoredProcedure })

When I try to open this page in the browser I am getting the

CS1026: ) expected error

on this line, but I don't see where it's throwing the error. I have read that an ; can cause this issue, but I don't have any of them.

I can help with any additional information needed, but I honestly don't know what question I need to ask. I am trying to google some answers on this, but most of them deal with an extra semicolon, which I don't have.

Any help is appreciated. Thank you.


回答1:


If this is .NET 2.0, as your tags suggest, you cannot use the object initializer syntax. That wasn't added to the language until C# 3.0.

Thus, statements like this:

SqlCommand cmd = new SqlCommand("ReportViewTable", cnx) 
{ 
    CommandType = CommandType.StoredProcedure 
};

Will need to be refactored to this:

SqlCommand cmd = new SqlCommand("ReportViewTable", cnx);
cmd.CommandType = CommandType.StoredProcedure;

Your using-statement can be refactored like so:

using (SqlCommand cmd = new SqlCommand("ReportViewTable", cnx))
{
    cmd.CommandType = CommandType.StoredProcedure;
    // etc...
}



回答2:


You meant this:

using (SqlCommand cmd = new SqlCommand("ReportViewTable", cnx)) { cmd.CommandType = CommandType.StoredProcedure; }



回答3:


Addition to ioden answers:

Breaking code in multiple lines,
then double click on error message in compile result should redirect to exact location

something like:

enter image description here



来源:https://stackoverflow.com/questions/10029563/cs1026-expected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!