更改格式日期并可以在 vb.net 中用空白或零更新文本框
change format date and can update in textbox with blank or zero in vb.net
我想将文本框中的日期格式更改为“dd-mmm-yy”格式,但在 datagridview 中没有显示日期和时间时它不起作用,而且我无法在文本框中更新空白和零导致错误“数据类型错误”。如果我不在“日期”字段中执行 sql 命令进行更新,则不会导致错误。我用 visual studio 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
你的 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)
将您的日期时间文本框更改为日期时间选择器。不要为数值使用文本框,而是使用 NumericUpDown。
您必须调用AddWithValue("@xxx"...
的顺序与@xxx 在SQL 中出现的顺序相同。 Access 忽略名称并查看参数出现在 SQL 中的顺序,因此按相同顺序调用 AWV 很重要。有一天,当您使用不同的数据库升级 o 时,它将支持参数的名称部分,因此您可以按任何顺序添加您的值,但现在访问时,顺序很重要
你必须像我展示的那样写下你的SQL。永远,永远不要做你以前做过的事情。切勿使用字符串连接将 user-supplied 数据值构建到 SQL 语句中。它使您的程序变得微不足道。当你正在编写一个小应用程序来索引奶奶的记录集合时,这可能无关紧要,但绝不能在任何 system of importance such as one used by many people
上完成
我想将文本框中的日期格式更改为“dd-mmm-yy”格式,但在 datagridview 中没有显示日期和时间时它不起作用,而且我无法在文本框中更新空白和零导致错误“数据类型错误”。如果我不在“日期”字段中执行 sql 命令进行更新,则不会导致错误。我用 visual studio 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
你的 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)
将您的日期时间文本框更改为日期时间选择器。不要为数值使用文本框,而是使用 NumericUpDown。
您必须调用AddWithValue("@xxx"...
的顺序与@xxx 在SQL 中出现的顺序相同。 Access 忽略名称并查看参数出现在 SQL 中的顺序,因此按相同顺序调用 AWV 很重要。有一天,当您使用不同的数据库升级 o 时,它将支持参数的名称部分,因此您可以按任何顺序添加您的值,但现在访问时,顺序很重要
你必须像我展示的那样写下你的SQL。永远,永远不要做你以前做过的事情。切勿使用字符串连接将 user-supplied 数据值构建到 SQL 语句中。它使您的程序变得微不足道。当你正在编写一个小应用程序来索引奶奶的记录集合时,这可能无关紧要,但绝不能在任何 system of importance such as one used by many people
上完成