Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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# 从数据库获取数据时,关键字“Table”附近的语法不正确_C# - Fatal编程技术网

C# 从数据库获取数据时,关键字“Table”附近的语法不正确

C# 从数据库获取数据时,关键字“Table”附近的语法不正确,c#,C#,我创建了一个从本地数据库检索数据并将其显示在列表框中的方法 private void getOwned() { string connection = "server=(local)\\SQLEXPRESS;database=<default>;Integrated Security=SSPI"; string sql = @"select * from Table"; SqlConnection conn =

我创建了一个从本地数据库检索数据并将其显示在列表框中的方法

        private void getOwned()
    {
        string connection = "server=(local)\\SQLEXPRESS;database=<default>;Integrated Security=SSPI";
        string sql = @"select * from Table";
        SqlConnection conn = new SqlConnection(connection);

        try
        {
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Table");
            DataTable dt = ds.Tables["Table"];
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                lst_information.DataSource = row[col];
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            conn.Close();
        }
    }
当我运行代码时,有一个错误出现在关键字“Table”附近,语法不正确。我只是想知道为什么会发生这个错误。感谢您提供的帮助。

表格是TSQL中的一个表格,您需要在其周围放上方括号:

 string sql = @"select * from [Table]";

Table是sqlTry字符串sql=@select*from[Table]中的关键字;您不能这样使用它。请这样尝试:从[表]中选择*如何突出显示注释中的代码@SriramSakthivel@EhsanSajjad:把它放在反引号周围`