更改格式日期,并可以在 vb.net 中用空白或零更新文本框

发布于 2025-01-14 03:17:55 字数 3659 浏览 2 评论 0原文

我想将文本框中的日期格式更改为“dd-mmm-yy”格式,但是当在 datagridview 中没有出现日期和时间时它不起作用,而且我无法在文本框中用空白和零进行更新导致错误“数据类型错误”。如果我不执行 sql 命令来更新“日期”字段,则不会导致错误。我用的是视觉工作室2010 所以日期列中的问题,如果我在没有日期列的情况下更新,那么它运行得很好。问题错误:更新语句中的语法错误 谢谢 杰克 表单显示网格视图和文本框错误语法

  Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim cn As String = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    Private connectionString As String
    Private con As OleDbConnection
    Private cmd As OleDbCommand
    Private da As OleDbDataAdapter
    Private sql As String
    Public x As Integer
    Public Sub dbConnection()
        connectionString = CStr(cn)
        con = New OleDbConnection(connectionString)
        con.Open()
    End Sub
    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
        txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
        DateTimePicker1.Value = DataGridView1.Rows(x).Cells(1).Value.ToString()
NumericUpDown1.Value = DataGridView1.Rows(x).Cells(2).Value.ToString()
       
    End Sub
    Private Sub FillDataGridView()
        Try
            Dim query = "select CODE,DTE,QTY FROM EXAMPLE"
            Using con As OleDbConnection = New OleDbConnection(CStr(cn))
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    Using da As New OleDbDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        Me.DataGridView1.DataSource = dt
                    End Using
                End Using
            End Using
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            dbConnection()
            x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
            'sql = "UPDATE EXAMPLE SET QTY=? WHERE CODE=?"
            sql = "UPDATE EXAMPLE SET DTE=?,QTY=? WHERE CODE=?"
            cmd = New OleDbCommand(sql, con)
            cmd.Parameters.Add("DTE", OleDbType.Date)
            cmd.Parameters.Add("QTY", OleDbType.Numeric)
            cmd.Parameters.Add("CODE", OleDbType.VarChar)
            cmd.Parameters("DTE").Value = DateTimePicker1.Value
            cmd.Parameters("QTY").Value = NumericUpDown1.Value
            cmd.Parameters("CODE").Value = txtCODE.Text

            cmd.ExecuteNonQuery()
            MessageBox.Show("Successfully Updated...", "Update")
            con.Close()
            FillDataGridView()
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDataGridView()
    End Sub

更新语句中的语法错误错误转换

I wanted to change the date format in the textbox to the format "dd-mmm-yy" but it did not work when in datagridview did not appear the date with the time and also I could not update in the textbox with blank and zero which caused the error "data type error". if I do not perform sql command for update in the "DATE" field then do not cause error. I use visual studio 2010
so the problem in the date column and if I update without a date column then it runs perfectly. Problem error : syntax error in update statement
thanks
jack
form display gridview and textbox
error syntax

  Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim cn As String = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
    Private connectionString As String
    Private con As OleDbConnection
    Private cmd As OleDbCommand
    Private da As OleDbDataAdapter
    Private sql As String
    Public x As Integer
    Public Sub dbConnection()
        connectionString = CStr(cn)
        con = New OleDbConnection(connectionString)
        con.Open()
    End Sub
    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
        txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
        DateTimePicker1.Value = DataGridView1.Rows(x).Cells(1).Value.ToString()
NumericUpDown1.Value = DataGridView1.Rows(x).Cells(2).Value.ToString()
       
    End Sub
    Private Sub FillDataGridView()
        Try
            Dim query = "select CODE,DTE,QTY FROM EXAMPLE"
            Using con As OleDbConnection = New OleDbConnection(CStr(cn))
                Using cmd As OleDbCommand = New OleDbCommand(query, con)
                    Using da As New OleDbDataAdapter(cmd)
                        Dim dt As DataTable = New DataTable()
                        da.Fill(dt)
                        Me.DataGridView1.DataSource = dt
                    End Using
                End Using
            End Using
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Try
            dbConnection()
            x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
            'sql = "UPDATE EXAMPLE SET QTY=? WHERE CODE=?"
            sql = "UPDATE EXAMPLE SET DTE=?,QTY=? WHERE CODE=?"
            cmd = New OleDbCommand(sql, con)
            cmd.Parameters.Add("DTE", OleDbType.Date)
            cmd.Parameters.Add("QTY", OleDbType.Numeric)
            cmd.Parameters.Add("CODE", OleDbType.VarChar)
            cmd.Parameters("DTE").Value = DateTimePicker1.Value
            cmd.Parameters("QTY").Value = NumericUpDown1.Value
            cmd.Parameters("CODE").Value = txtCODE.Text

            cmd.ExecuteNonQuery()
            MessageBox.Show("Successfully Updated...", "Update")
            con.Close()
            FillDataGridView()
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDataGridView()
    End Sub

syntax error in update statement
error CONVERSION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2025-01-21 03:17:55

您的 SQL 行必须这样写:

sql = "UPDATE EXAMPLE SET DATE=@d,QTY=@q WHERE CODE=@c"

您通过添加更多代码来设置这些 @xxx 的值:

cmd.Parameters.AddWithValue("@d", dtpDATE.Value)
cmd.Parameters.AddWithValue("@q", nudQTY.Value)
cmd.Parameters.AddWithValue("@c", txtCODE.Text)

将您的日期时间 TextBox 更改为日期时间选择器。不要使用文本框来表示数值,而是使用 NumericUpDown。

必须按照@xxx在SQL中出现的顺序调用AddWithValue("@xxx"...。Access会忽略名称并查看参数出现的顺序在 SQL 中,因此在同一订单上调用 AWV 非常重要,有一天,当您使用不同的数据库升级时,它将支持参数的名称部分,因此您可以按任何顺序添加值,但现在在访问时,订单。很重要

你必须像我展示的那样编写 SQL。永远不要像以前那样使用字符串连接来将用户提供的数据值构建到 SQL 语句中,这会使您的程序很容易被侵入。收集,但绝不能在任何系统上进行很重要,例如很多人使用的一个

Your SQL line must be written like:

sql = "UPDATE EXAMPLE SET DATE=@d,QTY=@q WHERE CODE=@c"

You set the values of these @xxx by adding more code:

cmd.Parameters.AddWithValue("@d", dtpDATE.Value)
cmd.Parameters.AddWithValue("@q", nudQTY.Value)
cmd.Parameters.AddWithValue("@c", txtCODE.Text)

Change your date time TextBox to be a datetimepicker instead. Instead of using textboxes for numeric values, use a NumericUpDown instead.

You must call AddWithValue("@xxx"... in the same order as the @xxx appear in the SQL. Access ignores the names and looks at the order Parameters appear in the SQL so it is important to call AWV on the same order. One day when you upgrade o using a different database it will support the name part of the parameter so you can add your values in any order but right now on access, order is important

You must write your SQL like I've shown. Never, ever do what you were doing before. Never use string concatenation to build a user-supplied data value into an SQL statement. It makes your program trivial to hack into. This might not matter while you're writing a tiny app to index grandma's record collection but it must never be done on any system of importance such as one used by many people

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文