如题。本人刚接触 希望大虾们能给个代码参考。

解决方案 »

  1.   

    using system.data.sql
    using system.data.sqlclientdataset ds= new dataset();
    string connectionString=连接字符串
    string commandString=查询语句(如"select * from 表 where 内容='"+ textbox.text +"'")
    SqlConnection con= new SqlConnection(connectionString)
    con.open()
    SqlDataAdapter da= new SqlDataAdapter(commandString)
    da.fill(ds)
    datagrid.datesource=ds.table[0].defaultview
     
      

  2.   

    如果想动态查询就要控制TEXTBOX的动作事件了,如果最后操作一下,如楼上。
      

  3.   

    楼上的方法的确可以,但是建议你用参数的方法
    如果别人输入textbox.text ="1 or 1=1";
    就用"select * from 表 where 内容='"+ textbox.text +"'"查出所有东西了
      

  4.   

    你可以先对第一个字符如‘A’进行模糊查询,查出一个记录集Dataset出来绑定到grid,然后从第2个字符如‘d’开始对这个记录集进行查询Dataset.Tables[0].Select("keyword like 'Ad%'"),然后绑定grid,依次第三个字符如‘a’同样Dataset.Tables[0].Select("keyword like 'Ada%'")然后绑定grid,这样每增加一个字符或减少一个字符相应的更改Select里面的条件就行。
      

  5.   

    事件就利用textbox的textchange事件,在textchange事件里进行查询判断就可以。
      

  6.   

    我是把代码放在按钮里面的
    //=================================================================================
    private void button1_Click(object sender, EventArgs e)
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += @"Data Source=D:\shop.mdb";
                DataSet ds = new DataSet();
                string commandString = " SELECT * FROM custom where name='" + textBox1.Text.ToString() + "' ";
                SqlConnection conn = new SqlConnection(strConnection);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(commandString);
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            } 
    //=================================================================================运行的时候SqlDataAdapter da= new SqlDataAdapter(commandString)出现以下错误 请指教

    1 与“System.Data.SqlClient.SqlDataAdapter.SqlDataAdapter(System.Data.SqlClient.SqlCommand)”最匹配的重载方法具有一些无效参数

    2参数“1”: 无法从“string”转换为“System.Data.SqlClient.SqlCommand
      

  7.   

    利用一条sql语句,where条件那添加参数textbox的值
      

  8.   

    如果最后去查询出来的就像一楼的就行了,如果想即时的那就在textbox里的change的事件里去写
      

  9.   

    你debug一下看看是不是fill那步出错了
    还有 结尾时刻要记得加conn.close() 不然操作太多了连接池会饱和
      

  10.   

    对了 还要datagrid.databind()
    不绑定是显示不出来结果的
      

  11.   

    我错了 没说清楚 浪费大家时间 很抱歉~~
    我连的是ACCESS数据库 所以
      

  12.   

    搞定了 谢谢各位帮助。
    using System.Data.SqlClient;
    using System.Data.OleDb;namespace Shop
    {
        public partial class Form5 : Form
        {
            public Form5()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += @"Data Source=D:\shop.mdb";
                DataSet ds = new DataSet();
                string commandString = " SELECT * FROM custom where customer='" + textBox1.Text.ToString() + "' ";
                OleDbConnection conn = new OleDbConnection(strConnection);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter(commandString,strConnection);
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                conn.Close();
            }    }
    }
      

  13.   

    TextBox里的条件放入你需要查询的sql语句中在和DataGridView重新绑定~
      

  14.   

    假设楼主的Access数据库文件是E:\MyData.mdb
    using System.Data.SqlClient;// 连接数据库
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\MyData.mdb";// 创建命令
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandText = "select * from tableName where someField=" + "'" + textBox.Text + "'";// 打开连接
    connection.Open();// 执行命令
    DataSet dataSet = new DataSet();
    SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
    dataAdapter.Fill(dataSet);// 将查询结果显示在DataGridView上
    dataGridView.datesource = dataSet.Table[0];// 如果是Web程序,还需要进行绑定dataGridView.Bind();// 关闭连接
    connection.Close();
      

  15.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    private void button10_Click(object sender, EventArgs e)
            {
                //输入房屋情况进行查询
                SqlConnection com = new SqlConnection("Data Source=PC-201202221639;Initial Catalog=XSGL;Integrated Security=True");
                SqlDataAdapter qksda = new SqlDataAdapter("select * from cz where 房屋情况='" + comboBox3.Text + "'", com);
                DataTable qkdt = new DataTable();
                qksda.Fill(qkdt);
                dataGridView1.DataSource = qkdt;
            }
    你可以查找这个写的,希望能帮到你的