【发布时间】:2016-08-09 20:59:22
【问题描述】:
当我运行 cod 时,我看到了这些错误 SelectCommand 属性在调用“Fill”之前尚未初始化。 关于“adb.Fill(ds1)”
Imports System.Data.Sql
Module ComModule
Public sqlconn As New SqlClient.SqlConnection
Public Sub openconn()
If sqlconn.State = 1 Then sqlconn.Close()
Try
sqlconn.ConnectionString = "Data Source=MRSOFTWARE-PC;Initial Catalog=ComShop;Integrated Security=True"
sqlconn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message, "Not Connection", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
sqlconn.Close()
End
End Try
End Sub
Public Function LastNum(tablename, orderbyfield) As Integer
LastNum = 0
Dim str = "select * from " & tablename & "order by" & orderbyfield
Dim adb As New SqlClient.SqlDataAdapter()
Dim ds1 = New DataSet
adb.Fill(ds1)
Dim DT As DataTable
DT = ds1.Tables(0)
If DT.Rows.Count <> 0 Then
Dim i = DT.Rows.Count - 1
LastNum = Val(DT.Rows(i).Item(0))
End If
End Function
结束模块
TextBox1.Text = Format(LastNum("Customer", "CustomerId") + 1, "c0")
【问题讨论】:
-
Dim adb As New SqlClient.SqlDataAdapter("select * from " & tablename & " order by " & orderbyfield) 但请注意您的输入。这是 sql 注入(以及缺失的空格)的一扇敞开的大门
-
您只需将选择命令放在一个字符串中,您必须以某种方式将其提供给
SqlDataAdapter(构造函数、.SelectCommand 属性等)
标签: sql-server vb.net