Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “新表达式在类型之后需要()、[]或{}”是什么意思?_C#_Linq_Types_Expression - Fatal编程技术网

C# “新表达式在类型之后需要()、[]或{}”是什么意思?

C# “新表达式在类型之后需要()、[]或{}”是什么意思?,c#,linq,types,expression,C#,Linq,Types,Expression,这是我的代码: using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebContro

这是我的代码:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=OCS-MXL930055N\\;Initial           Catalog=sample;Integrated Security=True";
        SqlCommand cmd = new SqlCommand

        con.Open("Select * from ShoppingList", con);

        con.Close("Select * from ShoppingList", con);
    }
}
以下是我遇到的问题:

con.Open("Select * from ShoppingList", con)();

con.Close("Select * from ShoppingList", con)();
对他们的意思有帮助吗?我不太确定我做错了什么。

你的声明:

SqlCommand cmd = new SqlCommand
应该是:

SqlCommand cmd = new SqlCommand("Select * from ShoppingList");
稍后在打开和关闭连接时,只需删除参数,这些参数是SqlCommand构造函数所必需的

您的代码可能类似于:

using(SqlConnection con = new SqlConnection("Data Source=OCS-MXL930055N\\;Initial Catalog=sample;Integrated Security=True"))
using(SqlCommand cmd = new SqlCommand("Select * from ShoppingList", con))
{
    //.. code to execute command
}

了解基本C、构造函数和

您尚未正确创建SqlCommand实例:

SqlCommand cmd = new SqlCommand
变成:

SqlCommand cmd = new SqlCommand("Select * from ShoppingList");
这反过来意味着:

con.Open("Select * from ShoppingList", con)();
变得简单:

con.Open();

同样,con.Close

C不是ruby,你需要让别人知道你的意图。实例化对象有3种方法,都使用新运算符:: e、 g

注意,您可以混合使用数组和初始值设定项,以便执行此操作及其法律法规:

var myInts = new int[]{1,2,3,4,5,6}; //create an int array with 6 values.
要修复代码段,您需要添加如下参数:

   SqlCommand cmd = new SqlCommand();

如果您发送的是sql字符串,我强烈建议您使用nuget上的库

这意味着编译器需要知道您试图初始化一个新实例或一个类型的多个实例。在您的例子中,将括号添加到:SqlCommand cmd=newsqlcommand;注意括号?
   SqlCommand cmd = new SqlCommand();