Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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# SqlException未经System.Data.dll中的c处理_C#_Sql_Datatable_Inner Join_Using - Fatal编程技术网

C# SqlException未经System.Data.dll中的c处理

C# SqlException未经System.Data.dll中的c处理,c#,sql,datatable,inner-join,using,C#,Sql,Datatable,Inner Join,Using,因此,当我遇到以下错误时,我尝试运行我的代码: System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 其他信息:“b”附近的语法不正确 代码如下所示: string query = "select a.Navn from Ejer a" + "INNER JOIN SlipsEjer b ON a.Id = b.EjerId" +

因此,当我遇到以下错误时,我尝试运行我的代码:

System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 其他信息:“b”附近的语法不正确

代码如下所示:

            string query = "select a.Navn from Ejer a" +
                       "INNER JOIN SlipsEjer b ON a.Id = b.EjerId" +
                       "Where b.SlipsId = SlipsId";

        using (connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            command.Parameters.AddWithValue("@SlipsId", SlipsLB.SelectedValue);

            DataTable ejerTable = new DataTable();
            adapter.Fill(ejerTable);

            EjerLB.DisplayMember = "Navn";
            EjerLB.ValueMember = "Id";
            EjerLB.DataSource = ejerTable;
        }
我不知道为什么。。我试着在Sql中通过一个新的查询来运行它,结果很好


真的希望你们能帮忙!谢谢

在每行末尾添加一个空格。还为参数添加@符号

string query = "select a.Navn from Ejer a " +
               "INNER JOIN SlipsEjer b ON a.Id = b.EjerId " +
               "Where b.SlipsId = @SlipsId";
有人建议在评论中使用逐字记录字符串。下面是一个逐字字符串示例。您可以轻松地从ManagementStudio复制查询。尽管我更喜欢使用存储过程

string query = @"
select a.Navn from Ejer a
INNER JOIN SlipsEjer b ON a.Id = b.EjerId
Where b.SlipsId = @SlipsId
";

非常感谢你!是的,我以前有个@,一定是误删了!但我不知道空格会起作用!谢谢@Handoko.chen同时使用逐字字符@将有助于避免这些错误。如果不添加空格,则如下所示:从Ejer aINNER中选择a.Navn在a.Id=b.EjerIdWhere b.SlipsId=@SlipsId上加入SlipsEjer b。