我正在编写一些脚本来读取Intouch环境下的sql查询结果,它不是C#语言,而是类似的语言。我只想得到存储在"SQLTest“变量中的"1”(定义为字符串数据类型)。
这是我的密码:
Dim objDB As System.Data.SqlClient.SqlConnection;
Dim objCmd As System.Data.SqlClient.SqlCommand;
Dim objDR As System.Data.SqlClient.SqlDataReader;
Dim objTbl As System.Data.DataTable;
Dim sDBConnStr As String;
Dim sSQL As String;
Dim bOk As Boolean;
sDBConnStr = "Server=Desktop-3J641FK;Database=Runtime;Integrated Security=True;";
'' Connect and open the database
objDB = New System.Data.SqlClient.SqlConnection(sDBConnStr);
objDB.Open();
sSQL = "SELECT sum (case when EventLogKey = '5' and DetectDateTime between '2022-07-21 11:00:20' and '2022-07-25 11:00:20' then 1 else 0 end) FROM [Runtime].[dbo].[EventHistory]";
'' Invoke the SQL command
objCmd = New System.Data.SqlClient.SqlCommand(sSQL, objDB);
'' Retrieve the queried fields from the query into the reader
objDR = objCmd.ExecuteReader();
InTouch:SQLTesting = objDR.Read();
while objDR.Read() == true
InTouch:SQLTest = objDR.GetValue(0).ToString;
endwhile;
objDR.Close();
objDB.Dispose();
objCmd.Dispose();发布于 2022-07-28 05:42:02
InTouch:SQLTesting = objDR.Read();
while objDR.Read() == true您要给Read打两次电话,那么您预期会发生什么呢?
Read。Read语句调用if。Read循环调用while。做一个,也是唯一一个以上。如果可能有多个行,如果没有行,您想要做一些不同的事情,那么首先使用HasRows属性,然后使用while循环。
尽管如此,如果结果集中只有一个值,那么您应该调用ExecuteScalar,因此数据读取器与此无关:
InTouch:SQLTest = objCmd.ExecuteScalar().ToString();发布于 2022-07-28 05:41:18
我认为您的While循环在这里是不必要的,因为您已经在While之前使用了sqldata读取器,并且您在While中使用的是Sum,它将始终返回一个值。试试这个:
InTouch:SQLTesting = objDR.Read();
InTouch:SQLTest = objDR.GetValue(0).ToString;https://stackoverflow.com/questions/73147481
复制相似问题