Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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
C# 我在搜索代码中遇到问题帮助我_C# - Fatal编程技术网

C# 我在搜索代码中遇到问题帮助我

C# 我在搜索代码中遇到问题帮助我,c#,C#,在我搜索数据时显示语法错误 string sql = "Select Name,Father_name,NIC_No,Image from Admform WHERE Member_ID=" + textBoxmember.Text + ""; if (cn.State != ConnectionState.Open) cn.Open(); command = new SqlCommand(sql, cn); SqlD

在我搜索数据时显示语法错误

   string sql = "Select Name,Father_name,NIC_No,Image from Admform WHERE Member_ID=" + textBoxmember.Text + "";
        if (cn.State != ConnectionState.Open)
            cn.Open();
        command = new SqlCommand(sql, cn);
        SqlDataReader reader = command.ExecuteReader();
        reader.Read();
        if (reader.HasRows)
        {
            textBoxname.Text = reader[0].ToString();
            textBoxfname.Text = reader[1].ToString();
            textBoxnic.Text = reader[2].ToString();
            byte[] img = (byte[])(reader[3]);
            if (img == null)
                pictureBox1.Image = null;
            else
            {
                MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms);
            }
        }
        else
        {
            MessageBox.Show("This is does not exist.");
        }
        cn.Close();

首先,您应该使用参数化查询,这样您的查询就变得

string sql = "Select Name,Father_name,NIC_No,Image from Admform WHERE Member_ID=@memid";.

using (SqlConnection connection = new SqlConnection(/* connection info */))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        var memidParam = new SqlParameter("memid", SqlDbType.Int);
        memidParam.Value = textBoxmember.Text;

        command.Parameters.Add(memidParam);
        var results = command.ExecuteReader();
        reader.Read();
        if (reader.HasRows)
        {
            textBoxname.Text = reader[0].ToString();
            textBoxfname.Text = reader[1].ToString();
            textBoxnic.Text = reader[2].ToString();
            byte[] img = (byte[])(reader[3]);
            if (img == null)
                pictureBox1.Image = null;
            else
            {
                MemoryStream ms = new MemoryStream(img);
                pictureBox1.Image = Image.FromStream(ms);
            }
        }
        else
        {
            MessageBox.Show("This is does not exist.");
        }
    }
}
或者简单地使用Parameters.AddWithValue


使用将帮助您在代码块完成后自动销毁或处置。因此需要手动关闭或处理对象。

让我总结一下问题,列成员ID可能不是整数,您正在尝试传递不带引号的文本,如果要传递任何字符串值,则必须将其包含在一对“.”中。但事实上,这可以看作是一种错误的方法,因为它为攻击者打开了一扇广阔的大门。因此,我强烈建议您使用如下参数化查询:

string querySql = "Select Name,Father_name,NIC_No,Image from Admform WHERE Member_ID=@memid";.

using (SqlConnection conSql = new SqlConnection("ConnectionString"))
{
    using (SqlCommand command = new SqlCommand(querySql , conSql ))
    {
        conSql.Open();
        command.Parameters.AddWithValue("@memid", textBoxmember.Text);
        SqlDataReader reader= command.ExecuteReader();
        while(reader.Read())
        {
              // Access your values here
        }  
    }
}

欢迎来到SO。简要介绍一下您面临的问题将有助于我们了解您的问题。你有错误吗?或意外输出?其附近出现语法错误“=”…搜索数据时出现此问题…执行problem@AhadAli:如果SQL查询中存在语法错误,则应检查SQL查询。因为它目前对SQL注入非常开放,所以您可以在运行时执行任何操作。m在“=”附近出现语法错误@AhadAli:要求您演示如何在应用程序中实现搜索功能对于堆栈溢出问题来说太广泛了。专注于解决手头的错误。下面给出的答案应该非常有用,因为它们纠正了您的SQL注入问题,这很可能是导致错误的原因。否则,请调试代码以了解有关错误的更多信息。具体来说,请检查您实际运行的导致语法错误的SQL查询。Reader需要打开且可用的SqlConnection,但您忘记打开ConnectionTanks@un-lucky。编辑。你总是一个储蓄者。酷。。。。我们都在这里互相帮助。Happy coding你在哪里用代码@Mohithrivastava打开了sqldatareader?你能建议我如何给出datetimepiker的参数吗?就像我们给出的文本框文本框一样,text=reader[0]。tostring???是的,它不是整数:…你能建议我如何给出datetimepiker的参数吗…就像我们给出的文本框文本框一样,text=reader[0]。tostring???您的代码工作正常,谢谢
string querySql = "Select Name,Father_name,NIC_No,Image from Admform WHERE Member_ID=@memid";.

using (SqlConnection conSql = new SqlConnection("ConnectionString"))
{
    using (SqlCommand command = new SqlCommand(querySql , conSql ))
    {
        conSql.Open();
        command.Parameters.AddWithValue("@memid", textBoxmember.Text);
        SqlDataReader reader= command.ExecuteReader();
        while(reader.Read())
        {
              // Access your values here
        }  
    }
}