请帮我看看这段代码!

sharesoft21 2003-10-18 10:48:34
我要将查询的结果在dataGrid中显示,代码如下,但是一直出错,把我都搞糊了,请帮我看一下吧!!
另外,我在窗口里面放了一个dataGrid控件,感觉很不方便,我想在查询出结果后动态显示出来应该怎么做呢?

string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =f:\\pic\\boos.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= "+ txt_custname.Text +",pym= " + txt_pym.Text +",compyname=" + txt_compyneme.Text +",lxr = " + txt_lxr.Text +"";
OleDbCommand instr = new OleDbCommand (strsql,myConn);
instr.ExecuteNonQuery ();
OleDbDataAdapter da = new OleDbDataAdapter ();
DataSet ds = new DataSet ();
da.Fill (ds,"custinfo");
//DataGrid dg = new DataGrid ();
dataGrid1.DataSource = ds.Tables ["custinfo"].DefaultView ;
//dataGrid1.DataBind();

...全文
41 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
donger2000 2003-10-19
  • 打赏
  • 举报
回复
那个不关查询的事了!
请设置:
ds.Tables["custinfo"].DefaultView.AllowNew = false;
sharesoft21 2003-10-19
  • 打赏
  • 举报
回复
用第三种方式可以查询记录,这里有一个问题,在没有任何记录的情况下也会显示一个空记录, 这应该怎么来判断呢!
donger2000 2003-10-19
  • 打赏
  • 举报
回复
DetailForm detailForm1= new DetailForm(dr); //假设你有个显示明细信息的FORM,DetailForm 并有DataRow入参用于接收记录
之后,忘了关键的一句:
detailForm1.Show();

要不然就不会显示啦:)
donger2000 2003-10-19
  • 打赏
  • 举报
回复
想实现单击这个里面的一条记录,在一个窗口中详细的显示出来:
你双击DataGrid控件的的MouseUp事件,可以写上它的处理方法,代码大致如下:

private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.Cell)
{

dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
BindingManagerBase bm = this.dataGrid1.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
if(bm.Count==0)
return;
DataRow dr = ((DataRowView)bm.Current).Row; //取得当前行的记录
DetailForm detailForm1= new DetailForm(dr); //假设你有个显示明细信息的FORM,DetailForm 并有DataRow入参用于接收记录
}
}




DetailForm的大致样子:

public class DetailForm : System.Windows.Forms.Form
{

........
........
private DataRow datarow;
public detailForm(DataRow selectedrow)
{
InitializeComponent();
........
datarow=selectedrow;

//你可以有这个类的其他方法中用 datarow["字段名"].ToString() 来取得记录相关信息
........
}
........
........
}
sharesoft21 2003-10-19
  • 打赏
  • 举报
回复
今天真的很感谢donger2000(东东)老兄了 :)
qiuji 2003-10-18
  • 打赏
  • 举报
回复
用LIKE,如果不输入任何条件,显示全部数据。
你可以先判断TextBox是否为空。
if(txt_custname.Text=="" && txt_pym.Text=="" && txt_compyneme.Text=="" && txt_lxr.Text=="")
{
MessageBox.Show("请至少输入一个查询条件!");
}
else
{
//你的查询代码
}
feigehao 2003-10-18
  • 打赏
  • 举报
回复
txt_custname.Text,txt_pym.Text,txt_compyneme.Text,txt_lxr.Text 要分别改为‘txt_custname.Text ’,‘txt_pym.Text’,‘txt_compyneme.Text’,‘txt_lxr.Text’ 记得加‘’号
如你的那个程序应该为string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= "+ “‘”+txt_custname.Text+“’” +",pym= " + “‘”+txt_pym.Text+“’” +",compyname=" +“‘” +txt_compyneme.Text+“’” +",lxr = " +“‘”+ txt_lxr.Text +“’”+"";
donger2000 2003-10-18
  • 打赏
  • 举报
回复
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname like '%"+ txt_custname.Text +"%' OR pym like '%" + txt_pym.Text +"%' OR compyname like '%" + txt_compyneme.Text +"%' OR lxr like '%" + txt_lxr.Text +"%'";

改成:
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname like '%"+ txt_custname.Text +"%' AND pym like '%" + txt_pym.Text +"%' AND compyname like '%" + txt_compyneme.Text +"%' AND lxr like '%" + txt_lxr.Text +"%'";

用了LIKE之后,应该用AND了,否则就有些不合理了,不好意思!我没想仔细!


你说的点击出新窗口这问题有点复杂!不过我可能给你一些示例代码
rgbcn 2003-10-18
  • 打赏
  • 举报
回复
see

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q689q
sharesoft21 2003-10-18
  • 打赏
  • 举报
回复
我用了第三个方案,如果用LIKE的话,不输入任何条件都会显示数据,而且我只查询一个记录 ,但是全部都显示出来了
sharesoft21 2003-10-18
  • 打赏
  • 举报
回复
我现在可以查询了,但在dataGrid中看查询记录不太方便,我想实现单击这个里面的一条记录,在一个窗口中详细的显示出来应该怎么做呢,

我再加100分,谢谢了:)
donger2000 2003-10-18
  • 打赏
  • 举报
回复
qiuji(忆秋季):数值型的,加单引号也是没问题的,不过用like倒是不行了!

他的程序我看应该是WINFORM的,dataGrid1.DataBind();可能不用的!直接用
dataGrid1.DataSource = ds.Tables ["custinfo"].DefaultView ;
就可以绑定了!
qiuji 2003-10-18
  • 打赏
  • 举报
回复
还有,最好去掉:myConn.Open ( ) ;
此处不需要使用。
qiuji 2003-10-18
  • 打赏
  • 举报
回复
1.
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= "+ txt_custname.Text +",pym= " + txt_pym.Text +",compyname=" + txt_compyneme.Text +",lxr = " + txt_lxr.Text +"";
有几处错误,如 rgbcn(rgbcn) 所说,将,改成and
另外,你的custname,pym,compyname,lxr各是什么类型?字符型?
如果全是字符型的话,改成:
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= '"+ txt_custname.Text +"',pym= '" + txt_pym.Text +"',compyname='" + txt_compyneme.Text +"',lxr = '" + txt_lxr.Text +"'";
//注意这里的单引号,如果有数值型的,去掉相关的单引号

2.
OleDbDataAdapter da = new OleDbDataAdapter ();
改成:
OleDbDataAdapter da = new OleDbDataAdapter(strsql,myConn);

3.
//dataGrid1.DataBind();不能去掉?
改成:
dataGrid1.DataBind();

donger2000 2003-10-18
  • 打赏
  • 举报
回复
还有:
OleDbCommand instr = new OleDbCommand (strsql,myConn);
instr.ExecuteNonQuery ();
OleDbDataAdapter da = new OleDbDataAdapter ();

应该用:
OleDbDataAdapter da = new OleDbDataAdapter (strsql,myConn);
sharesoft21 2003-10-18
  • 打赏
  • 举报
回复
谢谢上面的朋友,我尝试后就给你加分哈 :)
donger2000 2003-10-18
  • 打赏
  • 举报
回复
问题有三个:
一是 rgbcn(rgbcn) 说的要用AND 而不是“,”
二是 如果用了AND,是个条件都要满足,这个条件是很严格的!一般情况下应该是找不到记录的!
三是 如果友好一点的话,应该是用模糊查找

string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =f:\\pic\\boos.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;

string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= '"+ txt_custname.Text +"' AND pym= '" + txt_pym.Text +"' AND compyname='" + txt_compyneme.Text +"' AND lxr = '" + txt_lxr.Text +"'"; //这样的话要三个条件都满足,我想应该是不对的!

OleDbDataAdapter da = new OleDbDataAdapter (strsql,myConn);
DataSet ds = new DataSet ();
da.Fill (ds,"custinfo");
dataGrid1.DataSource = ds.Tables ["custinfo"].DefaultView ;

方案二:四个条件中只要一个满足的:
string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =f:\\pic\\boos.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;

string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= '"+ txt_custname.Text +"' OR pym= '" + txt_pym.Text +"' OR compyname='" + txt_compyneme.Text +"' OR lxr = '" + txt_lxr.Text +"'";

OleDbDataAdapter da = new OleDbDataAdapter (strsql,myConn);
DataSet ds = new DataSet ();
da.Fill (ds,"custinfo");
dataGrid1.DataSource = ds.Tables ["custinfo"].DefaultView ;

方案三:四个条件都是模糊查找的, 并且只要一个满足就显示
string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =f:\\pic\\boos.mdb" ;
OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
myConn.Open ( ) ;

string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname like '%"+ txt_custname.Text +"%' OR pym like '%" + txt_pym.Text +"%' OR compyname like '%" + txt_compyneme.Text +"%' OR lxr like '%" + txt_lxr.Text +"%'";

OleDbDataAdapter da = new OleDbDataAdapter (strsql,myConn);
DataSet ds = new DataSet ();
da.Fill (ds,"custinfo");
dataGrid1.DataSource = ds.Tables ["custinfo"].DefaultView ;
sharesoft21 2003-10-18
  • 打赏
  • 举报
回复
我的目的就是在文本框中输入查询的条件,单击"查询"按钮,在一个dataGrid中显示出我的查询结果,我写出以上代码,老是不对。。。。。。
能不能帮我写一段啊! 谢谢
daou101 2003-10-18
  • 打赏
  • 举报
回复
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= "+ txt_custname.Text +",pym= " + txt_pym.Text +",compyname=" + txt_compyneme.Text +",lxr = " + txt_lxr.Text +"";
这个东西有错误,不解你的要求
rgbcn 2003-10-18
  • 打赏
  • 举报
回复
string strsql= "SELECT custname,pym,compyname,lxr FROM custinfo WHERE custname= "+ txt_custname.Text +",pym= " + txt_pym.Text +",compyname=" + txt_compyneme.Text +",lxr = " + txt_lxr.Text +"";

这个sql语句错了 where 后面逗号改成 and

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧