我的表单 dtp_VacationStart
和 dtp_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
@vacationStart
是 DateTime
类型,但你给它分配了一个字符串。
您应该获取 DateTimePicker
的 DateTime
值(而不是 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/