using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Day05_0100
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cboGrade_SelectedIndexChanged(object sender, EventArgs e)
{
#region 内存操作
//write what 写什么
//1.User select which grade
//string gradeName = cboGrade.Text;
//if (gradeName.Equals("S1"))
//{
// cboSubject.Items.Clear();
// cboSubject.Items.Add("S1----Java");
// cboSubject.Items.Add("S1----SQL");
// cboSubject.Items.Add("S1----C#");
//}
//else if (gradeName.Equals("S2"))
//{
// cboSubject.Items.Clear();
// cboSubject.Items.Add("S2----Java");
// cboSubject.Items.Add("S2----SQL");
//}
#endregion
string gradename = cboGrade.Text;
int gradeid= GetIdByName(gradename);
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
string sql = "select * from subject where gradeid=@gid";
SqlParameter para=new SqlParameter("@gid",gradeid);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(para);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds=new DataSet();
da.Fill(ds,"subjectInfo");
cboSubject.DataSource = ds.Tables["subjectInfo"];
cboSubject.ValueMember = "subjectid";
cboSubject.DisplayMember = "subjectname";
if (ds.Tables["subjectInfo"].Rows.Count == 0)
{
ds.Tables["subjectInfo"].Clear();
}
}
private int GetIdByName(string gradename)
{
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
string sql = "select gradeid from grade where gradename='"+gradename+"'";
SqlCommand cmd=new SqlCommand(sql,con);
con.Open();
int gradeid=Convert.ToInt32(cmd.ExecuteScalar());
con .Close();
return gradeid;
}
private void Form1_Load(object sender, EventArgs e)
{
string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
string sql = "select * from grade";
SqlDataAdapter da=new SqlDataAdapter(sql,con);
DataSet ds=new DataSet();
da.Fill(ds, "gradeInfo");
cboGrade.DataSource = ds.Tables[0];
cboGrade.DisplayMember = "gradeName";
cboGrade.ValueMember = "gradeid";
}
}
}
实体类的高级应用
最新推荐文章于 2022-06-23 10:00:53 发布