在C#中发送参数时,如何从查询中接收一组数据?

在C#中发送参数时,如何从查询中接收一组数据?,c#,sql-server,visual-studio,winforms,C#,Sql Server,Visual Studio,Winforms,我有一个例外,我不明白: System.Data.SqlClient.SqlException:“Clienti附近的语法不正确。” 这是导致错误的代码: string connectionString = @"Data Source=VONWOLFENSPC\MSSQLSERVER01;Initial Catalog=Gestionare_excursii_agentie_turism;Integrated Security=True"; SqlConnection sqlCon = new

我有一个例外,我不明白:

System.Data.SqlClient.SqlException:“Clienti附近的语法不正确。”

这是导致错误的代码:

string connectionString = @"Data Source=VONWOLFENSPC\MSSQLSERVER01;Initial Catalog=Gestionare_excursii_agentie_turism;Integrated Security=True";

SqlConnection sqlCon = new SqlConnection(connectionString: connectionString);

string selectsql = "Select G.Nume, G.Prenume, G.Telefon, G.NumarInsotitori, " +
                    "G.ClientID, G.Sex, " +
                    "(Select E.Nume From Excursii E where E.ExcursieID LIKE G.ExcursieID) AS Excursie" +
                    "From Clienti G Where G.CNP Like @cnp";

SqlCommand cmd = new SqlCommand(selectsql, sqlCon);
cmd.Parameters.AddWithValue("@cnp", comboBox2.Text);

try
{
    sqlCon.Open();

    // error happens on the next line
    using (SqlDataReader read = cmd.ExecuteReader())
    {
        while(read.Read())
        {
             //...

        }
    }
}
finally
{
    sqlCon.Close();
}

如何修复它?

使用多行字符串文字,而不是串联查询:

    string selectsql = @"
Select  G.Nume, G.Prenume, G.Telefon, G.NumarInsotitori, G.ClientID, G.Sex, 
       (
         Select E.Nume 
         From Excursii E 
         where E.ExcursieID LIKE G.ExcursieID
        ) AS Excursie
From Clienti G 
Where G.CNP Like @cnp
";

您可以直接将其粘贴到SSMS查询窗口或从中粘贴到SSMS查询窗口进行测试。

您遇到的问题只是空格,如果您尝试重新检查字符串,您会注意到之前从中缺少空格

此外,您还需要使用
using

    string selectsql =  @"SELECT  
                            G.Nume
                        ,   G.Prenume
                        ,   G.Telefon
                        ,   G.NumarInsotitori
                        ,   G.ClientID
                        ,   G.Sex
                        ,   (SELECT E.Nume FROM Excursii E WHERE E.ExcursieID LIKE G.ExcursieID) AS Excursie
                        FROM 
                            Clienti G 
                        WHERE 
                            G.CNP LIKE @cnp";

    using(SqlConnection sqlCon = new SqlConnection(connectionString))
    using(SqlCommand cmd = new SqlCommand(selectsql, sqlCon) )
    using(SqlDataReader read = cmd.ExecuteReader())
    {
        cmd.Parameters.AddWithValue("@cnp", comboBox2.Text);        

        sqlCon.Open();

        while(read.Read())
        {
            textBox1.Text = (read["Nume"].ToString());
            textBox2.Text = (read["Prenume"].ToString());
            textBox3.Text = (read["Telefon"].ToString());
            textBox4.Text = (read["NumarInsotitori"].ToString());
            textBox5.Text = (read["ClientID"].ToString());
            comboBox1.Text = (read["Excursie"].ToString());
            string sex = (read["Sex"].ToString());
            if (sex == "M")
            {
                checkBox1.Checked = true;
                checkBox2.Checked = false;
            }
            else
            {
                checkBox2.Checked = true;
                checkBox1.Checked = false;
            }

        }
    }

乍一看,在
FROM
子句和别名
shifte
之间缺少一个空格``字符。添加空格,然后报告就是这样,我尝试了1个小时来解决它,但什么都没有(=))。谢谢,你太棒了。@VonWolfen我有点不确定,但你可能正试图用第二个选择重新创建联接。不相关的提示:SqlConnection和SqlCommand都是IDisposable的,因此它们都应该使用块位于
中。完成此操作后,就不需要关闭连接,因为它将在退出块时被隐式Dispose关闭。此外,你可能想阅读。