简体   繁体   English

int与datetime2不兼容

[英]int is incompatible with datetime2

When my stored procedure is used in my asp web form I'm trying to make it grab the current time and date. 当我的ASP Web表单中使用存储过程时,我正在尝试使其获取当前时间和日期。 The info will be stored in a column with datetime2 data type. 该信息将存储在datetime2数据类型的列中。 When filling out my form I get this.. 填写表格时,我得到了这个。

Operand type clash: int is incompatible with datetime2 操作数类型冲突:int与datetime2不兼容

command.Parameters.AddWithValue("@dt2LastLoginDate", SqlDbType.DateTime2);

What should be used in order to store the date and time as the correct data type? 为了将日期和时间存储为正确的数据类型,应该使用什么?

protected void Submit_Click(object sender, EventArgs e)
{
    try
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString);
        con.Open();
        SqlCommand command = new SqlCommand("dbo.P_AddAccount");
        command.Connection = con;
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@nvcAccountName", TextBoxUN.Text));
        command.Parameters.Add(new SqlParameter("@inyAccountLevelCode", 100));
        command.Parameters.Add(new SqlParameter("inyCharacterCreateLimit", 4));
        command.Parameters.Add(new SqlParameter("@inyCharacterMaxCount", 4));
        var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
        param.Value = DateTime.Now;
        command.Parameters.Add(param);
        var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
        wrongParam.Value = System.Data.SqlDbType.DateTime2;
        command.Parameters.Add(new SqlParameter("@vchLastLoginIP", null));
        command.Parameters.Add(new SqlParameter("@@IntLastSessionID", null));
        command.Parameters.Add(new SqlParameter("@vchJoinIP", null));
        command.Parameters.Add(new SqlParameter("@inyPublisherCode", 4));
        command.Parameters.Add(new SqlParameter("@inyGenderCode", null));
        command.Parameters.Add(new SqlParameter("@DaTBirthDate", null));
        command.Parameters.Add(new SqlParameter("@vchPassphrase", TextBoxPass.Text));
        command.Parameters.Add(new SqlParameter("@inyNationalityCode", null));
        command.Parameters.Add(new SqlParameter("@inyChannelPartnerCode", null));
        command.Parameters.Add(new SqlParameter("@EmailAddress", TextBoxEA.Text));
        command.Parameters.Add(new SqlParameter("@FullName", TextBoxFN.Text));
        command.Parameters.Add(new SqlParameter("@Country", DropDownListCountry.SelectedValue));
        command.ExecuteNonQuery();
        con.Close();


    }

The SqlDBType.DataTime2 ( MSDN reference ) is an Enum which will be converted to an int (value 33 ) when parameter is executed. SqlDBType.DataTime2MSDN参考 )是一个Enum ,当执行参数时,它将转换为int (值33 )。 You will need to provide the set the actual DateTime value you wish to set to the paramter on execution. 您将需要在执行时向集合提供您希望设置为参数的实际DateTime值。 A sample would be: 一个示例是:

var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
param.Value = System.DateTime.Now;
command.Parameters.Add(param);

This creates a new paramter with the parameter name, and data type. 这将创建一个具有参数名称和数据类型的新参数。 Next we set the value of the paramter to DateTime.Now or the date value of your choice. 接下来,我们将参数的值设置为DateTime.Now或您选择的日期值。 Finally this is added to the paramters collection. 最后,将其添加到paramters集合中。

Now in your sample the Parameters.AddWithValue() method accepts two parameters. 现在在示例中, Parameters.AddWithValue()方法接受两个参数。 The name of the parameter (ie @dt2LastLoginDate ) and the value to set the paramter to during execution. 参数的名称(即@dt2LastLoginDate )和在执行期间将参数设置为的值。 Therefore you can rewrite this statement can be rewritten as: 因此可以重写此语句,可以将其重写为:

var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
wrongParam.Value = System.Data.SqlDbType.DateTime2;

Which as you can see is setting the value to the enum value of SqlDbType.DateTime2 如您所见,将值设置为SqlDbType.DateTime2的枚举值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM