我一直在寻找有关如何显示表格中的多行或所有行的示例。由于数据库连接和查询字符串,我发现的许多示例使我难以正确执行它。
我使用自己的数据库类和预制查询功能。所以在我的表格中,我有这个:
userInfo = searchBox.Text;
string dateResult = Database.databaseAccess.GetColumnData(HLlib.ValueType.VALUETYPE_STRING, userInfo, "date", "username", userInfo, true);
string ipResult = Database.databaseAccess.GetColumnData(HLlib.ValueType.VALUETYPE_STRING, userInfo, "ip", "username", userInfo, true);
string userResult = Database.databaseAccess.GetColumnData(HLlib.ValueType.VALUETYPE_STRING, userInfo, "username", "username", userInfo, true);
string hwidResult = Database.databaseAccess.GetColumnData(HLlib.ValueType.VALUETYPE_STRING, userInfo, "hwid", "username", userInfo, true);
string pcResult = Database.databaseAccess.GetColumnData(HLlib.ValueType.VALUETYPE_STRING, userInfo, "pcname", "username", userInfo, true);
(userInfo 是一个包含正在搜索的名称(表)的文本框。)
获取该格式数据的函数如下:
public string GetColumnData(ValueType valueType, string tableName, string col, string colTwo, string equalsVal, bool compare)
{
MySqlConnection connection = null;
MySqlDataReader dataReader = null;
string colData = "";
try
{
string query = "";
if (compare)
query = "SELECT `" + col + "` FROM `" + tableName + "` WHERE `" + colTwo + "`='" + equalsVal + "';";
else
query = "SELECT `" + col + "` FROM `" + tableName + "`";
using (connection = new MySqlConnection(Hash.RunDecryption()))
{
using (MySqlCommand command = new MySqlCommand(query, connection))
{
connection.Open();
command.CommandText = query;
using (dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
switch (valueType)
{
case ValueType.VALUETYPE_STRING:
colData = dataReader.GetString(0);
break;
case ValueType.VALUETYPE_INT16:
colData = dataReader.GetInt16(0).ToString();
break;
case ValueType.VALUETYPE_INT32:
colData = dataReader.GetInt32(0).ToString();
break;
case ValueType.VALUETYPE_INT64:
colData = dataReader.GetInt64(0).ToString();
break;
}
}
if (dataReader.NextResult())
{
colData = "";
while (dataReader.Read())
{
switch (valueType)
{
case ValueType.VALUETYPE_STRING:
colData = dataReader.GetString(0);
break;
case ValueType.VALUETYPE_INT16:
colData = dataReader.GetInt16(0).ToString();
break;
case ValueType.VALUETYPE_INT32:
colData = dataReader.GetInt32(0).ToString();
break;
case ValueType.VALUETYPE_INT64:
colData = dataReader.GetInt64(0).ToString();
break;
}
}
}
}
}
}
}
catch
{
System.Windows.Forms.MessageBox.Show("Failed to retrieve data!", "Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
dataReader.Close();
CloseConnection(connection);
}
finally
{
dataReader.Close();
CloseConnection(connection);
}
return colData;
}
现在已经解释了,我要做的是从底部到顶部显示所选表格中的每一行,或按日期排序(一列显示日期/时间)
我已经将我的列添加到 DataGrid,它们是:日期、ip、uid、hid、pname。
与我在数据库表中的格式相同。
谁能通过提供有关如何正确执行此操作的示例来帮助我?