简体   繁体   English

Visual Studio错误:从字符串转换日期和/或时间时转换失败

[英]Visual Studio error: Conversion failed when converting date and/or time from character string

Hi I am new in this forum. 嗨,我是这个论坛的新手。 its my first post so if I made something wrong... I have the code below. 这是我的第一篇文章,所以,如果我做错了什么...我有下面的代码。 I am receiving the error 我收到错误

"conversion failed when converting date and/or time from character string. visual studio" “从字符串转换日期和/或时间时转换失败。visualstudio”

 public void pay_fighter(string FighterID, string PaymentDay, string PaymentAmount, string PaymentDescr)
    {
        if (FighterID.Length != 0)
        {
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.Parameters.AddWithValue("PaymentAmount", PaymentAmount);
            newCmd.Parameters.AddWithValue("PaymentDescr", PaymentDescr);
            newCmd.CommandText = "INSERT INTO PaymentInfo VALUES('" + FighterID + "','" + PaymentDay + "',@PaymentAmount, @PaymentDescr)";
            newCmd.ExecuteNonQuery();
        }
    }

Use correct types and SqlParameters for all parameters in the query. 对查询中的所有参数使用正确的类型和SqlParameters。

public void pay_fighter(
    string fighterID, 
    DateTime paymentDay, 
    decimal paymentAmount, 
    string paymentDescr)
{
    if (String.IsNullOrEmpty(fighterId))
    {
        return;
    }

    var query = 
        @"INSERT INTO PaymentInfo VALUES (
            @FighterId, 
            @PaymentDay, 
            @PaymentAmount, 
            @PaymentDescr)";
    var parameters = new[]
    {
        new SqlParameter
        {
            ParameterName = "@FigtherId",
            SqlDbType = SqlDbType.Int, // use correct type for column
            Value = fighterId
        },
        new SqlParameter
        {
            ParameterName = "@PaymentDay",
            SqlDbType = SqlDbType.DateTime, // use correct date type defined in column
            Value = paymentDay
        },
        new SqlParameter
        {
            ParameterName = "@PaymentAmount",
            SqlDbType = SqlDbType.Decimal, // use correct type for column
            Value = paymentAmount
        },
        new SqlParameter
        {
            ParameterName = "@PaymentDescr",
            SqlDbType = SqlDbType.VarChar, // use correct type for column
            Size = 100, // use size defined for column
            Value = paymentDescr
        }
    };

    using (var connection = new SqlConnection(connectionString))
    using (var command = new SqlCommand(query, connection))
    {
        command.Parameters.AddRange(parameters);

        connection.Open();
        command.ExecuteNonQuery();
    }
}

Will be more safe to create new SqlConnection instance for query. 创建新的SqlConnection实例进行查询将更加安全。 ADO.NET will handle actual/physical connections and re-use already created if needed. ADO.NET将处理实际/物理连接,并根据需要重新使用已经创建的连接。

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

相关问题 从字符串错误4转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string error 4 错误“从字符串转换日期和/或时间时转换失败” - Error “Conversion failed when converting date and/or time from character string” 错误:从字符串转换日期和/或时间时转换失败 - Error : Conversion failed when converting date and/or time from character string 错误:从字符串转换日期和/或时间时转换失败 - Error: Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and /or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and /or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 问题:从字符串转换日期和/或时间时转换失败 - Problem: Conversion failed when converting date and/or time from character string 从字符串#2转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string #2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM