开发者

vb.net - error filling data adapter with dataset

开发者 https://www.devze.com 2023-03-16 22:45 出处:网络
below is my code: myCommand = New SqlCommand(\"SELECT VisitorID,开发者_如何学编程 VisitorName, Skill FROM tblVisitor LEFT JOIN tblSkill ON tblVisitor.SkillID = tblSkill.SkillID\", myConnection)

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.


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


have you created instance for your dataset??

refer this

check this

0

精彩评论

暂无评论...
验证码 换一张
取 消