Foxtable(狐表)用户栏目专家坐堂 → 利用Replace函数简化代码示例....菜鸟来学习.


  共有20415人关注过本帖树形打印复制链接

主题:利用Replace函数简化代码示例....菜鸟来学习.

帅哥哟,离线,有人找我吗?
菜鸟foxtable
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 菜鸟中的老鸟
等级:七尾狐 帖子:1486 积分:10064 威望:0 精华:9 注册:2008/11/14 8:54:00
利用Replace函数简化代码示例....菜鸟来学习.  发帖心情 Post By:2009/1/17 11:23:00 [显示全部帖子]

看看绿色代码,大家可以复制到命令窗口执行,看看如何利用Replace替换字符来简化代码.

Dim kz As String = "TextBox2,TextBox3,ComboBox1,TextBox5,TextBox6,ComboBox2,TextBox7,ComboBox3"
Dim sql As String = "UPDATE [住院登记] SET 住院号='@$10',姓名='@$11',性别='@$12',年龄='@$13',地址='@$14',费用类别='@$15',医农保证号='@$16',入院诊断='@$17' Where 住院ID = '"& Vars("str3") &"'"
dim i as integer
For Each k as string In kz.split(",")
      sql = sql.Replace("@$" & i+10,k)
      i += 1
Next
Output.Show(sql)


-------------------------------------------实例1----------------------------------------------
Dim cmd As New SQLCommand
cmd.C
With e.Form
    If .Controls("TextBox2").Value is Nothing Then
        MsgBox("住院号不能为空!",64,"提示")
    Else If .Controls("TextBox3").Value is Nothing Then
        MsgBox("姓名不能为空!",64,"提示")
    Else If .Controls("ComboBox1").Value is Nothing Then
        MsgBox("性别不能为空!",64,"提示")
    Else If .Controls("ComboBox3").Value is Nothing Then
        MsgBox("入院诊断不能为空!",64,"提示")
    Else
        Dim kz As String = "TextBox2,TextBox3,ComboBox1,TextBox5,TextBox6,ComboBox2,TextBox7,ComboBox3"
        Dim sql As String = "UPDATE [住院登记] SET 住院号='@$10',姓名='@$11',性别='@$12',年龄='@$13',地址='@$14',费用类别='@$15',医农保证号='@$16',入院诊断='@$17' Where 住院ID = '"& Vars("str3") &"'"
        dim i as integer
        For Each k as string In kz.split(",")
            sql = sql.Replace("@$" & i+10,.Controls(k).Value)
            i += 1
        Next

        cmd.CommandText = sql
        cmd.ExecuteNonQuery()
        DataTables("住院登记").Load()
        Dim dst As WinForm.DataList = e.Form.Controls("DataList1")
        cmd.CommandText = "Select 住院ID,住院号,姓名,性别,年龄,费用类别,医农保证号,入院诊断,地址 From [住院登记] Where 出院申请 = False"
        dst.DataTable = cmd.ExecuteReader()
        dst.Build()
    End If
End With
[此贴子已经被作者于2009-1-18 9:40:31编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
菜鸟foxtable
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 菜鸟中的老鸟
等级:七尾狐 帖子:1486 积分:10064 威望:0 精华:9 注册:2008/11/14 8:54:00
  发帖心情 Post By:2009/1/18 9:41:00 [显示全部帖子]

老爹,这样更改还行吧?图片点击可在新窗口打开查看

 回到顶部