Reputation: 9713
below is my code:
myCommand = New SqlCommand("SELECT VisitorID, VisitorName, Skill FROM tblVisitor LEFT JOIN tblSkill ON tblVisitor.SkillID = tblSkill.SkillID", myConnection)
myAdapter = New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet, "tblVisitor")
tblView.DataSource = myDataSet.Tables(0)
then it catch an error as Incorrect Syntax near".
which it points to myAdapter.Fill(myDataSet, "tblVisitor")
not the Select command.
Please help.
Upvotes: 0
Views: 5301
Reputation: 460018
It's a normal behavior that the exception is thrown on DataAdapter.Fill
and not on building the SqlCommand. Because Fill
is the first point where ADO.NET will query your database.
What comes after "Incorrect syntax near"? Because normally the database will give you a hint where to find the error in SQL-Statement.
Your SQl-Statement seems to be correct if your Model is similar to this test-model:
declare @tblVisitor table(
VisitorID int,
VisitorName varchar(100),
SkillID int
)
declare @tblSkill table(
SkillID int,
Skill int
)
INSERT INTO @tblVisitor VALUES(1,'Name1',1);
INSERT INTO @tblVisitor VALUES(2,'Name2',2);
INSERT INTO @tblVisitor VALUES(3,'Name3',3);
INSERT INTO @tblSkill VALUES(1,100);
INSERT INTO @tblSkill VALUES(2,200);
INSERT INTO @tblSkill VALUES(3,300);
SELECT VisitorID, VisitorName, Skill
FROM @tblVisitor tblVisitor LEFT JOIN
@tblSkill tblSkill ON tblVisitor.SkillID = tblSkill.SkillID
The result:
VisitorID VisitorName Skill
1 Name1 100
2 Name2 200
3 Name3 300
Upvotes: 1