如何绑定日期参数 [英] How to bind date parameter

查看:40
本文介绍了如何绑定日期参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void AddNew(string stu_name, string address, string Phone, datetime date)
        {
       String sql = "Select I_Student_ID.NEXTVAL from dual;
       Int newStudent_ID = 0;
  OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
    using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))
            {
                newStudent_ID = Convert.ToInt32(db.ExecuteScalar(cmd));
            }
     
        OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("PHTS");
 
 using (System.Data.Common.DbCommand cmd = db.GetStoredProcCommand("AddNewStudent"))
            {
                db.AddInParameter(cmd, "I_Student_ID",DbType.Int32,newStudent_ID);
                db.AddInParameter(cmd, "I_STU_Name", DbType.String,stu_name);
                db.AddInParameter(cmd, "I_Address", DbType.String,address);
                db.AddInParameter(cmd, "I_Phone", DbType.String,Phone);
                db.AddInParameter(cmd, "I_Date", DbType.date,date);
 
               db.ExecuteNonQuery(cmd);
 
            }
 
        }
    }



Oracle中的存储过程



Stored Procedure in Oracle

Create or replace PRPCEDURE AddNewStudent (
I_STU_ID    Number,    
I_STU_Name  Varchar2,
I_Address  Varchar2,
I_Phone  Varchar2,
I_Date   date,
)
As
Begin
Insert into I_Student
(
Student_ID, STU_Name, Address, Phone, Date
)
Values
(
I_Student_ID, I_STU_Name, I_Address, I_phone, I_Date
);

The stored Procedure is working fine in oracle. I am getting error mismatch datatype for the date. How can I bind the date datatype in my parameter. 



用户界面



User interface

private void savebutton_Click(object sender, EventArgs e)
        {
            string student = this.STU_NameTextBox.Text;
            string address = this.addressTextBox.Text;
            string phone = this.phoneTextBox.Text;
            string date = DateTime.Parse(this.DatetextBox.Text).ToString("MM/dd/yyyy");
         
            AddNew(STU_Name, address, Phone,date);



现在我得到错误无法将类型" string"隐式转换为" System.DateTime""

谢谢



Now I am getting " Error Cannot implicitly convert type ''string'' to ''System.DateTime'' "

Thanks

推荐答案

使用db.AddInParameter(cmd,"I_Date",DbType.Date,date);
而不是db.AddInParameter(cmd,"I_Date",DbType.datetime,date);

这应该可以解决您的问题,除此之外,代码中没有任何错误.


有了荣誉,

Pradeep
Use db.AddInParameter(cmd, "I_Date", DbType.Date,date);
instead of db.AddInParameter(cmd, "I_Date", DbType.datetime,date);

this should solve your problem, besides this there is nothing wrong in the code.


With kudos,

Pradeep


string date = DateTime.Parse(this.DatetextBox.Text).ToString("MM/dd/yyyy");
            AddNew(STU_Name, address, Phone,date);



您的AddNew(string,string,string,datetime)的输入参数
但是您正在尝试将字符串值发送到datetime参数.



your input parameter for AddNew(string ,string ,string, datetime)
but you are trying to send string value to datetime parameter.


这篇关于如何绑定日期参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆