Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从列表中获取值并显示到标签_Sql_Sql Server_Database_List - Fatal编程技术网

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

我正在创建一个基于SQL数据库的搜索引擎

是否可以在列表中获取sql数据,然后在标签上显示数据

这是我的代码:

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告诉我一些关于它的知识