加急帖!C#连接数据库查询的问题! 我建了个数据库,用C#连接上了,现在想实现简单的在TEXTBOX里输入值,按BUTTON进行查询,然后返回所需要的行,请各位江湖仁人志士帮帮忙,能留下QQ的最好,时间紧迫!感激不尽!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 补充说明,我用DATASET访问表,DATAGRIDVIEW显示返回的信息。我想在textbox里输入姓名,然后查询表中的“姓名”列,符合的就输出就行了,请帮我写出代码即可。表有50行。 需要两页 一页提交查询 一页显示结果总的原则是将 select * from ** where ** 语句分开 来查询一般情况使用 switch() {case break;case break;...... default break;} 来判断查询类型,textbox里面的值使用类似 and ** like '%"+ this.TextBox1.Text +"%' "; 这样的语句判断 到显示页面后 组成一组完整的查询语句 来显示就可以了.也不知道对你有没有帮助.. :) //库名DBCheck//表名usersstring search = TextBox1.Text.Trim();SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");string sql = "select name from users where name like'%"+search+"%'";SqlDataAdapter adap = new SqlDataAdapter(sql,conn);DataSet ds = new DataSet();adap.Fill(ds);if(ds.Tables[0].Rows.Count > 0){ //返回所要的记录 //GridView1.DataSource = ds.Tables[0].DefaultView;} 哥们你可以这样:string selectSql="select * from table where";if(this.textBox.text!=""){ selectSql+="name='"+textBox.text.Trim().ToString()+"'";}else{ MessageBox.Show("请输入正确的查询条件!","提示消息",MessageBoxButtons.OK,MessageBoxIcon.Information);}try{ using(conn= new OracleConnection(Connection.localConnectString)) { ds = new DataSet(); da = new OracleDataAdapter(selectSql, conn); da.Fill(ds, "tableName"); dataGridView.AutoGenerateColumns = true; dataGridView.DataSource = ds; dataGridView.DataMember = "tableName"; }}catch(Exception ex){ MessagerBox.Show(ex.Message);}如果表中的列不要全部显示的话,那你可以这样写:dataGridView.Columns[“列名”].Visible=false;要是用的其他数据库把上边的dataadapter该下。 好象dataset有个filter 属性还是dataset.table有个filter属性dataset.filter = "列名 = '" + 条件 +"'"; 对不起,刚才看了哈,是bindingSource的Filter 属性如果你用了bindingSource的话.如:bindingSource.Filter = "username = ' " + textbox.text + " ' "; 如果你用了bindingSource的话. 如:bindingSource.Filter = "username = ' " + textbox.text + " ' ";这里出现错误。:((System.ComponentModel.ISupportInitialize)(this.enterprise_DataDataSet)).EndInit(); DataRow[] rows = this.dbCenter.busDt.Select("MyBusId like '" + this.textBox1.Text+ "%'"); this.listView1.Items.Clear(); foreach (DataRow row in rows) { ListViewItem newItem = new ListViewItem(); newItem.SubItems[0].Text = row["MyBusId"].ToString(); newItem.SubItems.Add(row["MyBusType"].ToString()); this.listView1.Items.Add(newItem); } 在button里面加入SQL语句就可解决· 一句话 用SqlDataReader能解决这个问题 dahai99007 等 级: 发表于:2008-01-02 17:41:076楼 得分:0 哥们你可以这样: string selectSql="select * from table where"; if(this.textBox.text!="") { selectSql+="name='"+textBox.text.Trim().ToString()+"'"; } else { MessageBox.Show("请输入正确的查询条件!","提示消息",MessageBoxButtons.OK,MessageBoxIcon.Information); } try { using(conn= new OracleConnection(Connection.localConnectString)) { ds = new DataSet(); da = new OracleDataAdapter(selectSql, conn); da.Fill(ds, "tableName"); dataGridView.AutoGenerateColumns = true; dataGridView.DataSource = ds; dataGridView.DataMember = "tableName"; } } catch(Exception ex) { MessagerBox.Show(ex.Message); } 如果表中的列不要全部显示的话,那你可以这样写: dataGridView.Columns[“列名”].Visible=false; 要是用的其他数据库把上边的dataadapter该下。 照着模仿一下就好了! 关于继承的一个问题 img 问题求大神 ? 点击GridView表头进行排序 关于类中 public private 等的疑问 一个C#小问题 多线程结束问题 各位帮忙; 水晶报表(8.0和 10) 不能显示Access中存取图片为"长二进制数据" ?? 怎么样反编译dll文件? 请高手解答byte[] 强制转换问题? 关于GDI+的单位问题,给100,望能解决! 调试程序的时候正常,但是发布后再安装运行却不行了 wpf怎么实现文件列表功能
我想在textbox里输入姓名,然后查询表中的“姓名”列,符合的就输出就行了,请帮我写出代码即可。
表有50行。
也不知道对你有没有帮助.. :)
//表名users
string search = TextBox1.Text.Trim();
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
string sql = "select name from users where name like'%"+search+"%'";
SqlDataAdapter adap = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
adap.Fill(ds);
if(ds.Tables[0].Rows.Count > 0)
{
//返回所要的记录
//GridView1.DataSource = ds.Tables[0].DefaultView;
}
string selectSql="select * from table where";
if(this.textBox.text!="")
{
selectSql+="name='"+textBox.text.Trim().ToString()+"'";
}
else
{
MessageBox.Show("请输入正确的查询条件!","提示消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
try
{
using(conn= new OracleConnection(Connection.localConnectString))
{
ds = new DataSet();
da = new OracleDataAdapter(selectSql, conn);
da.Fill(ds, "tableName");
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = ds;
dataGridView.DataMember = "tableName";
}
}
catch(Exception ex)
{
MessagerBox.Show(ex.Message);
}
如果表中的列不要全部显示的话,那你可以这样写:
dataGridView.Columns[“列名”].Visible=false;
要是用的其他数据库把上边的dataadapter该下。
还是dataset.table有个filter属性dataset.filter = "列名 = '" + 条件 +"'";
如果你用了bindingSource的话.
如:bindingSource.Filter = "username = ' " + textbox.text + " ' ";
如:bindingSource.Filter = "username = ' " + textbox.text + " ' ";这里出现错误。
:((System.ComponentModel.ISupportInitialize)(this.enterprise_DataDataSet)).EndInit();
foreach (DataRow row in rows)
{
ListViewItem newItem = new ListViewItem();
newItem.SubItems[0].Text = row["MyBusId"].ToString();
newItem.SubItems.Add(row["MyBusType"].ToString()); this.listView1.Items.Add(newItem);
}
用SqlDataReader能解决这个问题
等 级:
发表于:2008-01-02 17:41:076楼 得分:0
哥们你可以这样:
string selectSql="select * from table where";
if(this.textBox.text!="")
{
selectSql+="name='"+textBox.text.Trim().ToString()+"'";
}
else
{
MessageBox.Show("请输入正确的查询条件!","提示消息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
try
{
using(conn= new OracleConnection(Connection.localConnectString))
{
ds = new DataSet();
da = new OracleDataAdapter(selectSql, conn);
da.Fill(ds, "tableName");
dataGridView.AutoGenerateColumns = true;
dataGridView.DataSource = ds;
dataGridView.DataMember = "tableName";
}
}
catch(Exception ex)
{
MessagerBox.Show(ex.Message);
}
如果表中的列不要全部显示的话,那你可以这样写:
dataGridView.Columns[“列名”].Visible=false;
要是用的其他数据库把上边的dataadapter该下。
照着模仿一下就好了!