?? studentselect.cs
字號:
?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 _7計應的班級管理系統
{
public partial class studentselect : Form
{
public studentselect()
{
InitializeComponent();
}
private void buttonselect_Click(object sender, EventArgs e)
{
string strconn = "user id=sa;data source=localhost;integrated security=false;initial catalog=學生數據庫1";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd = conn.CreateCommand();
conn.Open();
if (radioButtonname.Checked || radioButtonclass.Checked || radioButtonsex.Checked || radioButtonnumber.Checked)
{
try
{
if (radioButtonname.Checked)
{
if (textBoxname.Text.Trim() != "")
{
cmd.CommandText = "select * from student_information where 姓名='" + textBoxname.Text.Trim() + "'";
}
else
MessageBox.Show("姓名不能為空");
}
else if (radioButtonclass.Checked)
{
if (textBoxclass.Text.Trim() != "")
{
listViewdisplay.Clear();
cmd.CommandText = "select * from student_information where 班級='" + textBoxclass.Text.Trim() + "'";
}
else
MessageBox.Show("班級不能為空");
}
else if (radioButtonsex.Checked)
{
if (textBoxsex.Text.Trim() != "")
{
listViewdisplay.Clear();
cmd.CommandText = "select * from student_information where 姓別='" + textBoxsex.Text.Trim() + "'";
}
else
MessageBox.Show("姓別不能為空");
}
else if (radioButtonnumber.Checked)
{
if (textBoxnumber.Text.Trim() != "")
{
listViewdisplay.Clear();
cmd.CommandText = "select * from student_information where 學號='" + textBoxnumber.Text.Trim() + "'";
}
else
MessageBox.Show("學號不能為空");
}
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i < dr.FieldCount; i++)
{
ColumnHeader ch = new ColumnHeader();
ch.Text = dr.GetName(i);
listViewdisplay.Columns.Add(ch);
}
while (dr.Read())
{
ListViewItem item = new ListViewItem();
item.Text = dr.GetValue(0).ToString();
for (int i = 1; i < dr.FieldCount; i++)
{
item.SubItems.Add(dr.GetValue(i).ToString());
}
listViewdisplay.Items.Add(item);
}
dr.Close();
}
catch
{
//MessageBox.Show(ee.Message,"錯誤的輸入!");
}
finally
{
conn.Close();
}
}
else
MessageBox.Show("請選擇查找方式!");
}
private void buttonclear_Click(object sender, EventArgs e)
{
textBoxname.Clear();
textBoxsex.Clear();
textBoxnumber.Clear();
textBoxclass.Clear();
listViewdisplay.Clear();
}
private void buttonexit_Click(object sender, EventArgs e)
{
this.Hide();
}
private void refresh()
{
listViewdisplay.Clear();
SqlConnection conn = new SqlConnection("server=any;uid=sa;pwd=sa;database=學生數據庫1");
SqlCommand cmd = new SqlCommand();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from student_information";
conn.Open();
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
//if(dr==null)
//{
for (int i = 0; i < dr.FieldCount; i++)
{
ColumnHeader ch = new ColumnHeader();
ch.Text = dr.GetName(i);
listViewdisplay.Columns.Add(ch);
}
while (dr.Read())
{
ListViewItem item = new ListViewItem();
item.Text = dr.GetValue(0).ToString();
for (int i = 1; i < dr.FieldCount; i++)
{
item.SubItems.Add(dr.GetValue(i).ToString());
}
listViewdisplay.Items.Add(item);
}
//}
//else
// MessageBox.Show("沒有這樣的記錄!");
dr.Close();
conn.Close();
}
private void buttonrefresh_Click(object sender, EventArgs e)
{
refresh();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -