vb.net 教程 2-7 流程控制:另类的判断语句IIF

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
个人觉得 IIF 是比较另类的判断语句

变量=IIF(判断语句,值1,值2)

说明一下:

如果 判断语句结果为真,则返回值1;否则返回值2。

类似于下面的If语句:

IF 判断语句=True Then 变量=值1 Else 变量=值2

看以下代码:

    Sub Main()
        Dim i As Integer
        Console.WriteLine("请输入一个数字")
        i = CType(Console.ReadLine, Integer)

        Dim outString As String
        outString = IIf(i Mod 2 = 0, "这是一个偶数", "这是一个奇数")
        Console.WriteLine(outString)

        Console.ReadKey()
    End Sub

运行如下:

 

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

 

 

 

展开阅读全文

VB.NET中UPDATE语句执行后没有报错,数据库也没有更新

07-04
编译环境:VS2010 Ultimate+SQL Server 2008 VB.NET中UPDATE语句执行后没有报错,数据库也没有更新,但是把语句放在Textbox中提出来,在SQL Server中直接执行是成功的 与数据库是可以连接的,在其他窗体中也可以执行UPDATE语句、INSERT语句,SELECT语句 ``` Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim con As New SqlConnection(connectstring) Dim sqlcmd As New SqlCommand Dim record As Integer Dim i As Integer Dim MEDSID As String Dim DPQTY As String Dim DPTIME As String Dim MGRID As String DPTIME = Now.ToString("yyyy/mm/dd hh:mm:ss") MGRID = login.user1 Try con.Open() For i = 0 To ds.Tables(0).Rows.Count - 1 MEDSID = ds.Tables(0).Rows(i).Item("药品ID") DPQTY = ds.Tables(0).Rows(i).Item("药品数量") txtsql = "UPDATE dbo.MEDS" & vbCrLf & " SET STOCK=STOCK-" & DPQTY & vbCrLf & " WHERE MEDSID='" & MEDSID & "'" TextBox1.Text = txtsql 'sqlcmd = New SqlCommand(updatetxt, con) record = executesql(txtsql, 3) MsgBox(Str(record)) Next da.UpdateCommand = New SqlCommand("UPDATE dbo.LIST_ORDER SET MGRID='" & MGRID & "', STATUS='已发货' & DPTIME='" & DPTIME & "' WHERE ORDERNUMBER='" & ORDERNUMBER & "'", con) Catch ex As Exception MsgBox("NO") Exit Sub End Try End Sub ``` executesql函数的代码如下: ``` Imports System.Data.SqlClient Module Module1 Public Function connectstring() As String connectstring = "Data Source=(local);Initial Catalog=PMIS;Persist Security Info=True;User ID=sa;Password=000000" End Function Public DBSET As DataSet Public txtsql As String Public Function executesql(ByVal strsql As String, ByRef errmsg As String) As Integer Dim cnn As SqlClient.SqlConnection Dim cmd As New SqlClient.SqlCommand() Dim adpt As SqlClient.SqlDataAdapter Dim rst As New DataSet Dim splitsql() As String errmsg = "" Try splitsql = Split(strsql) cnn = New SqlClient.SqlConnection(connectstring) If InStr("INSERT,DELETE,UDATE", UCase(splitsql(0))) Then cmd.Connection = cnn cmd.Connection.Open() cmd.CommandText = strsql executesql = cmd.ExecuteNonQuery() Else adpt = New SqlClient.SqlDataAdapter(strsql, cnn) adpt.Fill(rst) executesql = rst.Tables(0).Rows.Count DBSET = rst End If Catch ex As Exception errmsg = ex.Message executesql = -1 Finally rst = Nothing cnn = Nothing End Try End Function End Module ```
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值