gpt4 book ai didi

c# - 将 DateTimePicker 值传递给存储过程

转载 作者:行者123 更新时间:2023-11-30 14:09:39 25 4
gpt4 key购买 nike

我的表单 dtp_VacationStartdtp_VacationEnd 上有两个 DateTimePicker 。我想将其值传递给存储过程

我的 SQL 代码

CREATE PROC [ADD_VACATION]  @vacationStart date ,@vacationEnd date ,@vacationMem nvarchar (100) = NULL ,@idVacationKind_L int ,@idMember_L int

AS INSERT INTO [tblVacation]
([vacationStart]
,[vacationEnd]
,[vacationMem]
,[idVacationKind_L]
,[idMember_L])
VALUES
(@vacationStart
,@vacationEnd
,@vacationMem
,@idVacationKind_L
,@idMember_L)

C#代码:

public void ADD_VACATION(string vacationStart,string vacationEnd, string vacationMem, int idVacationKind_L, int idMember_L)
{

DAL.open();

SqlParameter[] param = new SqlParameter[5];

param[0] = new SqlParameter("@vacationStart", SqlDbType.Date);
param[0].Value = vacationStart;

param[1] = new SqlParameter("@vacationEnd", SqlDbType.Date);
param[1].Value = vacationEnd;

param[2] = new SqlParameter("@vacationMem ", SqlDbType.NVarChar, 100);
param[2].Value = vacationMem;

param[3] = new SqlParameter("@idVacationKind_L", SqlDbType.Int);
param[3].Value = idVacationKind_L;

param[4] = new SqlParameter("@idMember_L", SqlDbType.Int);
param[4].Value = idMember_L;

DAL.ExecuteCommand("ADD_VACATION", param);
DAL.close();

}

和事件

private void btnAddVacation_Click(object sender, EventArgs e)
{

cms.ADD_VACATION( dtp_VacationStart.Text, dtp_VacationEnd.Text, txtVacationMemory.Text,
Convert.ToInt32(cmbVacationKind.SelectedValue)
,Convert.ToInt32(dgMember_Grade.CurrentRow.Cells[0].Value.ToString()));

MessageBox.Show("Added Successfuly", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

这段代码给我一个错误:输入字符串的格式不正确。

谢谢大家...抱歉混淆..这是我的第一个问题

最佳答案

你的 SqlParameter @vacationStartDateTime 类型,但你给它分配了一个字符串。

您应该获取 DateTimePickerDateTime 值(而不是 DateTimePicker.Text 中的字符串表示形式):

DateTime vacationStart = dtp_VacationStart.Value;

并更改ADD_VACATION的签名:

public void ADD_VACATION(DateTime vacationStart, ...

现在你可以使用它了:

private void btnAddVacation_Click(object sender, EventArgs e)
{
DateTime vacationStart = dtp_VacationStart.Value;
cms.ADD_VACATION(vacationStart, ...

关于c# - 将 DateTimePicker 值传递给存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com