-1

我的一个朋友有一个问题,她用 c# 做了一个项目,一切看起来都很好,但问题是,当她将 SQL 查询放入代码时,她会出错,但是当我们在 microsoft SQL 中尝试代码时服务器它工作。

这是错误显示的代码示例:

DataSet dsDelP = new DataSet();
string sql = "";
sql = "SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa, d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi";
sql += "FROM grupetOraret gro";
sql += "inner join Profesori p on gro.Profesori_ID=p.Profesor_ID";
sql += "inner join Klasa k on gro.Klasa_ID=k.Klasa_ID";
sql += "inner join Dita d on gro.ID_Dita=d.ID_dita";

SqlDataAdapter daDelP = new SqlDataAdapter(sql, con);
daDelP.Fill(dsDelP, "grupetOraret");
dataGridViewKerkoOrarin.DataSource = dsDelP.Tables[0].DefaultView;
4

3 回答 3

1

你没有空格。您应该将它们添加到每一行(在其开头或结尾)。假设查询本身是正确的(只要您已经检查过),它应该是这样的:

sql = "SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa,  d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi ";
sql += "FROM grupetOraret gro ";
sql += "inner join Profesori p on gro.Profesori_ID=p.Profesor_ID ";
sql += "inner join Klasa k on gro.Klasa_ID=k.Klasa_ID ";
sql += "inner join Dita d on gro.ID_Dita=d.ID_dita ";
于 2012-12-26T01:13:57.457 回答
0

连接字符串后,您的 SQL 如下所示:

SELECT ... as MbarimiFROM .... on gro.Profesori_ID= p.Profesor_IDinner ...

您需要在每行的最后一个单词和下一行的第一个单词之间留一个空格:

string sql = "... as Mbarimi";
sql += " FROM ...";
sql += " inner join Profesori p on gro.Profesori_ID=p.Profesor_ID";
sql += " inner join Klasa k on gro.Klasa_ID=k.Klasa_ID";
sql += " inner join Dita d on gro.ID_Dita=d.ID_dita";
于 2012-12-26T01:13:02.873 回答
0

附加到 sql 变量不会自动插入换行符,所以sql最终看起来像

...FROM grupetOraret groinner join Profesori p on gro.Profesori_ID=p.Profesor_ID...

请注意,grupetOraret gro它们inner join之间没有空格。

尝试使用@多行字符串文字,使事情变得更容易和更高效:

string sql = @"
    SELECT Grupi_ID, (p.emri + ' ' + p.mbiemri) as Profesori , k.EmriKlases as Klasa, d.emri as Dita, Ora_fillimit as Fillimi, Ora_mbarimit as Mbarimi
    FROM grupetOraret gro
    inner join Profesori p on gro.Profesori_ID=p.Profesor_ID
    inner join Klasa k on gro.Klasa_ID=k.Klasa_ID
    inner join Dita d on gro.ID_Dita=d.ID_dita";
于 2012-12-26T01:15:09.770 回答