我有一個表Absences
| Id | Name | Job |
-------------------------
| 1 | James | 1 |
-------------------------
| 2 | Simon | 1 |
-------------------------
其中,ID是1
遞增的身份Primary Key
我正在訪問此ta從C#程序竹葉提取,我需要做到以下幾點:
Insert Into Absences (Name, Job) Values ('aName', 'aJob')
的問題是我需要得到Id
欄在那裏我同時插入,因爲Name
和Job
不是唯一的,這樣之後,我將無法恢復此確切列。
是否可以在該查詢的Id
列中添加一個選擇?
更新
SqlConnection myConnection = new SqlConnection(@"SomeConnection");
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "Insert Into Absences (Name, Job) Values ('aName', 'aJob')";
int currentAbs = (int)myCommand.ExecuteScalar();
我上的ExecuteScalar線的錯誤。對象引用未設置爲對象的實例。
[適用範圍身份有時會返回不正確的值(https://connect.microsoft.com/SQLServer/feedback/details/328811 /範圍-身份有時-返回-不正確值)。使用變通方法中建議的'OUTPUT'子句。 – HABO 2012-08-01 12:53:50
@HABO - 這個錯誤在SQL 2008中也得到了證實嗎? – 2012-08-01 13:02:46
@PhilMurray - AFAIK,它在2008年的一生中得到了修復,但我還沒有找到一個關於何時的聲明。它被標記爲「關閉爲固定」。 – HABO 2012-08-01 13:07:25