如果不为空则更新文本框 [英] update textbox if not empty
问题描述
我想要将文本框的更新值更改为ms访问数据库,但我不想在文本框空白时进行任何操作
这个我的代码
Dim SavInto As 新 OleDb.OleDbCommand
Dim adapter2 As 新 OleDbDataAdapter(SQLstr,Conn)
SavInto.Connection = Conn
SavInto.CommandType = CommandType.Text
SavInto.CommandText = UPDATE Table1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox1.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox2.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox3.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox4.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox5.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox6.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox7.Text)& '
SavInto.CommandText = 更新表1 SET Place ='&修剪(ComboBox2.SelectedItem)& 'WHERE FilNum ='& (TextBox8.Text)& '
Conn.Open()
SavInto.ExecuteNonQuery()
Conn.Close()
i想要今天完成任何帮助吗?
< blockquote>只需对文本框的内容进行测试即可包围CommandText位...
如果修剪(TextBox8.Text)。长度> 0然后
SavInto.CommandText =UPDATE Table1 SET Place ='&修剪(ComboBox2.SelectedItem)& '在哪里FilNum ='& (TextBox8.Text)& '
结束如果
请注意,当你覆盖 SavInto.CommandText时,实际上只会执行你的最后一个命令
每次。
最后,对SQL注入做一些研究,并用适当的参数替换字符串连接... http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand .parameters.aspx [ ^ ]
Dim tbList As StringBuilder = 新 StringBuilder( ()' 比字符串连接更有效
For 每个 t As Control 在 我 .GroupBox1.Controls ' 我将所有相关的TextBox放入一个(不可见的)组框
如果 TypeOf (t) TextBox 然后
如果修剪(t.Text).Length> 0 然后 ' 仅对内容的文本框感兴趣
tbList.Append(修剪( '& t.Text)& ',) 建立文本框内容列表
结束 如果
结束 如果
下一步
如果 tbList.Length> 1 然后 ' 有效检查所有空白框
Dim cmdList As 字符串 = tbList.ToString()
如果 cmdList.EndsWith( ,)然后 cmdList = Strings.Left(cmdList, cmdList.Length - 1 )' 删除最后一个逗号
cmdList + = ) ' 将结束括号放在适当位置
SavInto.CommandText = UPDATE Table1 SET Place =?在哪里FilNum& cmdList
SavInto.Parameters.Add(ComboBox2.SelectedItem)' 使用参数来避免SQL注入
Conn.Open()
SavInto.ExecuteNonQuery()' 执行单个查询
Conn.Close()
结束 如果跨度>
I want update value of textbox to ms access db but i dont want make any action if textbox left empty
this my code
Dim SavInto As New OleDb.OleDbCommand
Dim adapter2 As New OleDbDataAdapter(SQLstr, Conn)
SavInto.Connection = Conn
SavInto.CommandType = CommandType.Text
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox1.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox2.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox3.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox4.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox5.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox6.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox7.Text) & "'"
SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox8.Text) & "'"
Conn.Open()
SavInto.ExecuteNonQuery()
Conn.Close()
i want finish it today maximum any help?
Just surround the CommandText bits with a test on the contents of the text box ...
If Trim(TextBox8.Text).Length > 0 Then SavInto.CommandText = "UPDATE Table1 SET Place = '" & Trim(ComboBox2.SelectedItem) & "' WHERE FilNum ='" & (TextBox8.Text) & "'" End If
Note that only your last command will actually be executed as you're overwritingSavInto.CommandText
each time.
Finally, do some research on SQL Injection and replace the string concatenation with proper parameters ... http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx[^]
[Edit - an alternative approach that is extensible, avoids SQL injection and uses a single update statement]
Dim tbList As StringBuilder = New StringBuilder("(") 'More efficient than string concatenation For Each t As Control In Me.GroupBox1.Controls 'I placed all the relevent TextBoxes into an (invisible) group box If TypeOf (t) Is TextBox Then If Trim(t.Text).Length > 0 Then 'Only interested in textboxes with content tbList.Append(Trim("'" & t.Text) & "',") 'Build up a list of the textbox contents End If End If Next If tbList.Length > 1 Then 'Effectively checks for all boxes being empty Dim cmdList As String = tbList.ToString() If cmdList.EndsWith(",") Then cmdList = Strings.Left(cmdList, cmdList.Length - 1) 'Remove last comma cmdList += ")" 'Put the closing bracket in place SavInto.CommandText = "UPDATE Table1 SET Place = ? WHERE FilNum in " & cmdList SavInto.Parameters.Add(ComboBox2.SelectedItem) 'Use parameters to avoid SQL Injection Conn.Open() SavInto.ExecuteNonQuery() 'Execute the single query Conn.Close() End If
这篇关于如果不为空则更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!