新人求教 非常感谢 麻烦了要从数据库SQL SERVER读取一张表,(可以先设定为数据库在本地)
表中 列名就先用 A,B,C,D,E,F,G,H,I,J,K表示
A为用户ID,后面几列均为用户的详细内容
A的值是在最初的登录的时候就可以得到,查询的话就可以直接用 A=IDABC 表示即可,查询就比如只显示 BCDE几列即可
然后在窗口界面点击查询 按钮,就可以在窗口显示出来如果可以在加一个功能 就是界面有几个CHECK按钮, 分别是几个列名,比如一般不用查询的F,G,H,也就是我一般查询的话只显示BCDE列,如果我勾选了那就可以额外查询这个列。麻烦有详细的代码,,最好直接可以执行的。。然后如果需要修改的地方 比如数据库,,和列那边。。加点备注提示下 最好了。。非常非常感谢前辈赐教!!
表中 列名就先用 A,B,C,D,E,F,G,H,I,J,K表示
A为用户ID,后面几列均为用户的详细内容
A的值是在最初的登录的时候就可以得到,查询的话就可以直接用 A=IDABC 表示即可,查询就比如只显示 BCDE几列即可
然后在窗口界面点击查询 按钮,就可以在窗口显示出来如果可以在加一个功能 就是界面有几个CHECK按钮, 分别是几个列名,比如一般不用查询的F,G,H,也就是我一般查询的话只显示BCDE列,如果我勾选了那就可以额外查询这个列。麻烦有详细的代码,,最好直接可以执行的。。然后如果需要修改的地方 比如数据库,,和列那边。。加点备注提示下 最好了。。非常非常感谢前辈赐教!!
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;
{
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或者其他控件
}
别在csdn一刻树上吊死,自己找找网上的简单例子,这个一所所一大把。
全部的 麻烦了。。SQL我倒是会写- -
下面就显示 B C D E的列
中间的选择 比如就是F G H。。- -这些要是麻烦不写也可以 主要查询就行
QQ:1653354951,小号,验证时告诉我是CSDN朋友
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或者其他控件
}
数据库:
为了这你给的分数,我特的给你写了一个例子,你参考一下:
全部代码: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();
}
}
}