注册 登录
编程论坛 ASP.NET技术论坛

[求助] 查询思想是不是出现问题?

bavfhpdn66 发布于 2007-04-24 02:58, 516 次点击

请教高人一下:我下面这段用来“查询”的代码
为什么不是我想要结果!
难道是我的编程思想出了故障?
protected void btnselect_Click(object sender, EventArgs e)
{
string condition = "";//定义一个变量,用来查询的条件;
if (this.chkID.Checked)//通过数据表的ID来查询
{
if (this.chkID.Text == "")
{
condition = "pID like '%'";
}
else
{
condition =Convert.ToString("pID" ==this.txtID.Text);//把我想要查询ID添在“textbox”控件里且“checkbox”为真
}
}
else
{
condition = "pID like '%'";
}
if (this.chkname.Checked)
{
condition += " and personName like '%" + this.txtname.Text + "%'";
}
if (this.chksex.Checked)
{
if (this.rBtnNan.Checked)
{
condition += " and personSex='男'";
}
else
{
condition += " and personSex='女'";
}
}
DataView dv = new DataView(personoperat.selectAllPerson());
dv.RowFilter = condition;
dv.Sort = "pID Desc";
this.GridView1.DataSource = dv;
this.GridView1.DataBind();

}
}
谢谢!

8 回复
#2
cyyu_ryh2007-04-24 11:44
没SQL语句,这样也可以查询吗
#3
bavfhpdn662007-04-24 12:39

呵呵
有啊
这是其中一部

下面这个就是操作类代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// personoperat 的摘要说明
/// </summary>
public class personoperat
{
public personoperat()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection creatercon()
{return new SqlConnection("server=.;database=adonettest;uid=sa;pwd=;");
}
public static bool findPerson(string pID)
{
SqlConnection con = personoperat.creatercon();
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from person where pID='"+pID+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{return true;
}
else
{return false;
}
}
public static DataTable selectAllPerson()
{
SqlConnection con = personoperat.creatercon();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from person", con);
DataSet ds = new DataSet();
sda.Fill(ds, "person");
return ds.Tables["person"];
}
public static bool insertOperate(person p)
{ try
{
SqlConnection con = personoperat.creatercon();
con.Open();
SqlCommand cmd= new SqlCommand("insert into person values(@pID,@pName,@pSex)",con);
SqlParameter para=new SqlParameter("@pID",SqlDbType.VarChar,10);
para.Value=p.pID;
cmd.Parameters.Add(para);
para=new SqlParameter("@pName",SqlDbType.VarChar,20);
para.Value=p.pName;
cmd.Parameters.Add(para);
para=new SqlParameter("@pSex",SqlDbType.VarChar,2);
para.Value=p.pSex;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
return true;
}
catch(Exception e)
{return false;
}

}
public static bool updataOperate(person p)
{
try
{ SqlConnection con=personoperat.creatercon();
con.Open();
SqlCommand cmd=new SqlCommand("updata person set personName='"+p.pName+"',personSex='"+p.pSex+"' where pID='"+p.pID+"'",con);
cmd.ExecuteNonQuery();
return true;
}
catch(Exception e)
{return false;
}
}
public static bool delOperate(string pID)
{
try
{
SqlConnection con = personoperat.creatercon();
con.Open();
SqlCommand cmd = new SqlCommand("delect from person where pID='" + pID + "'", con);
cmd.ExecuteNonQuery();
return true;
}
catch(Exception e)
{
return false;
}
}
}

#4
bygg2007-04-24 12:54

定义的时候加个 condition = "1=1"试试.

#5
bavfhpdn662007-04-24 12:58

还是不行
郁闷

#6
cyyu_ryh2007-04-24 13:05
不知道是不是condition 在连接SQL语句的时候出了错
#7
爱编程的小猪2007-04-24 17:01

public SqlConnection openCon()
{
DBConnect = System.Configuration.ConfigurationSettings.AppSettings["ConnectString"];
con = new SqlConnection(DBConnect);
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}

return con;
}
public void closeCon()
{
try
{
if (openCon().State== ConnectionState.Open)
{
con.Close();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
--------------------------------------------------------------------------
public DataSet likeDs(String str)
{
Class_Connect mycon=new Class_Connect();
con=mycon.openCon();
SqlCommand cmd=new SqlCommand("select_like",con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@Name",SqlDbType.NVarChar);
cmd.Parameters["@Name"].Value=str;
cmd.Parameters.Add("@Rowcount", SqlDbType.Int);
cmd.Parameters["@Rowcount"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
if (ds != null)
{
sda.Fill(ds, "batle02");
}
return ds;
}
-------------------------------------------------------------------------------
ALTER PROCEDURE select_like
@Name Nvarchar(40)=null,
@Rowcount INT=null OUTPUT
AS
begin
select 产品名称,单位数量,单价,库存量,中止 from 产品 where 产品名称 like '%'+@Name+'%' SET @Rowcount=@@ROWCOUNT
end


-------------------------------------------------------------------------------
protected void Button1_Click(object sender, EventArgs e)
{
string str = null;
str = this.TextBox1.Text.ToString();
Class_Select slt = new Class_Select();
ds = slt.likeDs(str);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
Class_Connect mycon = new Class_Connect();
mycon.closeCon();

}

#8
爱编程的小猪2007-04-24 17:03
功能和你类似的呵呵。
#9
bavfhpdn662007-04-24 19:24

呵呵
谢谢楼上的“爱编程的小猪”
你的QQ号码多少?

1