获取插入ID

本文关键字:ID 插入 获取 | 更新日期: 2023-09-27 17:49:31

我正在尝试将数据插入数据库。我这里有什么,它的工作原理,但我需要从我插入的数据的ID。该ID将用于存储提醒的名称。

private void appBarButton_Click(object sender, EventArgs e)
    {
        using (DatabaseContext context = new DatabaseContext(ConnectionString))
        {
            ToDoList todolist = new ToDoList();
            todolist.Title = titleTBox.Text;
            todolist.Description = descriptionTBox.Text;
            //date was added later, datacontext is needed to regenerate
            //DateTime date = DateTime.Parse(textDateOutput.Text);
            Console.WriteLine(dateData.Value.ToString());
            todolist.Date = DateTime.Parse(dateData.Value.ToString());
            context.ToDoList.InsertOnSubmit(todolist);
            context.SubmitChanges();
            DateTime Date = rDate.Value.Value;
            TimeSpan Time = rTime.Value.Value.TimeOfDay;
            Date = Date.Date + Time;
            String Content = descriptionTBox.Text;
            String Title = titleTBox.Text;
                   // ID = get the ID from database of the inserted data.

            if (Convert.ToBoolean(cBox.IsChecked))
            {
                if (Date < DateTime.Now)
                    MessageBox.Show("Please do not enter datetime lower then the current datetime");
                else if (String.IsNullOrEmpty(Title))
                    MessageBox.Show("Please do not leave the title empty");
                else
                {
                    //gets the info and call the function to send the parameter
                    RegisterScheduleIfNotExist( /* ID */, Title, Content, Date);
                    NavigationService.GoBack();
                }
            }
            else
                NavigationService.GoBack();
        }
    }

RegisterScheduleIfNotExist方法
private void RegisterScheduleIfNotExist(string name, string title, string content, DateTime time)
    {
        ScheduledAction currentReminder = ScheduledActionService.Find(name);
        if (currentReminder != null)
        {
            ScheduledActionService.Remove(currentReminder.Name);
        }
        var reminder = new Reminder(name)
        {
            BeginTime = time,
            Title = title,
            Content = content,
        };
        ScheduledActionService.Add(reminder);
    }

获取插入ID

如果您在ToDoList类中有一个ID字段,它将在SubmitChanges之后填充分配的值。

你可以像这样获取id:

context.ToDoList.InsertOnSubmit(todolist);
context.SubmitChanges();
int id = todolist.Id; //or whatever your ID field is called on the class

EDIT about COMMENT:

您的RegisterscheduleIfNOtExist()方法没有id的参数字段。

您应该添加一个(将方法的签名更改为包含int id, string name, etc...

)

如果方法的name参数为 id字段,只需将int id = todolist.Id更改为string id = todolist.Id.ToString()

下面是一个改变方法的例子:

private void RegisterScheduleIfNotExist(int id, string name, string title, string content, DateTime time) 
{   
    ScheduledAction currentReminder = ScheduledActionService.Find(name);
    if (currentReminder != null)
    {
        ScheduledActionService.Remove(currentReminder.Name);
    }
    var reminder = new Reminder(name)
    {
        Id = id,  //Add the id field here to the reminder
        BeginTime = time,
        Title = title,
        Content = content,
    };
    ScheduledActionService.Add(reminder);
}