cmd.CommandText ="SELECT IDENT_CURRENT('WebUrl') AS Current_Identity";
string id = cmd.ExecuteScalar().ToString();
運用此方法可以抓到最近一筆存入的資料的ID是多少
可以利用此方式當作上傳檔案的檔案夾名稱 這樣就不會有上傳檔案卻有檔案名稱重複的問題
以下是資料庫存入完整程式碼
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO WebUrl(Title, Picture, Url, IsActived, PublishUpDate, PublishDownDate, CreateDate, ModifyDate) "
+ "VALUES (@Title, @Picture, @Url, @IsActived, @PublishUpDate, @PublishDownDate, @CreateDate, @ModifyDate);"
+ "SELECT IDENT_CURRENT('WebUrl') AS Current_Identity";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Title", WebUrlbean.Title);
cmd.Parameters.AddWithValue("@Picture", WebUrlbean.Picture);
cmd.Parameters.AddWithValue("@Url", WebUrlbean.Url);
cmd.Parameters.AddWithValue("@IsActived", WebUrlbean.IsActived);
cmd.Parameters.AddWithValue("@PublishUpDate", WebUrlbean.PublishUpDate);
cmd.Parameters.AddWithValue("@PublishDownDate", WebUrlbean.PublishDownDate);
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
cmd.Parameters.AddWithValue("@ModifyDate", DateTime.Now);
string id = cmd.ExecuteScalar().ToString();
conn.Close();
return id;
沒有留言:
張貼留言