我的一个朋友有问题,她在c#做了一个项目,一切看起来都不错,但问题是,当她把它放在代码中时,她得到SQL查询错误,但是当我们尝试代码时在Microsoft SQL Server中,它可以工作。
以下是错误显示的代码示例:
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;
答案 0 :(得分: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 ";
答案 1 :(得分: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";
答案 2 :(得分: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";