简体   繁体   English

使用日期“'操作数类型冲突:日期与int不兼容'用SQL查询填充Datagrid

[英]Fill Datagrid with Sql Query using Date " 'Operand type clash: date is incompatible with int''

I am new to Programming and I started with C# (Visual Studio 2017 CE); 我是编程新手,从C#(Visual Studio 2017 CE)开始。
I am writing an application and using a DataGrid . 我正在编写应用程序并使用DataGrid I am trying to fill the DataGrid using a query to a C# service based DB (mdf file). 我正在尝试使用对基于C#服务的数据库(mdf文件)的查询来填充DataGrid
When I run the app and try the query statement I get this error: 当我运行应用程序并尝试查询语句时,出现此错误:

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

at the SqlDataReader Line. SqlDataReader行。
I have tested the SQL Select statement in the SQL Server and it works there. 我已经在SQL Server中测试了SQL Select语句,并且可以在其中运行。 I have read multiple questions related to the error, but since I am a rookie to programming almost all answers are difficult to understand, Thanks in advance for your understanding 我已经阅读了与错误相关的多个问题,但是由于我是编程的新手,因此几乎所有答案都难以理解,在此先感谢您的理解

using (SqlConnection conn = Conexion.Conectado())
{
    string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago = " + Dtp_fecha_cuadre.Text;


    SqlCommand cmd = new SqlCommand(strsql, conn);
    cmd.CommandType = CommandType.Text;
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        string Nombres = dr["Nombres"].ToString();
        string Apellidos = dr["Apellidos"].ToString();
        string num_prestamo = dr["prestamo_id"].ToString();
        DateTime fecha = Convert.ToDateTime(dr["fecha"].ToString());
        double Monto_prestamo = Convert.ToDouble(dr["Monto_prestamo"].ToString());
        string Codigo_ruta = dr["Ruta"].ToString();
        string Quotas = dr["Quotas"].ToString();
        double Balance = Convert.ToDouble(dr["Balance"].ToString());
        double Monto_pago = Convert.ToDouble(dr["Monto_pago"].ToString());
        double Mora = Convert.ToDouble(dr["Mora"].ToString());

        Dgv_cuadre_rutas.Rows.Add(Nombres, Apellidos, num_prestamo, fecha,Monto_prestamo , Codigo_ruta, Quotas, Balance, Monto_pago, Mora);
    }
    conn.Close();
}

Uses SQL DataAdapter instead which is much easier and will get rid of error 使用SQL DataAdapter代替它更容易并且将摆脱错误

            using (SqlConnection conn = Conexion.Conectado())
            {
                string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago = " + Dtp_fecha_cuadre.Text;


                SqlCommand cmd = new SqlCommand(strsql, conn);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(strsql, conn);

                DataTable dt = new DataTable();
                adapter.Fill(dt);

                Dgv_cuadre_rutas.DataSource = dt;

                conn.Close();
            }

I was able to fix the error! 我能够解决该错误! the error occurs first because in my original query I was using " = " + Dtp_fecha_cuadre.Text;" the equal sends the data in an int format, so I had to change it to "--like '" + Dtp_fecha_cuadre.Value.ToString() +"'";,-- but at this point it was not filling up the datagrid, then I came up with the idea that, date string being send was not in the correct format, and adjusted the query to "like '" + Dtp_fecha_cuadre.Value.ToString("yyyy-MM-dd") +"'"; and this solve my problem, thanks all that helped me here – engel 1 min ago edit 该错误首先发生,因为在我的原始查询中,我使用的是“ = = + Dtp_fecha_cuadre.Text;”,该函数以int格式发送数据,因此我不得不将其更改为“ --like'” + Dtp_fecha_cuadre.Value.ToString ()+“'”;,-,但此时还没有填满数据网格,然后我想到了一个想法,即发送的日期字符串格式不正确,并将查询调整为“喜欢' “ + Dtp_fecha_cuadre.Value.ToString(” yyyy-MM-dd“)+”'“;这样就解决了我的问题,感谢所有帮助我的工作–恩格尔1分钟前编辑

 using (SqlConnection conn = Conexion.Conectado())
        {

            string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago like '" + Dtp_fecha_cuadre.Value.ToString("yyyy-MM-dd") +"'";


            SqlCommand cmd = new SqlCommand(strsql, conn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {

                string Nombres = dr["Nombres"].ToString();
                string Apellidos = dr["Apellidos"].ToString();
                string num_prestamo = dr["prestamo_id"].ToString();
                DateTime fecha = Convert.ToDateTime(dr["fecha"].ToString());
                double Monto_prestamo = Convert.ToDouble(dr["Monto_prestamo"].ToString());
                string Codigo_ruta = dr["Ruta"].ToString();
                string Quotas = dr["Quotas"].ToString();
                double Balance = Convert.ToDouble(dr["Balance"].ToString());
                double Monto_pago = Convert.ToDouble(dr["Monto_pago"].ToString());
                double Mora = Convert.ToDouble(dr["Mora"].ToString());


                Dgv_cuadre_rutas.Rows.Add(Nombres, Apellidos, num_prestamo, fecha, Monto_prestamo, Codigo_ruta, Quotas, Balance, Monto_pago, Mora);

            }
            conn.Close();

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

相关问题 错误:日期类型上的“操作数类型冲突:日期与int不兼容” - Error: 'Operand type clash: date is incompatible with int ' on date type 如何修复错误 - 操作数类型冲突:日期与 int 不兼容 - How to fix the error - Operand type clash: date is incompatible with int c#操作数类型冲突:日期与int不兼容 - c# Operand type clash: date is incompatible with int 操作数类型冲突:当我尝试更新数据库时,int与日期不兼容 - Operand type clash: int is incompatible with date when I am trying to update my database 操作数类型冲突:表类型与int不兼容 - Operand type clash: table type is incompatible with int {“操作符类型冲突:int与文本不兼容”} - {“Operand type clash: int is incompatible with text”} 操作数类型碰撞:int与uniqueidentifier不兼容 - Operand type clash: int is incompatible with uniqueidentifier 操作数类型冲突:int 与 DaysAndHours 不兼容 - Operand type clash: int is incompatible with DaysAndHours 操作数类型冲突:int与utblProgramAllotment [用户定义的表类型]不兼容 - operand type clash: int is incompatible with utblProgramAllotment[user defined table type] EF核心更新数据库错误“操作数类型冲突:日期与smallint不兼容” - EF Core Update-Database error “Operand type clash: date is incompatible with smallint”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM