using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
run();
}
public void run()
{
if(listView1.Items.Count>0)
{
listView1.Items.Clear();
}
string ent = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
SqlConnection conn = new SqlConnection(ent);
StringBuilder sql = new StringBuilder();
sql.AppendLine(" select s.[StudentNo],s.[StudentName],s.[Sex],G.[GradeName] ");
sql.AppendLine(" from dbo.Student s,dbo.Grade G ");
sql.AppendLine(" where s.[GradeId]=G.[GradeId] ");
sql.AppendFormat(" and s.[StudentName] like '%{0}%'",this.textBox1.Text.Trim());
DBHelper per = new DBHelper();
try
{
SqlCommand command = new SqlCommand(sql.ToString(), per.Connection);
per.OpenConnection();
SqlDataReader rader = command.ExecuteReader();
if (!rader.HasRows)
{
MessageBox.Show("没有要查询的语句", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
while (rader.Read())
{
//获得查询到的数据
string studentNo = rader["StudentNo"].ToString();
string sutdentname = rader["StudentName"].ToString();
string sex = rader["Sex"].ToString();
string granfname = rader["GradeName"].ToString();
//创建ListViewItem
ListViewItem tem = new ListViewItem(studentNo);
//添加子项
tem.SubItems.Add(sutdentname);
tem.SubItems.Add(sex);
tem.SubItems.Add(granfname);
listView1.Items.Add(tem);
}
}
rader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("系统出现错误", "操作", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
per.CloseConnection();
}
}
private void Form1_Load(object sender, EventArgs e)
{
run();
}
private void button2_Click(object sender, EventArgs e)
{
this.listView1.View = View.LargeIcon;
}
private void smill_Click(object sender, EventArgs e)
{
this.listView1.View = View.SmallIcon;
}
private void xiangxi_Click(object sender, EventArgs e)
{
this.listView1.View = View.Details;
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
string studentNo=this.listView1.SelectedItems[0].Text;
Form2 one = new Form2();
one.two = studentNo;
one.Show();
}
//public bool dent()
//{
// bool succss = false;
// string pwd = this.listView1.ToString();
// string sex = this.listView1.ToString();
// string gradname = this.listView1.ToString();
// DBHelper ww = new DBHelper();
// try
// {
// StringBuilder du = new StringBuilder();
// du.AppendFormat(" update [Student] set [StudentNo]='{0}'", pwd);
// du.AppendFormat(" ,[StudentName]='{0}'", Name);
// du.AppendFormat(" ,[Sex]='{0}'", sex);
// du.AppendFormat(" ,[GradeName]='{0}'", gradname);
// SqlCommand com = new SqlCommand(du.ToString(), ww.Connection);
// ww.OpenConnection();
// int result = com.ExecuteNonQuery();
// if (result == 1)
// {
// succss = true;
// }
// }
// catch
// {
// succss = false;
// }
// finally
// {
// ww.CloseConnection();
// }
// return succss;
//}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
string ent = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
public string two = "";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
student();
}
public void student()
{
try
{ string ull = "select * from sutdent where StudentNo=''" + two +"'";
SqlConnection conn = new SqlConnection(ent);
DBHelper s=new DBHelper();
s.OpenConnection();
string sqlName = string.Format(" select s.[StudentNo],s.[StudentName],s.[Sex],G.[GradeName] from dbo.Student s,dbo.Grade G where s.[GradeId]=G.[GradeId] and s.[StudentNo]='{0}'", two);
SqlCommand comm = new SqlCommand(sqlName.ToString(),s.Connection);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
//获得查询到的数据
this.textBox1.Text = reader["StudentNo"].ToString();
this.textBox2.Text = reader["StudentName"].ToString();
if (reader["Sex"].ToString() == "男")
{
this.radioButton1.Checked=true;
}
else{
this.radioButton2.Checked=true;
}
this.textBox3.Text = reader["GradeName"].ToString();
MessageBox.Show(ull);
}
reader.Close();
}
catch (Exception r)
{
MessageBox.Show(r.Message);
}
}
//保存
private void baocun_Click(object sender, EventArgs e)
{
setdata();
}
public void setdata()
{
bool success = true;
SqlConnection conn = new SqlConnection(ent);
string id = this.textBox1.Text;
string name = this.textBox2.Text;
string sex = "";
if (radioButton1.Checked == true)
{
sex = "男";
}
else if (radioButton2.Checked == true)
{
sex = "女";
}
string nianji = this.textBox3.Text;
// DateTime date = this.time.Value; //出生日期
//string birthday = string.Format("{0}-{1}-{2}", date.Year, date.Month, date.Day);
try
{
conn.Open();
//构建按年级名称查询年级编号的Sql语句
string sqlname = string.Format(" select * from Student where GradeName='{0}'");
//创建Command对象
SqlCommand command = new SqlCommand(sqlname, conn);
//查询获得年级编号
int gradeld = (int)command.ExecuteScalar();
//查询插入学生记录的SQL语句
StringBuilder sql = new StringBuilder();
sql.AppendLine("insert into Student (LoginPwd,StudentName,Gender,GradeId,Phone,Address,Birthday,Email) ");
sql.AppendFormat("VALUES ('{0}','{1}','{2}','{3}')", id, name, sex, nianji);
//创建command对象
command = new SqlCommand(sql.ToString(), conn);
//打开数据库连接
conn.Open();
//执行命令
int result = command.ExecuteNonQuery();
//根据操作结果给出提示信息
if (result == 1)
{
success = true;
//string SqlNo = "select @@IDENTITY from dbo.Student";
//command.CommandText = SqlNo;
//int StudentNo = Convert.ToInt32(command.ExecuteScalar()); //学号
//this.id.Text = StudentNo.ToString();
MessageBox.Show("保存成功!");
}
}
catch (Exception e)
{
success = false;
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
}
//清空
private void clear_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
}
}
}