此功能有效,因为我按用户 ID 搜索:
private void showList_Click(object sender, EventArgs e)
{
int id = 0;
for (int i = 0; i <= sqlClient.Count("UserList"); i++)
{
Dictionary<string, string> dik = sqlClient.Select("UserList", "userid = " + id);
var lines = dik.Select(kv => kv.Key + ": " + kv.Value.ToString());
userList.AppendText(string.Join(Environment.NewLine, lines));
userList.AppendText(Environment.NewLine);
userList.AppendText("--------------------------------------");
id++;
}
}
此功能不起作用,因为我通过电子邮件搜索:
private void login_Click(object sender, EventArgs e)
{
string email = lemail.Text;
Dictionary<string, string> dik = sqlClient.Select("UserList", "firstname = " + email);
var lines = dik.Select(kv => kv.Key + ": " + kv.Value.ToString());
logged.AppendText(string.Join(Environment.NewLine, lines));
}
这是我单击登录按钮时收到的错误消息:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“@aol.com”附近使用正确的语法
我在数据库中搜索的电子邮件是“aces@aol.com”,没有引号。错误消息使我相信@符号引起冲突,因为我知道它是一个特殊字符,但我很难弄清楚要搜索什么短语来帮助我。
另外,这里是被调用的函数:
public Dictionary<string, string> Select(string table, string WHERE)
{
//This methods selects from the database, it retrieves data from it.
//You must make a dictionary to use this since it both saves the column
//and the value. i.e. "age" and "33" so you can easily search for values.
//Example: SELECT * FROM names WHERE name='John Smith'
// This example would retrieve all data about the entry with the name "John Smith"
//Code = Dictionary<string, string> myDictionary = Select("names", "name='John Smith'");
//This code creates a dictionary and fills it with info from the database.
string query = "SELECT * FROM " + table + " WHERE " + WHERE + "";
Dictionary<string, string> selectResult = new Dictionary<string, string>();
if (this.Open())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataReader = cmd.ExecuteReader();
try
{
while (dataReader.Read())
{
for (int i = 0; i < dataReader.FieldCount; i++)
{
selectResult.Add(dataReader.GetName(i).ToString(), dataReader.GetValue(i).ToString());
}
}
dataReader.Close();
}
catch { }
this.Close();
return selectResult;
}
else
{
return selectResult;
}
}
我的数据库表称为“UserList”
按顺序排列的字段如下:
用户名、电子邮件、密码、姓氏、名字
任何帮助将不胜感激。这个网站太棒了!