【发布时间】:2014-09-09 13:26:46
【问题描述】:
我在尝试编写这个程序时需要帮助。在这方面我完全是个菜鸟,所以请原谅我的缺点,但我正在尝试创建一个搜索功能,它会在您写下第一个字母后立即从数据库中完成名称。我已经成功地做到了,现在我也必须从同一个数据库中调出图片,我遇到了一些错误。你能看看我的代码并告诉我有什么问题吗?这也够了吗?
注意:我的目标是在他们写下名字时显示图片。
void showpic(string queryStr)
{
SqlConnection conn = new SqlConnection(@"server adress");
conn.Open();
SqlCommand execute = new SqlCommand("SELECT Pernr from View_PhoneBook where DisplayName=" + textBox1.Text, conn);
try
{
StringCollection View_Phonebook = new StringCollection();
SqlDataReader reader = execute.ExecuteReader();
while (reader.Read())
{
View_Phonebook.Add(reader.GetString(0));
}
pictureBox1.ImageLocation.Equals("url" + View_Phonebook + ".jpg");
}
catch (Exception ex)
{
}
conn.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
showpic(textBox1.Text);
}
【问题讨论】:
-
通过使用此语句'new SqlCommand("SELECT Pernr from View_PhoneBook where DisplayName=" + textBox1.Text',您将让自己对 SQL 注入开放。您应该使用 SQLCommand 参数。更多信息: msdn.microsoft.com/en-us/library/…
-
你得到什么错误?
-
当我尝试在搜索的文本框中输入名称时,我得到“没有名为“x”的列”。