试图将自动编号从一个表复制到下一个表 [英] trying to copy the autonumber from one table to the next

查看:56
本文介绍了试图将自动编号从一个表复制到下一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经尝试了这个的微软版本,并且丢失了表格

LOl

所以我试着用这种方式写入两个携带autoid的表

但到目前为止我无法获得自动编号并将其放在另一张表中。


感谢大家提供任何帮助

也许我试着以愚蠢的方式做到这一点但它看起来像我这样

应该工作

可能不是这样:)


哦,人和成员的传递值都在sql字符串中

至少我知道怎么检查

sparks


==============================

tblperson创建autoid PersonID

但是当我尝试在tblmember中创建它时,我在第一个

ExecuteNonQuery上出现错误。


========= =====================

public void SavePerson(此人,会员)

{

Connection.Open(); // ToShortDateString()

尝试

{

String sqlString =" insert into

tblPerson(ContractNumber,姓名,地址,电话号码,条形码)" +

" values(" +

person.getContractNumber()+",''" +

person.getName()+"'',''" +

person.getAddress()+"'',''" +

person.getPhoneNum()+"'',''" +

person.getBarcodeNum()+"'')" ;;

OleDbCommand command = new

OleDbCommand(sqlString,Connection);


command.ExecuteNonQuery();

OleDbCommand cmd = new

OleDbCommand(" SELECT @@ IDENTITY",Connection);

int nId =(int)cmd.ExecuteScalar();

- -------------------------------------------------- -------------------------------------------------- ---

String sqlStr =" insert into tblMember(personID,MonthlyFee,paidup)" +
" values(" + nId +

member.getFee()+"," + member.getMemValidation()+")" ;;

OleDbCommand comma = new OleDbCommand(sqlStr,Connection);


command.ExecuteNonQuery();

Connection.Close();

}

终于

{

如果(连接!= null)

{

if(Connection.State ==

ConnectionState.Open)

Connection.Close();

}

}

解决方案

Sparks ,


您不能只调用SELECT @@ IDENTITY。 Sql无法知道你想要查找什么

记录。您是否可以编写存储的

proc并将其发送给您的参数?然后你可以在

中使用return语句来存储proc以返回插入记录的ID,然后使用ExecuteScalar()获取它。

。这是一个非常有效和可靠的方式

左右。


祝你好运!





实际上你可以说SELECT @@ IDENTITY。

为了证明这一点,只需在查询分析器中运行。在它自己运行

你得到一个值为NULL的未命名列。


原始帖子的问题更可能是原来的海报

没有在同一个命令中运行INSERT和@@ IDENTITY

。因此,@@ IDENTITY将为null,如果你在命令中运行

作为INSERT,你将获得一个数字。


再见,

Pete

" Brian Brown" <峰; br ******** @ discussions.microsoft.com>在消息中写道

news:92 ********************************** @ microsof t.com ...

Sparks,

您不能只调用SELECT @@ IDENTITY。 Sql无法知道你想要查找什么
记录。你有可能写一个
存储过程并发送你的参数吗?然后你可以在存储过程中使用返回语句
返回插入记录的ID,然后使用ExecuteScalar()获取它。这是一个更加高效和可靠的所有
方式。

祝你好运!



这是我在microsoft上发现的


// RowUpdated事件的事件处理程序

private static void HandleRowUpdated(object sender,OleDbRowUpdatedEventArgs e)

{

if(e.Status == UpdateStatus.Continue&& e.StatementType ==

StatementType.Insert)

{

//获取标识列值

e.Row [" PersonID"] = Int32.Parse(cmdGetIdentity.ExecuteScalar()。ToStri ng() );

System.Diagnostics.Debug.WriteLine(e.Row [" personID"]);

e.Row.AcceptChanges();

}

}

====================

cmdGetIdentity = new OleDbCommand();

cmdGetIdentity.CommandText =" SELECT @@ IDENTITY" ;;

cmdGetIdentity.Connection = Connection;

我可以不做这项工作我猜我是傻瓜。

但你可以看到他们也使用了SELECT。

这就是我知道我是怎么做的。

2004年12月6日星期一14:31:24 -0000,Peter Row < fuck.off@bastard_spammers.com>

写道:



其实你可以说SELECT @@ IDENTITY。
要证明这一点,只需在查询分析器中运行该行。在它自己运行它
你得到一个值为NULL的未命名列。

原始帖子的问题更可能是原始海报
没有运行INSERT和@@ IDENTITY
在同一命令中选择。因此@@ IDENTITY将为null,如果你在命令中运行
作为INSERT,你将得到一个号码。

再见,
Pete
Brian Brown <峰; br ******** @ discussions.microsoft.com>在消息中写道
新闻:92 ********************************** @ microso ft.com。 ..

Sparks,

你不能只调用SELECT @@ IDENTITY。 Sql无法知道你想要查找什么
记录。你有可能写一个
存储过程并发送你的参数吗?然后你可以在存储过程中使用返回语句
返回插入记录的ID,然后使用ExecuteScalar()获取它。这是一个更加高效和可靠的所有
方式。

祝你好运!




So far I have tried the microsoft version of this, and lost the table
LOl
so I tried this way to write to two tables carrying over the autoid
but so far I can not get the autonumber and put it in the other table.

thanks big time for any help
maybe I tried to do it the dumb way but it looks to me like this
should work
probably not :)

oh the passed values for person and member are are in the sql strings
at least I knew how to check that
sparks

==============================
the tblperson creates the autoid PersonID
but when I try to create it in tblmember I get an error on the first
ExecuteNonQuery.

==============================

public void SavePerson (Person person,Member member)
{
Connection.Open (); //ToShortDateString()
try
{
String sqlString = "insert into
tblPerson (ContractNumber,Name, Address, PhoneNumber, barcode)"+
"values (" +
person.getContractNumber() + ",''" +
person.getName() +"'',''" +
person.getAddress() +"'',''" +
person.getPhoneNum() +"'',''" +
person.getBarcodeNum() + "'')";
OleDbCommand command = new
OleDbCommand (sqlString, Connection);

command.ExecuteNonQuery();
OleDbCommand cmd = new
OleDbCommand("SELECT @@IDENTITY", Connection);
int nId = (int)cmd.ExecuteScalar();
---------------------------------------------------------------------------------------------------------
String sqlStr = "insert into tblMember (personID,MonthlyFee,paidup)"+
"values (" + nId +
member.getFee() + "," + member.getMemValidation() + ")";
OleDbCommand comma = new OleDbCommand (sqlStr, Connection);

command.ExecuteNonQuery();
Connection.Close();
}
finally
{
if (Connection!= null)
{
if (Connection.State ==
ConnectionState.Open)
Connection.Close();
}
}

解决方案

Sparks,

You cannot call just SELECT @@IDENTITY. Sql has no way of knowing what
record you want to look for. Would it be possible for you to write a stored
proc and send it your parameters? Then you could use a return statement in
the stored proc to return the ID of the inserted record and then grab it
using ExecuteScalar(). This is a lot more efficient and reliable all the way
around.

Good Luck!


Hi,

Actually you can say just SELECT @@IDENTITY.
To prove this just run the line in query analyser. Running it on it''s own
you get an unnamed column with the value NULL.

The problem with the original post is more likely that the original poster
is not running the INSERT and the @@IDENTITY
select in the same command. Therefore @@IDENTITY will be null, if you ran it
within the command as the INSERT you would
get a number back.

Bye,
Pete
"Brian Brown" <Br********@discussions.microsoft.com> wrote in message
news:92**********************************@microsof t.com...

Sparks,

You cannot call just SELECT @@IDENTITY. Sql has no way of knowing what
record you want to look for. Would it be possible for you to write a
stored
proc and send it your parameters? Then you could use a return statement
in
the stored proc to return the ID of the inserted record and then grab it
using ExecuteScalar(). This is a lot more efficient and reliable all the
way
around.

Good Luck!



this is what I found at microsoft

// Event Handler for RowUpdated Event
private static void HandleRowUpdated(object sender, OleDbRowUpdatedEventArgs e)
{
if (e.Status == UpdateStatus.Continue && e.StatementType ==
StatementType.Insert)
{
// Get the Identity column value
e.Row["PersonID"]=Int32.Parse(cmdGetIdentity.ExecuteScalar().ToStri ng());
System.Diagnostics.Debug.WriteLine(e.Row["personID"]);
e.Row.AcceptChanges();
}
}
====================
cmdGetIdentity = new OleDbCommand();
cmdGetIdentity.CommandText = "SELECT @@IDENTITY";
cmdGetIdentity.Connection = Connection;
I could not make this work I guess I am stupid.
but as you can see they used the SELECT as well.
that is where I got the idea of how I did it.
On Mon, 6 Dec 2004 14:31:24 -0000, "Peter Row" <fuck.off@bastard_spammers.com>
wrote:

Hi,

Actually you can say just SELECT @@IDENTITY.
To prove this just run the line in query analyser. Running it on it''s own
you get an unnamed column with the value NULL.

The problem with the original post is more likely that the original poster
is not running the INSERT and the @@IDENTITY
select in the same command. Therefore @@IDENTITY will be null, if you ran it
within the command as the INSERT you would
get a number back.

Bye,
Pete
"Brian Brown" <Br********@discussions.microsoft.com> wrote in message
news:92**********************************@microso ft.com...

Sparks,

You cannot call just SELECT @@IDENTITY. Sql has no way of knowing what
record you want to look for. Would it be possible for you to write a
stored
proc and send it your parameters? Then you could use a return statement
in
the stored proc to return the ID of the inserted record and then grab it
using ExecuteScalar(). This is a lot more efficient and reliable all the
way
around.

Good Luck!




这篇关于试图将自动编号从一个表复制到下一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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