Sql 从列表中获取值并显示到标签
我正在创建一个基于SQL数据库的搜索引擎 是否可以在列表中获取sql数据,然后在标签上显示数据 这是我的代码:Sql 从列表中获取值并显示到标签,sql,sql-server,database,list,Sql,Sql Server,Database,List,我正在创建一个基于SQL数据库的搜索引擎 是否可以在列表中获取sql数据,然后在标签上显示数据 这是我的代码: protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(@"Data Source=SHKELQIM\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Secur
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=SHKELQIM\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True");
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT Word FROM Dictionary WHERE Word like'%" + lblsearch.Text + "%'", con);
DataSet ds = new DataSet();
da.Fill(ds, "Dictionary");
List<string> word = new List<string>();
foreach (DataRow row in ds.Tables["Dictionary"].Rows)
{
word.Add(row["Word"].ToString());
}
Label1.Text = word[0].ToString();
}
catch (SqlException ex)
{
Label1.Text = ex.Message;
}
finally
{
con.Close();
}
}
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
SqlConnection con=newsqlconnection(@“数据源=shkelkim\SQLEXPRESS;初始目录=Dictionary;集成安全性=True”);
尝试
{
con.Open();
SqlDataAdapter da=new SqlDataAdapter(“从字典中选择单词,其中单词“%”+lblsearch.Text+“%”,con);
数据集ds=新数据集();
da.填写(ds,“字典”);
列表字=新列表();
foreach(ds.Tables[“Dictionary”].Rows中的数据行)
{
添加(行[“word”].ToString());
}
Label1.Text=word[0].ToString();
}
catch(SqlException-ex)
{
Label1.Text=例如消息;
}
最后
{
con.Close();
}
}
}
}
我在第-Label1.Text=word[0].ToString()行获得ArgumentOutOfRangeException代码>
我不知道为什么,因为那列包含数据
另外,如果需要为每个单词创建新标签,如何将标签定位到所需的位置?在执行任何其他操作之前,您需要阅读、理解并开始使用参数化查询。这是一个sql注入漏洞的教科书式示例。这是一个简单的项目,但我很乐意避免漏洞,在您做任何其他需要阅读、理解和开始使用参数化查询的事情之前,请在@seanlange给我一些关于它的知识。这是一个sql注入漏洞的教科书式示例。这是一个简单的项目,但我很乐意避免漏洞,请在@SeanLange告诉我一些关于它的知识