为什么在我使用executenonquery时不必使用@@ identity [英] why don't I have to use @@identity when I'm using executenonquery

查看:108
本文介绍了为什么在我使用executenonquery时不必使用@@ identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi

表Film具有ID和Film列.列ID是一个主键和一个自动编号.

当我使用此代码时,连接的版本可以正常工作.

Hi

The table Film has columns ID and Film. The column ID is a primarykey and an autonumber.

When I'm using this code, the connected version, it works fine.

strSQL = 插入胶片(胶片)值(?)"

strSQL = "Insert into Film (Film) values(?)"

cmorder = OleDb.OleDbCommand(strSQL,conn)

cmorder = New OleDb.OleDbCommand(strSQL, conn)

Me .dFilmAdpt.InsertCommand = cmorder

Me.dFilmAdpt.InsertCommand = cmorder

Me .dFilmAdpt.InsertCommand .Parameters.Add( "Film" ,OleDbType.LongVarChar)

Me.dFilmAdpt.InsertCommand.Parameters.Add("Film", OleDbType.LongVarChar)

conn.Open()

conn.Open()

dFilmAdpt .InsertCommand.Parameters(table).Value = cbo.Text
dFilmAdpt .InsertCommand.ExecuteNonQuery()

dFilmAdpt.InsertCommand.Parameters(table).Value = cbo.Text
dFilmAdpt.InsertCommand.ExecuteNonQuery()

conn.Close()

但是当我使用断开连接的版本时,它不起作用.我已经知道我可以使用OnRowUpdated和@@ identity,但是我不明白为什么在我使用executenonquery时它起作用,而在我不使用时就不起作用.与数据库的连接或断开连接有任何关系.

da.Fill(ds,
电影" )

conn.Close()

But when I'm using the disconnected version it doesn't work. I have seen that I can use OnRowUpdated and @@identity, but I don't undertstand why it works when I'm using executenonquery and not when I don't. Has it anything with connection or disconnection to the database.  

da.Fill(ds,
"Film")

dt = ds.Tables( 电影" )

dt = ds.Tables("Film")

Dim insCommand 新建 OleDbCommand( "插入Film(Film)values(?)" ,conn)

Dim insCommand As New OleDbCommand("insert into Film(Film)values(?)", conn)

暗淡 新建 OleDbParameter

Dim par As New OleDbParameter

par.ParameterName = 电影"

par.ParameterName = "Film"

par.SourceColumn = " ;电影"

par.SourceColumn = "Film"

insCommand.Parameters.Add(par)

insCommand.Parameters.Add(par)

dFilmAdpt.InsertCommand = insCommand

dFilmAdpt.InsertCommand = insCommand

Dim newRow = dt. NewRow

Dim newRow = dt.NewRow

newRow( 电影" )= cbo.Text

newRow("Film") = cbo.Text

dt.Rows.Add(newRow)
dFilmAdpt.Update(ds)

dt.Rows.Add(newRow)
dFilmAdpt.Update(ds)

请解释,并请准确解释

Fia





 

推荐答案



原因是数据集使用其自身的机制在内存中临时创建行,并且在插入db时需要使用自动生成的数字.
Still Around?

The reason is that the dataset uses its own mechanism to temporarily create rows in memory and when inserting in db, it needs to use the autogenerated numbers.


这篇关于为什么在我使用executenonquery时不必使用@@ identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆