【问题标题】:Pass DateTimePicker Value to a stored procedure将 DateTimePicker 值传递给存储过程
【发布时间】:2015-01-28 01:39:09
【问题描述】:

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

我的 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);
}

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

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

【问题讨论】:

  • 你约会怎么样?来自 textBox 或 dateTimePicker?如果是 dateTimePicker,则必须使用 .Value 代替 .Text。如果它的文本框,那么你必须使用ParseExact Method
  • dateTimePicker 中的日期...我尝试使用 value 但它给了我一个错误 Argument2: cannot convert from 'System.DateTime' to 'String'

标签: c# winforms datetimepicker


【解决方案1】:

您的 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, ...

【讨论】:

  • 我应该把 DateTime holidayStart = dtp_VacationStart.Value; ?
  • 另一个错误输入字符串的格式不正确。
  • 请使用调试器。错误发生在哪里?各个字符串的内容是什么?
【解决方案2】:

我认为您输入的数据格式错误...
以正确的格式输入数据或更改数据类型

【讨论】:

  • 我同意你的观点,但错误是在说同样的事情......所以这不是答案
【解决方案3】:

您必须将 dtp_VationStart.Text 和 dtp_VacationEnd.Text 转换为日期。

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

您还可以使用 DateTime.Parse 函数。更多信息可以here

【讨论】:

  • 这涉及到两个转换 DateTime->Text->DateTime 以及所有可能的格式错误。不是 OP 想要的……
  • error Argument1:无法从“System.DateTime”转换为“String”
【解决方案4】:

函数ADD_VACATION() vacationStartvacationEnd 的参数是string 类型,而您将它们作为SqlDbType.Date 传递参数。将参数的类型更改为DateTime,并在调用函数ADD_VACATION() 中,使用.Value 进行DateTimePicker 控制。这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多