我使用以下代码在名为DaisyServicesForm的表单上填充DataGridView
'//Fill Table
GlobalVar.dataadapter2.Fill(GlobalVar.ds2, "Missing")
DaisyServicesForm.DataGridView3.AutoGenerateColumns = False
DaisyServicesForm.DataGridView3.DataSource = GlobalVar.ds2.Tables("Missing")
因为它将数据加载到另一个表单,所以我使用公共类配置了我的变量,以便它们可用。
Public Class GlobalVar
Public Shared Filename2 As String
'//build table on left for missing items
Public Shared ds2 As DataSet = New DataSet()
Public Shared connStr2 As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Public Shared sql2 As String = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.bill From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" & GlobalVar.Filename2 & "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE s.[CLI] is NULL"
Public Shared conn2 As SqlConnection = New SqlConnection(GlobalVar.connStr2)
Public Shared comm2 As SqlCommand = New SqlCommand(GlobalVar.sql2, GlobalVar.conn2)
Public Shared dataadapter2 As SqlDataAdapter = New SqlDataAdapter(GlobalVar.comm2)
End Class
问题是当我运行代码时,使用单击按钮事件失败并显示以下错误消息。
"对象或列名称缺失或为空。对于SELECT INTO语句,请验证每列是否具有名称。对于其他语句,请查找空别名。别名定义为""或[]是不允许的。将别名更改为有效名称。"
看起来Filename变量未正确传递。
我已经通过MsgBox测试了变量的存在,当我对动态表名进行硬编码时,它可以正常工作。
非常感谢任何帮助。
答案 0 :(得分:1)
Shared
个变量。
Public Shared comm2 As SqlCommand = New SqlCommand(GlobalVar.sql2, GlobalVar.conn2)
当FileName2
尚未初始化时,用sql2创建命令。
您可以在公共类中创建一个方法来初始化您的连接,命令和DataAdapter ...
Public Class GlobalVar
Public Shared Filename2 As String
''build table on left for missing items
Public Shared ds2 As DataSet
Public Shared connStr2 As String
Public Shared sql2 As String
Public Shared conn2 As SqlConnection
Public Shared comm2 As SqlCommand
Public Shared dataadapter2 As SqlDataAdapter
Public Shared Sub InitializeConnection()
''Call this after you fill Filename2
ds2 = New DataSet()
connStr2 = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
sql2 = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.bill From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" & GlobalVar.Filename2 & "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i. [Description]=s.[Description] WHERE s.[CLI] is NULL"
conn2 = New SqlConnection(GlobalVar.connStr2)
comm2 = New SqlCommand(GlobalVar.sql2, GlobalVar.conn2)
dataadapter2 = New SqlDataAdapter(GlobalVar.comm2)
End Sub
End Class
... 但是我真的很鼓励你重新考虑你的设计。例如,您可以在公共类中创建一个函数,该函数创建该函数所需的所有连接和适配器,打开和关闭这些连接并返回所需的DataAdapter。类似的东西:
Public Class GlobalVar
''Do not declare anything here...
Public Shared Function FillData(ByVal Filename2 as string) as DataSet
''...instead put inside this function your connections, data adapters, sql sentences,
''and use them to fill and return the dataset with the data you need
End Function
End Class
答案 1 :(得分:0)
您的sql2
变量将在第一次引用GlobalVar
类时初始化,使用当时的Filename2
值Nothing
。Filename2
。对sql2
的未来更改不会影响Sub Main
Console.WriteLine("String1:{0}", Test.String1)
Console.WriteLine("String2:{0}", Test.String2)
Test.String1 = "Goodbye"
Console.WriteLine("String1:{0}", Test.String1)
Console.WriteLine("String2:{0}", Test.String2)
End Sub
' Define other methods and classes here
Public Class Test
Public Shared String1 As String = "Hello"
Public Shared String2 As String = Test.String1 & " World"
End Class
的价值。
作为一个简单的例子,请参阅以下代码(粘贴到LINQPad进行测试):
String1:Hello
String2:Hello World
String1:Goodbye
String2:Hello World
这输出以下内容:
{{1}}