level 1
仓鼠爱小鑫鑫
楼主
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace search
{
public partial class Frmsearch : Form
{
public Frmsearch()
{
InitializeComponent();
}
private void Frmsearch_Load(object sender, EventArgs e)
{
}
public void GetScoure(string strSql)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=labdata;Integrated Security=True");
con.Open();
SqlCommand com = new SqlCommand(strSql, con);
dataGridView1.Rows.Clear();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString());
}
dr.Close();
con.Close();
}
//按取消键则取消
private void btnEsc_Click(object sender, EventArgs e)
{
ckClass.Checked = false;
ckName.Checked = false;
ckSID.Checked = false;
rdbMan.Checked = false;
rdbWoman.Checked = false;
}
//点击学号则输入学号
private void ckSID_CheckedChanged(object sender, EventArgs e)
{
if (ckSID.Checked == true)
{
txtSID.Enabled = true;
txtSID.Focus();
}
else
{
txtSID.Text = "";
txtSID.Enabled = false;
}
}
//点击班级则输入班级
private void ckClass_CheckedChanged(object sender, EventArgs e)
{
if (ckClass.Checked == true)
{
txtClass.Enabled = true;
txtClass.Focus();
}
else
{
txtClass.Text = "";
txtClass.Enabled = false;
}
}
//点击姓名则输入姓名
private void ckName_CheckedChanged(object sender, EventArgs e)
{
if (ckName.Checked == true)
{
ckName.Enabled = true;
txtName.Focus();
}
else
{
txtName.Text = "";
txtName.Enabled = false;
}
}
//
public string strSql;//用于存储SQL语句
public string strSID;//用于存学生编号
public string strName;//用于存储学生姓名
public string strClass;//用于存储学生班级
public string strSex;//用于存储学生姓别
public static int intCount = 0;//控制数据组索引
public string[] strScoure = new string[4];
public int intAdd;//用来判断SQL语句数组
private void btnsure_Click(object sender, EventArgs e)
{
intCount = 0;
if (txtSID.Text != "")
{
strSID = "学号 like'%" + txtSID.Text + "%'";
strScoure[intCount] = strSID;
intCount++;
}
if (txtName.Text != "")
{
strName = "姓名 like '%" + txtName.Text + "%' ";
strScoure[intCount] = strName;
intCount++;
}
if (txtClass.Text != "")
{
strClass = "年级 like '%" + txtClass.Text + "%'";
strScoure[intCount] = strClass;
intCount++;
}
//if (rdbMan.Checked == true)
//{
// strSex = "学生性别 like '%" + rdbMan.Text + "%'";
// strScoure[intCount] = strSex;
// intCount++;
//}
//if (rdbWoman.Checked == true)
//{
// strSex = "学生性别 like '%" + rdbWoman.Text + "%'";
// strScoure[intCount] = strSex;
// intCount++;
//}
for (int i = 0; i < strScoure.Length; i++)
{
if (strScoure[i] != null)
{
strSql += strScoure[i];
intAdd++;
}
}// end
switch (intAdd)
{
case 0:
strSql = "select * from Student";
break;
case 1:
strSql = "select * from Student where " + strScoure[0];
break;
case 2:
strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1];
break;
case 3:
strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2];
break;
//case 4:
// strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2] + " and " + strScoure[3];
// break;
}
GetScoure(strSql);
//MessageBox.Show(strSql);
intAdd = 0;
intCount = 0;
strSql = "";
for (int i = 0; i < strScoure.Length; i++)
{
if (strScoure[i] != null)
{
strScoure[i] = null;
}
}// end
}
public void getScoure(string str_name)
{
string txtSID, txtName, txtSex;
//连接数据库
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=labdata;Integrated Security=True");
con.Open();
SqlCommand com = new SqlCommand("select * from Student where 姓名= '" + Name + "'", con);
SqlDataReader dr = com.ExecuteReader();//
while (dr.Read())
{
txtSID = dr[0].ToString();
txtName = dr[1].ToString();
//txtEmpty = dr[2].ToString();
txtSex = dr[2].ToString();
//txtAge = dr[4].ToString();
//txtKnowledge = dr[5].ToString();
//txtTeching = dr[6].ToString();
}
dr.Close();
con.Close();
}
}
}
2015年11月14日 01点11分
1
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace search
{
public partial class Frmsearch : Form
{
public Frmsearch()
{
InitializeComponent();
}
private void Frmsearch_Load(object sender, EventArgs e)
{
}
public void GetScoure(string strSql)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=labdata;Integrated Security=True");
con.Open();
SqlCommand com = new SqlCommand(strSql, con);
dataGridView1.Rows.Clear();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString());
}
dr.Close();
con.Close();
}
//按取消键则取消
private void btnEsc_Click(object sender, EventArgs e)
{
ckClass.Checked = false;
ckName.Checked = false;
ckSID.Checked = false;
rdbMan.Checked = false;
rdbWoman.Checked = false;
}
//点击学号则输入学号
private void ckSID_CheckedChanged(object sender, EventArgs e)
{
if (ckSID.Checked == true)
{
txtSID.Enabled = true;
txtSID.Focus();
}
else
{
txtSID.Text = "";
txtSID.Enabled = false;
}
}
//点击班级则输入班级
private void ckClass_CheckedChanged(object sender, EventArgs e)
{
if (ckClass.Checked == true)
{
txtClass.Enabled = true;
txtClass.Focus();
}
else
{
txtClass.Text = "";
txtClass.Enabled = false;
}
}
//点击姓名则输入姓名
private void ckName_CheckedChanged(object sender, EventArgs e)
{
if (ckName.Checked == true)
{
ckName.Enabled = true;
txtName.Focus();
}
else
{
txtName.Text = "";
txtName.Enabled = false;
}
}
//
public string strSql;//用于存储SQL语句
public string strSID;//用于存学生编号
public string strName;//用于存储学生姓名
public string strClass;//用于存储学生班级
public string strSex;//用于存储学生姓别
public static int intCount = 0;//控制数据组索引
public string[] strScoure = new string[4];
public int intAdd;//用来判断SQL语句数组
private void btnsure_Click(object sender, EventArgs e)
{
intCount = 0;
if (txtSID.Text != "")
{
strSID = "学号 like'%" + txtSID.Text + "%'";
strScoure[intCount] = strSID;
intCount++;
}
if (txtName.Text != "")
{
strName = "姓名 like '%" + txtName.Text + "%' ";
strScoure[intCount] = strName;
intCount++;
}
if (txtClass.Text != "")
{
strClass = "年级 like '%" + txtClass.Text + "%'";
strScoure[intCount] = strClass;
intCount++;
}
//if (rdbMan.Checked == true)
//{
// strSex = "学生性别 like '%" + rdbMan.Text + "%'";
// strScoure[intCount] = strSex;
// intCount++;
//}
//if (rdbWoman.Checked == true)
//{
// strSex = "学生性别 like '%" + rdbWoman.Text + "%'";
// strScoure[intCount] = strSex;
// intCount++;
//}
for (int i = 0; i < strScoure.Length; i++)
{
if (strScoure[i] != null)
{
strSql += strScoure[i];
intAdd++;
}
}// end
switch (intAdd)
{
case 0:
strSql = "select * from Student";
break;
case 1:
strSql = "select * from Student where " + strScoure[0];
break;
case 2:
strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1];
break;
case 3:
strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2];
break;
//case 4:
// strSql = "select * from Student where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2] + " and " + strScoure[3];
// break;
}
GetScoure(strSql);
//MessageBox.Show(strSql);
intAdd = 0;
intCount = 0;
strSql = "";
for (int i = 0; i < strScoure.Length; i++)
{
if (strScoure[i] != null)
{
strScoure[i] = null;
}
}// end
}
public void getScoure(string str_name)
{
string txtSID, txtName, txtSex;
//连接数据库
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=labdata;Integrated Security=True");
con.Open();
SqlCommand com = new SqlCommand("select * from Student where 姓名= '" + Name + "'", con);
SqlDataReader dr = com.ExecuteReader();//
while (dr.Read())
{
txtSID = dr[0].ToString();
txtName = dr[1].ToString();
//txtEmpty = dr[2].ToString();
txtSex = dr[2].ToString();
//txtAge = dr[4].ToString();
//txtKnowledge = dr[5].ToString();
//txtTeching = dr[6].ToString();
}
dr.Close();
con.Close();
}
}
}