新人求教 非常感谢 麻烦了要从数据库SQL SERVER读取一张表,(可以先设定为数据库在本地)
表中 列名就先用 A,B,C,D,E,F,G,H,I,J,K表示
A为用户ID,后面几列均为用户的详细内容
A的值是在最初的登录的时候就可以得到,查询的话就可以直接用 A=IDABC 表示即可,查询就比如只显示 BCDE几列即可
然后在窗口界面点击查询 按钮,就可以在窗口显示出来如果可以在加一个功能 就是界面有几个CHECK按钮, 分别是几个列名,比如一般不用查询的F,G,H,也就是我一般查询的话只显示BCDE列,如果我勾选了那就可以额外查询这个列。麻烦有详细的代码,,最好直接可以执行的。。然后如果需要修改的地方 比如数据库,,和列那边。。加点备注提示下 最好了。。非常非常感谢前辈赐教!!

解决方案 »

  1.   

    就是个多条件查询了,可以如下试试string sql = "select * from A where 1=1 ";
    if(checkbox1.checked)  //f条件查询复选框是否勾上  下面同理
        sql += " and f='" + textbox1.text;    勾上附加查询条件  下面同理
    if(checkbox2.checked)
        sql += " and g='" + textbox2.text; 
    if(checkbox3.checked)
        sql += " and h='" + textbox3.text;
      

  2.   

    string strConnection = "Trusted_Connection=SSPI;Data Source=localhost;Initial Catalog=yourdb;Connect Timeout=30";private void QueryUserInfo()
    {
    this.dataSet1.userinfo.Clear(); using (SqlConnection conn = new SqlConnection(strConnection))
    {
    conn.Open(); //可根据CheckBox动态拼SQL
    string sql = string.Format("select * from userinfo where A = '{0}'", "IDABC");

    SqlCommand command = new SqlCommand(sql, conn);
    SqlDataReader reader = command.ExecuteReader();
    this.dataSet1.userinfo.Load(reader); reader.Close();
    command.Dispose();
    }
    this.dataSet1.userinfo.AcceptChanges();

    //TODO显示到页面上,Grid或者其他控件
    }
      

  3.   

    额, 上面sql中掉了几个结束单引号,自己修改
      

  4.   

    看来没有好好解决问题。
    别在csdn一刻树上吊死,自己找找网上的简单例子,这个一所所一大把。
      

  5.   

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
      

  6.   

    你详细说下要哪些详细的;SQL语句如何写?还是查询界面到连接数据库并到显示的全部代码?
      

  7.   


    全部的 麻烦了。。SQL我倒是会写- -
      

  8.   

    我只会托个界面- -就大概是这样 客户代码是不用输入的 就是我开始说的A..
    下面就显示 B C D E的列
    中间的选择 比如就是F G H。。- -这些要是麻烦不写也可以 主要查询就行
      

  9.   

    三楼的大哥不是给写了吗,你可以把那个提取出一个方法(每次传入SQL语句,返回一个数据集)出来有用的留下,没用的剔除;然后连接(三楼是strConnection )根据自己的进行配置,例如:strConnection = "Server=.;DataBase=hospital_callback;Uid=sa;Pwd=123"; //server=.指本机应该知道,DataBase指你要连接的数据库名称,Uid指访问数据库的用户名(userid),Pwd指访问密码(Password)
      

  10.   

    无语,留不留言;那就在这吧
    QQ:1653354951,小号,验证时告诉我是CSDN朋友
      

  11.   


    string strConnection = "Trusted_Connection=SSPI;Data Source=localhost;Initial Catalog=yourdb;Connect Timeout=30";
     
    private void QueryUserInfo()
    {
        this.dataSet1.userinfo.Clear();
     
        using (SqlConnection conn = new SqlConnection(strConnection))
        {
            conn.Open();
     
            //可根据CheckBox动态拼SQL
            string sql = string.Format("select * from userinfo where A = '{0}'", "IDABC");
             
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = command.ExecuteReader();
            this.dataSet1.userinfo.Load(reader);
     
            reader.Close();
            command.Dispose();
        }
        this.dataSet1.userinfo.AcceptChanges();
         
        //TODO显示到页面上,Grid或者其他控件
    }
      

  12.   

    预览:
    数据库:
    为了这你给的分数,我特的给你写了一个例子,你参考一下:
    全部代码: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;namespace SqlTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string strSql = "";
                string NameList = "";            List<string> list = new List<string>();            foreach (CheckBox item in groupBox1.Controls)
                {
                    if (item is CheckBox)
                    {
                        if (item.Checked)
                        {
                            list.Add(item.Text);
                        }
                    }
                }
                for (int i = 0; i < list.Count; i++)
                {
                    NameList += list[i].ToString() + ",";
                }            NameList = NameList.TrimEnd(',');
                strSql = "SELECT " + NameList + "  FROM TB_TEST2"; 
                GetData(strSql);
            }
            void GetData(string strSql)
            {
                string connectionString = "server=;database=TESTDB;uid=sa;pwd=";
                SqlConnection con = new SqlConnection(connectionString);
                SqlDataAdapter da;
                DataSet ds = new DataSet();
                con.Open();
                da = new SqlDataAdapter(strSql, con);
                da.Fill(ds, "tb_test2");
                this.dataGridView1.DataSource = ds.Tables[0];
                con.Close();
            }
        }
    }