[英]SQL with Visual C#: Incorrect syntax near ''
I keep on getting an sql exception error that states: 我不断收到一个SQL异常错误,指出:
Incorrect syntax near 'Bsc.'.
'Bsc。'附近的语法不正确。
This error happens at the line I have indicated with a comment. 此错误发生在我用注释指示的行上。
'Bsc'
is the value of val1 that has been parsed? 'Bsc'
是已解析的val1的值吗?
I have tried all means to resolve the error but I am not sure what's the problem, 我已经尝试了所有方法来解决该错误,但是我不确定是什么问题,
public Marksheet(object val1, object val2, object val3, object val4)
{
InitializeComponent();
string connectionString = null;
SqlConnection conn;
connectionString = "Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=jms";
SqlDataAdapter sda = new SqlDataAdapter("Select s.course_abbreviation, s.course_name, s.month_of_admission, s.year_of_admission from students s where exists (select 1 from units_allocation ua where s.course_abbreviation=ua.'"+val1+"' and s.course_name=ua.'"+val2+"' and s.month_of_admission=ua.'"+val3 +"' and s.year_of_admission=ua.'"+val4+"'", connectionString);
conn = new SqlConnection(connectionString);
DataTable dt5 = new System.Data.DataTable();
sda.Fill(dt5); // Error occurs here
gridControl1.DataSource = dt5;
}
The sql query is an inner join between students table and units_allocated table. sql查询是学生表和units_allocated表之间的内部联接。
I am fetching records from students table where the 4 fields are equal... 我正在从学生表中获取记录,其中4个字段相等...
The above function is in different form and which is called by the following: 上面的函数具有不同的形式,并由以下函数调用:
object val1;
object val2;
object val3;
object val4;
private void gridView1_RowClick(object sender, RowClickEventArgs e)
{
int rowHandle = e.RowHandle;
val1 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[2]);
val2 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[3]);
val3 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[4]);
val4 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[5]);
Marksheet marksheet = new Marksheet(val1, val2, val3, val4);
marksheet.Show();
}
I want to output a gridview/table in the second form with student records having 4 field values to the one I am clicking. 我想以第二种形式输出一个带有学生记录的gridview /表,该记录具有4个我要单击的字段值。
I am passing column values and expecting records from students table with fields registration_no, first_name, last_name, cat1_marks, cat2_marks, exam_marks and overall_marks. 我正在传递列值,并期望来自students表的记录包含字段registration_no,first_name,last_name,cat1_marks,cat2_marks,exam_marks和overall_marks。
It seems you are trying to match on column names and your val
variables contain the names of those columns, so you need to remove the single quotes - the quotes are only necessary if trying to match on an explicit value. 似乎您正在尝试匹配列名称,并且
val
变量包含这些列的名称,因此您需要删除单引号-仅在尝试匹配显式值时才需要引号。
where s.course_abbreviation=ua."+val1+"
so your query will look like this: 因此您的查询将如下所示:
SqlDataAdapter sda = new SqlDataAdapter(
"Select s.course_abbreviation, s.course_name, s.month_of_admission, s.year_of_admission " +
"from students s " +
"where exists (select 1 from units_allocation ua " +
"where s.course_abbreviation=ua."+val1+" and s.course_name=ua."+val2+" and s.month_of_admission=ua."+val3 +" and s.year_of_admission=ua."+val4+""
, connectionString);
Of course the usual "you should never inject SQL parameters that way!" 当然,通常的“您绝不应该那样注入SQL参数!” warning applies.
警告适用。
Edit: 编辑:
if my previous assumption was incorrect, then the other issue you have is the table alias in your where
clause - just remove it. 如果我以前的假设不正确,那么您遇到的另一个问题是
where
子句中的表别名-只需将其删除即可。 But if you do that then your inner select still looks really wrong*. 但是,如果您这样做,那么内部选择仍然看起来确实是错误的*。 If we distill it down to just a straight SQL statement we have:
如果我们将其简化为一条简单的SQL语句,则可以得到:
select s.course_abbreviation, s.course_name, s.month_of_admission, s.year_of_admission
from students s
where exists (
select 1
from units_allocation ua
where s.course_abbreviation='val1'
and s.course_name='val2'
and s.month_of_admission='val3'
and s.year_of_admission='val4'
)
This is a bit of a funny way to try to do an inner join. 这是尝试进行内部联接的一种有趣方式。 Why would you select from the
units_allocation
table by comparing students
columns to explicit values? 您为什么要通过将
students
列与显式值进行比较来从units_allocation
表中进行选择?
You may be stuck because there is no direct link from units_allocation
back to students
, - in this case you will need to join via other tables as well. 您可能会被卡住,因为从
units_allocation
没有直接链接回students
,在这种情况下,您还需要通过其他表加入。 Your inner join should look something like this: 您的内部联接应如下所示:
select s.*, ua.*
from students s
inner join units_allocation ua
on ua.[foreign_key_column] = s.[primary_key_column]
where s.course_abbreviation='val1'
and s.course_name='val2'
...etc...
Using an exists
could be counterproductive the way you have it - it will return all rows in the outer query if the exists
condition is satisfied. 使用一个
exists
的方法可能会适得其反-如果满足exists
条件,它将返回外部查询中的所有行。
* I'm a SQL generalist, not a specialist. * 我是SQL通才,不是专家。 So this
exists()
syntax for achieving an inner join may be totally wrong, I don't know unless I try it - which I haven't (and I've never written a join in that way). 因此,用于实现内部联接的
exists()
语法可能完全错误,除非尝试,否则我不知道-我没有(而且我从未以这种方式编写联接)。
where s.course_abbreviation=ua.'"+val1+"'
Remove the ua. 删除ua。 and it should fix it.
它应该修复它。
using x. 使用x。 in front of a variable in SQL tries to find the defination of a column.
在SQL中的变量前面尝试查找列的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.