我想獲取我通過我的asp.net網站插入數據庫表的最後一條記錄的id。
我的插入語句是一個存儲過程,我試過使用標量函數,但它給了我這個指定的轉換無效。對插入語句執行標量
這是INSERT語句,我現在用:
@ClientID varchar(250),
@BType varchar(250),
@CIP varchar(50),
@Sess varchar(500),
@LoggedIn int,
@LoggedInTime datetime
AS
BEGIN
INSERT INTO tblSession (ClientID, BType, CIP, Sess, LoggedIn, LoggedInTime) VALUES (@ClientID, @BType, @CP,@Sess, @LoggedIn, @LoggedInTime)
SELECT SCOPE_IDENTITY()
END
的,我是調用方法:
using (SqlConnection con = DBConnection.getConnection())
{
string sql = "Add";
SqlCommand command = new SqlCommand(sql, con);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ClientID ", SqlDbType.VarChar).Value = id;
command.Parameters.Add("@BType", SqlDbType.VarChar).Value = bt;
command.Parameters.Add("@CIP", SqlDbType.VarChar).Value = cip;
command.Parameters.Add("@Sess", SqlDbType.VarChar).Value = s;
command.Parameters.Add("@LoggedIn", SqlDbType.Int).Value = LoggedIn;
command.Parameters.Add("@LoggedInTime", SqlDbType.DateTime).Value = session.LoggedInTime;
int x;
x = (int)command.ExecuteScalar();
return x;
}
請任何幫助是非常appreiciate它
是否command.ExecuteScalar()返回任何東西?如果是這樣,它是什麼類型? –
更好地使用輸出參數或返回值; http://msdn.microsoft.com/en-us/library/59x02y99(v=vs.71).aspx –
commad.execute scallar返回0 – wandos