0

我正在尝试使用此代码访问 Web 服务。我还添加了一个带有文本框控件的默认页面,但在运行时只有文本框可见。我还在网络配置中添加了一个具有正确路径的数据库,但它仍然无法正常工作。请帮帮我。我正在尝试从我的机​​器 IP 地址访问它,但它返回错误。有必要安装吗?

public static List<string> getinfo(string prefixText)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
        conn.Open();
        SqlCommand cmd = new SqlCommand("select*from raj where firstname like +'%'", conn);
        cmd.Parameters.AddWithValue("@firstname", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> firstname = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            firstname.Add(dt.Rows[i][0].ToString());
        }
        return firstname;
    }

运行时它不会返回任何错误。

4

1 回答 1

0

你错过@firstname了命令

SqlCommand cmd = new SqlCommand("select*from raj where firstname like '%'+@firstname
+'%'", conn); 

或者第二种方式..

SqlCommand cmd = new SqlCommand("select*from raj where firstname like @firstname", conn);         
cmd.Parameters.AddWithValue("@firstname", "%"+prefixText+"%"); 

编辑:- 试试这个代码..使用 SqlReader....

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
     conn.Open();
SqlCommand cmd = new SqlCommand("select*from raj where firstname like  @firstname", conn);
     cmd.Parameters.AddWithValue("@firstname","%"+prefixText+"%");        
     List<string> firstname = new List<string>();


     using (SqlDataReader reader = cmd.ExecuteReader())
     {
         while (reader.Read())
         {
            firstname.Add(reader[0].ToString());
         }             
         reader.Close();
     }
     connection.Close(); 
     return firstname; 
于 2012-09-21T07:34:58.040 回答