如何将vb.net日期保存到mysql日期而没有注册 [英] how to vb.net date save to mysql date with no 0000-00-00

查看:249
本文介绍了如何将vb.net日期保存到mysql日期而没有注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经杀死脑细胞超过5个小时了. 如何正确地将vb.net中的日期保存到mysql日期,而又不始终获取0000-00-00.

I've been killing brain cell for over 5 hours with this question. how to properly save a date from vb.net to mysql date without getting the 0000-00-00all the time.

我尝试了多种建议,在输入日期之前先格式化日期值,但例如,我得到的结果相同:

I have tried multiple suggestions where you format the date value before entering but it gets me the same result ex:

Dim departDate As Date
Dim d As String = Format(departDate, "05/05/2007")

我在其中测试了但没起作用的我仍然得到了注册信息

in which i tested and didnt works i still got the 0000-00-00

我尝试过:

    Dim changeFormatDate As Date
    Dim AddFechaListadoCasas2 as String
    changeFormatDate = AddCasasListadoDatePicker.Text
    AddFechaListadoCasas2 = changeFormatDate.ToString("yyyy-MM-dd")

这仍然在mysql上为我提供了注册信息

which still gives me on mysql the 0000-00-00

我也尝试过以下示例:

Dim myDate as DateTime = DateTime.ParseExact(x, "yyyy-MM-dd", CultureInfo.CurrentCulture)

尝试保存到mysql仍然给了我注册信息

and trying to save to mysql still gave me the 0000-00-00

在我的程序中,我从日期选择器中获取日期作为文本,然后将其旋转为字符串,然后尝试将其保存到数据库中.如有必要,我希望将其转换为yyyy-MM-dd格式的日期,这对于MySQL是必需的,但是我在它上面所做的所有操作仍然向我显示了令人沮丧的0000-00-00.

in my program im grabbing the date from a datepicker as text then swtich it to a string and try to save it to the database like that. if necessary i would love to convert it to a date with the format yyyy-MM-dd which is necessary for MySQL but everything i've done above it still shows me the 0000-00-00 which is frustrating.

任何帮助将不胜感激.

狮子座

这是我在程序中使用的MySQL插入语句.

here is the MySQL insert statement i have on my program.

AddFechaListadoCasas2 = AddCasasListadoDatePicker.Text
        AddFechaCierreCasas2 = AddFechaCierreCasas.Text


SqlVentaCasas = "INSERT INTO VentaCasas (ID, Direccion, Ciudad_Municipio, Zona_Localidad, Barrio, " & _
                                    "Estatus, Precio, Unidad, Departamento, Conjunto, Estrato, Matricula_Inmobiliaria, Cedula_Catastral," & _
                                    "Escritura, Notaria, Ano, Area_Metros_2, Tipo_de_Construccion, Niveles, Habitaciones, Garajes," & _
                                    "Techo, Vista, Picina, Cocina_Integral, Bar, Cuarto_Bano_Servicio, Antejardin, Equipos," & _
                                    "Aire_Acondicionado, Detalles_addicionales, Estilo, Ano_Construccion, Orientacion, Banos, Parqueos," & _
                                    "Piso, Balcon, Jacuzzi_Sauna, Chimenea, Hall_Television, Zona_lavanderia, Patio, Accesorios," & _
                                    "Calefaccion, Administracion, Amenidades, Impuestos, Condiciones, Ocupacion," & _
                                    "Cuota_Administracion, Restricciones, Seguridad, Mostrar, Comision, Fecha_Listado, " & _
                                    "Agente_Cooperante, Fecha_Expiracion, CiudadEscritura, Agente, Licensia, Compania, NumeroPersonal," & _
                                    "NumeroCompania, NumeroFax, Email, AreaLotesM2, PrecioOriginal, Account_Creator, NumeroDias ) VALUES ('" & _
                                    AddIDCasas2 & "','" & _
                                    AddDireccionCasas2 & "','" & AddCiudadCasas2 & "','" & AddZonaCasas2 & "','" & AddBarrioCasas2 & "','" & _
                                    AddEstatusCasas2 & "'," & AddPrecioCasas2 & ",'" & AddUnidadCasas2 & "','" & AddDepartamentoCasas2 & "','" & _
                                    AddConjuntoCasas2 & "'," & AddEstratoCasas2 & ",'" & AddMatriculaCasas2 & "','" & _
                                    AddCedulaCatastralCasas2 & "','" & AddEscrituraCasas2 & "','" & AddNotariaCasas2 & "'," & _
                                    AddAnoCasas2 & "," & AddMetros2Casas2 & ",'" & AddTipoConstruccionCasas2 & "'," & _
                                    AddNivelesCasas2 & "," & AddHabitacionesCasas2 & "," & AddGarajesCasas2 & ",'" & _
                                    AddTechoCasas2 & "','" & AddVistaCasas2 & "','" & AddPicinaCasas2 & "','" & AddCocinaIntegralCasas2 & "','" & _
                                    AddBarCasas2 & "','" & AddCuartoCasas2 & "','" & AddAnteJardinCasas2 & "','" & AddEquiposCasas2 & "','" & _
                                    AddAireAcondicionadoCasas2 & "','" & AddDetallesAddicionalesCasas2 & "','" & AddEstiloCasas2 & "'," & _
                                    AddAnoConstruccionCasas2 & ",'" & AddOrientacionCasas2 & "'," & AddBanoCasas2 & "," & _
                                    AddParqueoCasas2 & ",'" & AddPisoCasas2 & "','" & AddBalconCasas2 & "','" & AddJacuzziCasas2 & "','" & _
                                    AddChimeneaCasas2 & "','" & AddTVCasas2 & "','" & AddZonaLavanderiaCasas2 & "','" & AddPatioCasas2 & "','" & _
                                    AddAccesoriosCasas2 & "','" & AddCalefaccionCasas2 & "','" & AddAdministracionCasas2 & "','" & _
                                    AddAmenidadesCasas2 & "','" & AddImpuestosCasas2 & "','" & AddCondicionesCasas2 & "','" & _
                                    AddOcupacionCasas2 & "','" & AddCuotaAdministracionCasas2 & "','" & AddRestriccionesCasas2 & "','" & _
                                    AddSeguridadCasas2 & "','" & AddMonstrarCasas2 & "','" & AddComisionCasas2 & "','" & _
                                    AddFechaListadoCasas2 & "','" & AddAgenteCooperanteCasas2 & "','" & _
                                    AddFecdhaExpiracionCasas2 & "','" & _
                                    AddCiudadEscrituraCasas2 & "','" & AgenteNameCasas2 & "','" & _
                                    AgenteLicensiaCasas2 & "','" & AgenteCompaniaCasas2 & "','" & _
                                    AgentePersonalCasas2 & "','" & AgenteTelefonoCasas2 & "','" & _
                                    AgenteFaxCasas2 & "','" & AgenteEmailCasas2 & "'," & AddAreaLotesM2Casas2 & "," & AddPrecioOriginalCasas2 & ", '" & AgenteAccountCreatorCasas2 & "', 0 );"

AddFechaListadoCasas2和AddFecdhaExpiracionCasas2是包含要在MySQL上输入日期的字符串

AddFechaListadoCasas2 and AddFecdhaExpiracionCasas2 are Strings that contains the date i want to input on MySQL

推荐答案

您当前的查询容易受到SQL Injection的攻击.请使用带有参数的ADONet Object,例如ConnectionCommand.更具体地说,由于使用的是MySQL,请使用MySQL Data provider.它将自动为您转换数据类型.

Your current query is vulnerable with SQL Injection. Please make use of ADONet Object such as Connection and Command with parameters. To be more specific, since you are using MySQL, use MySQL Data provider. It will automatically cast the data types for you.

Dim query As String = "INSERT INTO tableName (dateColumn) VALUES (@dateParam)"
Using conn As New MySqlConnection("connectionstringHere")
    Using comm As New MySqlCommand()
        With comm
            .Connection = conn
            .CommandText = query
            .CommandType = Commandtype.Text
            ' Assuming this is a DatePicker '
            .Parameters.AddWithValue("@dateParam", AddCasasListadoDatePicker.Value) 
        End With
        Try
            conn.Open()
            comm.ExecuteNonQuery()
        Catch(ex as MySqlException)
            Msgbox(ex.Message.ToString())
        End Try
    End Using
End Using

这篇关于如何将vb.net日期保存到mysql日期而没有注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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