将访问代码更改为sql server

时间:2018-05-15 08:14:22

标签: sql-server vb.net tsql ms-access

随机化记录的这段代码非常适合访问数据库..但是当我尝试在Sql server上更改数据库时,我有这个错误

  

'时间'不是公认的内置函数名称

我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles                                  Button1.Click
    DataBase_Connection()
    Dim rows As Integer
    If Not Integer.TryParse(TextBox1.Text, rows) Then
        MsgBox("DECIMAL NUMBER NOT AVAILABLE", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "ERROR")
        TextBox1.Text = ""
        TextBox1.Focus()
        Exit Sub
    End If
    Dim sql As String = String.Format("SELECT Top {0} ID, First_Name, Last_Name FROM FACTORY ORDER BY RND(-(100000*ID)*Time())", rows)

    InfoCommand = New SqlCommand(sql, Con)
    InfoAdapter = New SqlDataAdapter()
    InfoAdapter.SelectCommand = InfoCommand
    InfoTable = New DataTable()
    InfoAdapter.Fill(InfoTable)
    DataGridView1.DataSource = InfoTable
End Sub

我尝试使用time更改sql查询中的单词convert(time(0),getDate()),但遗憾的是不是最佳结果

2 个答案:

答案 0 :(得分:1)

尝试NEWID()

Dim sql As String = String.Format("SELECT Top {0} ID, First_Name, Last_Name FROM FACTORY ORDER BY NEWID()", rows)

答案 1 :(得分:0)

'Time'不是sql server中的内置函数,请尝试以下。

SELECT Top 100 ID, First_Name, Last_Name FROM FACTORY ORDER BY NEWID()