asp.net初学者,用户代码未处理获取错误sql异常.请帮助 [英] asp.net beginner, getting error sql exception was unhandled by user code. plz help

查看:84
本文介绍了asp.net初学者,用户代码未处理获取错误sql异常.请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != null)
        {
            String connectionString = "Data Source=mycomputer;Initial Catalog=master;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand("select * from listofjobs where jobs like '%'" + TextBox1.Text, conn);
            conn.Open();
            command.Connection = conn;
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            GridView1.DataSource = reader;
            GridView1.DataBind();
            GridView1.Visible = true;
        }

       
       }
}



并得到以下错误
{文员"附近的语法不正确."}
15级
错误代码-2146232060
102号
行号1



and getting the following error
{"Incorrect syntax near ''Clerk''."}
class-15
error code-2146232060
number-102
line number 1

推荐答案

尝试以下操作:
Try this:
SqlCommand command = new SqlCommand("select * from listofjobs where jobs like '%'" + "'" + TextBox1.Text + "'", conn);


尝试
SqlCommand command = new SqlCommand("select * from listofjobs where jobs like ''%" + TextBox1.Text + "''", conn);


您也可以执行以下操作:

You can do somthing like this too:

string job = TextBox1.Text;
string query = String.Format("select * from listofjobs where jobs like '%{0}'", job);
SqlCommand command = new SqlCommand(query, conn);



将变量作业"中的值分配给字符串查询"中的索引{0}.


但是Prasad_Kulkarni回答了您的问题,我只是个建议.



Where the value in variable "job"is assigned to the index {0} in the string "query".


But Prasad_Kulkarni answer your question, mine is just a sugestion.


这篇关于asp.net初学者,用户代码未处理获取错误sql异常.请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆