【发布时间】:2011-11-11 08:48:39
【问题描述】:
我对 VB.NET 还很陌生,所以当我遇到错误时我会进行大量研究。问题是我找不到解决方案,或者任何远程处理它。
我正在开发用于连接到远程 mysql 服务器、运行查询并返回数据集的软件。我已经通过 mysql 提示符对服务器运行了这个查询,并成功收到了结果。但是当我尝试从我的应用程序运行这个查询时,我得到一个对象引用错误。
通过调试,我发现用于 sql 字符串中的变量的 @ 字符导致了我的代码中的问题,但是我无法找到一种方法将此查询传递给服务器,而不会出现 Visual Studio 引发错误。我发现的关于变量使用的每个答案都建议在我的代码中明确规定变量值,我不想这样做。
这是我的一些代码和我的查询:
Dim mysqlCon As MySqlConnection = New MySqlConnection("Server=" & arrArgs & ";Database=DBNAME;Uid=USER;Pwd=PASS;allow zero datetime=true;")
Dim mysqlString As String = "set @from_scanner = '" & code & "'; set @code='', @altbarcode='', @qty='1', @price='',@Altprice='0', @date_start='',@date_end='',@disc_price='0',@description='', @altdescription=''; set @from_scanner=if(left(@from_scanner,2)='FF',trim(substring(@from_Scanner,3,13)),trim(substring(@from_Scanner,2,13))); select code, altbarcode,description, qty, price into @code, @altbarcode,@altdescription, @qty, @Altprice from altbar where altbarcode=@from_scanner; set @code=if(@code='',@from_Scanner,@code); select disc_price, date_end into @disc_price, @date_end from plu where (code=trim(left(@code,12)) or def_barcode=@code) and current_date between date_start and date_end and disc_price<>price and disc_price<>0; select price, description into @price, @description from plu where code=trim(left(@code,12)) or def_barcode=@code; set @price=if(ifnull(@disc_price,0)=0 or @disc_price='', @price,@disc_price); set @price=if(@qty>1 and @Altprice > 0, @Altprice, @price*@qty); set @description=if(@altdescription<>'', @Altdescription, @description); select @code, format(@price,2) Price, @description,@date_end;"
Dim mysqlCom As MySqlCommand = New MySqlCommand(mysqlstring, mysqlCon)
Dim mysqlAdapter As New MySqlDataAdapter
Dim mysqlData As New DataSet
mysqlCon.Open()
mysqlAdapter.SelectCommand = mysqlCom
mysqlAdapter.Fill(mysqlData, "tblDBData")
所以我真正需要知道的是如何将此查询传递到 mysql 服务器而不在 VB 中引起问题。
【问题讨论】:
标签: .net mysql vb.net variables ado.net