数据库中的跟踪错误
本文关键字:错误 跟踪 数据库 | 更新日期: 2023-09-27 18:15:53
我有一个客户端-服务器程序,服务器会检查客户端是否打开了正确的应用程序。如果没有,它将显示一些警告。它应该只打开三个应用程序和正确的一个。我计划将打开的应用程序的数据和用户主机名放入数据库。将有两个表,hostnametable和processtable。示例:
主机名表
hostname_id | hostname
------------------------
1 | Guest1
2 | Guest2
3 | Guest3
可处理
id | processID | processName | processExe | processPath | hostname_id
--------------------------------------------------------------------------
1 | 4332 | C# Basic | adobe.exe | C:/Users/Adm... | 2
2 | 5400 | PHP 101 | ppt.exe | C:/Users/Adm... | 2
3 | 0943 | Notice |notepad.exe | C:/Users/Adm... | 1
4 | 8932 | Java 101 | adobe.exe | C:/Users/Adm... | 3
5 | 7608 | Certific | word.exe | C:/Users/Adm... | 1
6 | 0101 | Aliens | ppt.exe | C:/Users/Adm... | 1
7 | 8200 | Progress | excel.exe | C:/Users/Adm... | 3
一旦客户端打开一个新的应用程序,就会在服务器中发送[hostname、processID、processName、processExe、processPath]。服务器将
-通过获取主机名计数来检查主机名是否存在
-如果是,则它将选择hostname_id和
-将它与其他数据一起插入到新表中。
如果不是
-则在主机名表中插入新的主机名
-它将选择hostname_id
-并插入数据
这是一个一对多的关系,所以在后面的部分中,我需要检索它以进行输出显示和验证。我的问题是,我已经一步一步地调试了代码的数据库部分,但当我将它们作为一个整体运行时,它会在过程的最后发现一个错误。在这里它将自动进入
catch { output.Text += "'nERROR THREE'n "; }
这是我的密码。
using (MySqlConnection conDataBase = new MySqlConnection(constring))
{
try
{
if (exeFile == openedFile)
{
listBox1.ForeColor = System.Drawing.Color.Blue;
listBox1.Items.Add(machineName + " has opened the right file.");
}
else
{
string count1 = "SELECT COUNT(hostName) FROM hostnametable;";
string gethostname_id = "SELECT INTO chtbuster.hostnametable(hostName_id) WHERE hostName=?machineName";
string insert_pt = "INSERT INTO chtbuster.processtable(processID,processFileName,processName,processPath,hostName_id) VALUES (@processID, @fileName, @exeFile, @filePath, " + gethostname_id + ");";
string insert_ht = "INSERT INTO chtbuster.hostnametable(hostName_id) VALUES(@machineName);";
//query for else condition 1 is at the top
listBox1.ForeColor = System.Drawing.Color.Green;
listBox1.Items.Add(machineName + " has opened " + exeFile);
conDataBase.Open();
MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
cmd1Database.ExecuteNonQuery();
Int32 ifexist = (Int32)cmd1Database.ExecuteScalar();
if(ifexist>0)
{
MySqlCommand cmd2Database = new MySqlCommand(gethostname_id, conDataBase);
machineName = (string)cmd1Database.ExecuteScalar();
cmd2Database.Parameters.AddWithValue("?machineName", machineName);
MySqlCommand cmd3Database = new MySqlCommand(insert_pt, conDataBase);
try
{
cmd3Database.Parameters.Clear();
cmd3Database.Parameters.AddWithValue("@processID", processID);
cmd3Database.Parameters.AddWithValue("@machineName", machineName);
cmd3Database.Parameters.AddWithValue("@filename", fileName);
cmd3Database.Parameters.AddWithValue("@exeFile", exeFile);
cmd3Database.Parameters.AddWithValue("@filePath", filePath);
cmd3Database.ExecuteNonQuery();
cmd2Database.ExecuteReader();
}
catch (Exception ex)
{
output.Text += "'n1" + (ex.Message);
}
}
else
{
MySqlCommand cmd4Database = new MySqlCommand(Query, conDataBase);
MySqlCommand cmd5Database = new MySqlCommand(insert_ht, conDataBase);
try
{
cmd5Database.Parameters.Clear();
cmd5Database.Parameters.AddWithValue("@processID", processID);
cmd5Database.Parameters.AddWithValue("@machineName", machineName);
cmd5Database.Parameters.AddWithValue("@filename", fileName);
cmd5Database.Parameters.AddWithValue("@exeFile", exeFile);
cmd5Database.Parameters.AddWithValue("@filePath", filePath);
cmd4Database.Parameters.AddWithValue("@machineName", machineName);
cmd4Database.ExecuteNonQuery();
cmd5Database.ExecuteNonQuery();
conDataBase.Close();
}
catch (Exception ex)
{
output.Text += "'n2" + (ex.Message);
}
}
}
}
catch (Exception ex)
{
output.Text += "'n3" + (ex.Message);
}
}
首先,我认为您仍然有一个逻辑问题:
MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
cmd1Database.ExecuteNonQuery();
Int32 ifexist = (Int32)cmd1Database.ExecuteScalar();
if(ifexist>0)
{
MySqlCommand cmd2Database = new MySqlCommand(gethostname_id, conDataBase);
machineName = (string)cmd1Database.ExecuteScalar();
自第一次运行cmd1Database
以来,您没有更改它的CommandText
属性。因此,它将运行相同的查询,即count1
中指定的查询。
这将返回一个Int32
。这不能直接转换为string
,就像您使用(string)cmd1Database.ExecuteScalar();
一样。
关于"指定的强制转换无效":
现在,我不知道MySqlCommand
的怪癖。但这里引用了文档中关于基类(.NET的System.Data.Common.DbCommand
(的一句话:
如果未找到结果集中第一行的第一列,则返回null引用(Visual Basic中为Nothing(。如果中的值如果数据库为null,则查询返回DBNull。价值
(来源(
根据导致问题的ExecuteScalar
,您可能会面临稍微不同的情况。如果没有返回任何行,则ExecuteScalar
将返回Null
。无法转换为Int32
。你必须这样处理:
int rowCount = 0;
object result = cmd1Database.ExecuteScalar();
if(result != null)
{
rowCount = (Int32) result;
}
如果有数据,但数据库中的值是Null
,则返回的值将是相当特殊的类型DBNull。这不会转换为任何其他类型(Object
除外(。所以你最终会做这样的事情:
object result = cmd1Database.ExecuteScalar();
if(!DBNull.Value.Equals(result))
{
machineName = (string) result;
}