帮助"SELECT @@ IDENTITY"; Windows C# [英] Help with "SELECT @@IDENTITY" Windows C#

查看:99
本文介绍了帮助"SELECT @@ IDENTITY"; Windows C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难输入自己的自动编号.谁能看到我在做错什么.感谢所有帮助.代码如下:

I''m having a hard time trying to input my autonumber. Can anyone see what I''m doing wrong. All help is appreciated. Code is as follows:

OleDbConnection DB_Connection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\HOST 000.MDB");
DB_Connection.Open();
//
DataSet DB_DataSet = new DataSet();
DataSet DB_DataSet_2 = new DataSet();
//
OleDbCommandBuilder DB_Builder;

OleDbDataAdapter Table_Adapter = new OleDbDataAdapter("SELECT * FROM [TABLE_ZONE]", DB_Connection);
Table_Adapter.RowUpdated += new OleDbRowUpdatedEventHandler(Table_Adapter_OnRowUpdate);
Table_Adapter.FillSchema(DB_DataSet, SchemaType.Source);

Table_Adapter.Fill(DB_DataSet, "TABLE_ZONE");
//
DB_Builder = new OleDbCommandBuilder(Table_Adapter);

DataRow NewRow = DB_DataSet.Tables["TABLE_ZONE"].NewRow();
//NewRow["CHECK_NUMBER"] = New_Check_Number.ToString();
NewRow["TABLE_NUMBER"] = New_Table_Number;
NewRow["TABLE_SEATS"] = Old_Seat_Amount;
NewRow["TABLE_COURSES"] = Old_Course_Amount;
NewRow["TABLE_START_DATE"] = Old_Table_Start_Date;
NewRow["TABLE_START_TIME"] = Old_Table_Start_Time;
NewRow["TABLE_ELAPSE_TIME"] = Old_Table_Elapse_Time;
NewRow["TABLE_USER_ID"] = Old_Table_User_ID;
NewRow["TABLE_USER_F_NAME"] = Old_User_F_Name;
NewRow["TABLE_USER_L_NAME"] = Old_User_L_Name;
NewRow["TABLE_DISPLAY_SEATING"] = Old_Display_Seating;
NewRow["TABLE_DISPLAY_COURSES"] = Old_Display_Courses;
NewRow["TABLE_REMINDER"] = Old_Table_Reminder;
NewRow["TABLE_MESSAGE"] = Old_Table_Message;
NewRow["TABLE_SPLIT"] = false;
NewRow["TABLE_PAYMENT"] = Old_Table_Payment;
NewRow["TABLE_PRINTED"] = false;
//
DB_DataSet.Tables["TABLE_ZONE"].Rows.Add(NewRow);
//UPDATE
Table_Adapter.Update(DB_DataSet, "TABLE_ZONE");

//CLOSE CONNECTION
DB_Connection.Close();





//UPDATE TABLE CHECK NUMBER
private void Table_Adapter_OnRowUpdate(object sender, OleDbRowUpdatedEventArgs  e)
{
OleDbCommand oCmd = new OleDbCommand("SELECT @@IDENTITY", e.Command.Connection);

if (e.StatementType == StatementType.Insert)
{
//Retrieve the identity value and store it in the CategoryID column.
New_Check_Number = oCmd.ExecuteScalar().ToString();
e.Row["CHECK_NUMBER"] = New_Check_Number;
//
e.Row.AcceptChanges();
}
}

推荐答案

@@ IDENTITY返回在当前作用域中设置的身份.看来您有两个单独的语句,这意味着两个单独的作用域
@@IDENTITY returns the identity that was set in the current scope. It appears you have two separate statements, meaning two separate scopes


就像Mark所说的超出范围一样.
我从不使用数据网格和/或数据行来操纵(更改,更新,删除)数据库.我肯定会用它们来显示数据
就是这样.其余的操作在单独的图层上进行.

当我使用access/mdb/jet时,我做了如下的事情
使用数据库生成的ID时:

Just like Mark said it is out of scope.
I never use datagrids and/or datarows to manipulate(isert,update,delete ect) the DB. Sure I use them to display data
but that is it. The rest is done on separate Layer(s).

When I used access/mdb/jet I did something like the following
when working with DB generated IDs:

//somewhere in a method
int id = -1;

OleDbConncetion con = new OleDbConnection(GetConnection(userID));
//create the command, asociate it with the connection
//declare/create the parameters, etc

try{
con.Open();
if (cmd.ExecuteNonQuery()==1)//the insert succeded
{
   cmd.CommandText = "SELECT @@IDENTITY";
   id = Convert.ToInt32(cmd.ExecuteScalar());
}
}
catch(OleDbException oex){
//log the ex
}
catch(Exception ex){
//log the ex
}
finally{
//close and dispose of the connection if still opened or !closed.
}

return id;


这篇关于帮助"SELECT @@ IDENTITY"; Windows C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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