开启添加新专业窗体设置住窗体 :
frmAddSpecialty frmAdd = newfrmAddSpecialty();
frmAdd.MdiParent = this;
frmAdd.Show();
//使用ado.net链接数据库并对其操作,对象有
connection用于数据库链接,常用判断是否打开open,使用完用close
command用于执行sql语句的命令,常用有3个方法executenonquery 执行insert ,update,delete ;executescalar用于获取表中的行总数,统计作用语法是 count;executereader用select语句,用于查询表中数据
datareader,用于读取数据库内容,速度最快,关键点是用完必须close,常用语法是while(){}
dataadapter用于填充数据,起到桥梁作用 常用fill
dataset断开式访问,取到数据后立刻断开链接,最常使用的数据集,可以在本机内存中进行数据处理,并在此链接数据库返回修改内容
//第一步获取数据库链接字符串 也可声明一个通用的类.
private string strcon =ConfigurationManager.ConnectionStrings["Dbcon"].ToString();//获取appconfig中标准数据库连接
//语法insert into 表名(列名,列名2)values(值1,值2)注意事项列名和后面值必须对应起来
//主键自动增长的列,不需要我们添加
string strsql = "insert intouserinfo(username,userpwd,userrole,adddate)values('" + username +"','" + userpwd+"','"+userrole+"','"+DateTime.Now.ToString()+"')";
//第二步打开数据库连接,connection
SqlConnection sqlcon = new SqlConnection(strcon);//k数据库链接字符串初始化到数据连接池中
sqlcon.Open();//打开数据库
//判断数据库是否打开状态
if (sqlcon.State.ToString().ToLower() == "open")
{
SqlCommand cmd=newSqlCommand(strsql,sqlcon);
//执行数据库命令,存储到库中
int ireturn = cmd.ExecuteNonQuery();//如果执行成功返回结果为1
if (ireturn>0)
{
}
窗体加载时的绑定:
//数据表中显示汉文
string strsql = "select specialityid as 编号,specialitynameas 专业名称,adddate as 添加时间,remark 备注 from specialityinfo";
SqlConnection sqlconnection = newSqlConnection(DbHelper.strcon);
sqlconnection.Open();
//用于填充数据集,起到中间桥梁的作用,现执行comman后用数据适配器
SqlDataAdapter dateAdapter=new SqlDataAdapter(strsql,sqlconnection );
//dataset断开是访问,获取数据后,即断开了数据库连接,可以在本地内存中保存数据,操作数据
DataSet ds = new DataSet();
//填充数据
dateAdapter.Fill(ds,"specialityinfo");
//绑定数据
dgv.DataSource = ds.Tables[0];
sqlconnection.Close();
//获取当前选中
修改
DataGridViewRow dr;//声明一个当前行的字段
if (this.dgv.CurrentRow != null)//判断是否选中行
{
dr = dgv.CurrentRow;//获取了当前行的值
string spid = dr.Cells["编号"].Value.ToString();//获取选中一行中第几个单元格
string spname =dr.Cells[1].Value.ToString().Trim();
string sptime =dr.Cells[2].Value.ToString();
string spmark = dr.Cells["备注"].Value.ToString();
//获取值以后传递到另一个窗体中来显示
frmUpdateSpecialty frmsp = newfrmUpdateSpecialty(spid, spname, sptime, spmark);
// MessageBox.Show(spid + spname+ sptime + spmark);
frmsp.MdiParent = MdiParent;
frmsp.Show();
删除
// 获取当前选中的行
DataGridViewRow dr;//声明一个当前行的字段
if (this.dgv.CurrentRow != null)//判断是否选中行
{
dr = dgv.CurrentRow;//获取了当前行的值
string spid = dr.Cells["编号"].Value.ToString();//获取选中一行中第几个单元格
string strsql = "delete from specialityinfo where specialityid='" + spid + "'";
try
{
SqlConnection sqlcon = new SqlConnection(DbHelper.strcon);
SqlCommand cmd = new SqlCommand(strsql, sqlcon);
sqlcon.Open();
int i =cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("删除成功");
}
sqlcon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
确定 添加 按钮
string spname = this.tbSpecialtyName.Text.ToString().Trim();
string spmark = this.rtbSpecialtyRemark.Text.ToString();
string spdate = DateTime.Now.ToString();
string strsql = "insert intoSpecialityInfo(specialityname,remark,adddate)values('" + spname + "','" + spmark + "','" + spdate + "')";
//连接数据库
try
{
SqlCommand cmd = new SqlCommand(strsql, sqlcon);
//
sqlcon.Open();
int iresult =cmd.ExecuteNonQuery();
if (iresult > 0)
{
MessageBox.Show("添加专业成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
this.tbSpecialtyName.Text= "";
this.rtbSpecialtyRemark.Text= "";
}
sqlcon.Close();
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
// throw;
}
finally
{
sqlcon.Close();
}
浏览照片
//浏览图片
private void btBrowsePic_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//ofd.Filter ="(*BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files(*.*)|*.*";
//ofd.Filter ="Bmp|*.BMP|jpg|*.JPG|gif|*.CIF|All files(*.*)|*.*";
ofd.Filter = "Bmp|*.BMP|jpg|*.JPG|gif|*.GIF|Png|*.png|Allfiles(*.*)|*.*";//过滤文件类型
ofd.FilterIndex = 2;//指定索引文件类型位置
ofd.ShowDialog();//打开对话框
if (ofd.ShowDialog ()== DialogResult.OK)//判断对话框是否点击了打开按钮
{
try
{
//MessageBox.Show("你选择了文件" + ofd.FileName);
pbPic.ImageLocation =ofd.FileName;//绑定选择的图片
}
catch (Exception ex)
{
MessageBox.Show("Error:Couldnor read file from disk.Original error:"+ex.Message);
}
}
}
///
///图片转换二进制方法
///
///
///
public Byte [] Convertphoto(string strphonto)
{
FileStream file = new FileStream(strphonto, FileMode.Open, FileAccess.Read);
Byte[] bytes = new Byte[file.Length];
file .Read (bytes,0,bytes.Length);
file .Close ();
return bytes ;
}
专业列表:
//载入专业列表
private voidfrmBrowseStudent_Load(object sender, EventArgs e)
{
string strsql = "selectspecialityid,[specialityname] from [specialityinfo];select * fromclassinfo";
//数据配置器填充数据集
da = new SqlDataAdapter(strsql, sqlcon);
// 填充数据集
ds = new DataSet();
da.Fill(ds, "studentinfo");
da.Fill(ds, "studentid");
//da.Fill(ds,"studentinfo");
// 指定值和名称
cbSpecialty.ValueMember = "specialtyid";//值隐藏的
cbSpecialty.DisplayMember = "specialityname";//名显示的
cbClass.ValueMember = "studentid";
cbClass.DisplayMember = "name";
cbSpecialty.DataSource =ds.Tables[0];
//通过绑定数据集中的第几个表
dataGridView1.DataSource =ds.Tables[1].DefaultView; //datafaultview
}
//
orderby studentnumber()
浏览中的修改按钮:
DataGridViewRow dr;
dr = dataGridView1.CurrentRow;
if (dr != null)
{
string studentNum =dr.Cells[1].Value.ToString().Trim();
frmUpdateStudent frmUpate = new frmUpdateStudent(studentNum);
frmUpate.MdiParent = this.MdiParent;
frmUpate.Show();
//开始查询
string szy =cbSpecialty.Text.ToString().Trim();
string ssj = dtpPicker.Value.ToString("yyyy-MM-dd");
//string ssj =string.Format(dtbPicker.Value.ToString(), "yyyyy-MM-dd");
string strsql = "select * fromstudentinfo where 1=1";
if (szy != "")
{
strsql += " andname='" +szy + "'";
}
if (ssj != "")
{
strsql += " andpostalcode='" +ssj + "'";
}
da = new SqlDataAdapter(strsql, sqlcon);
ds = new DataSet();
da.Fill(ds, "studentid");
dataGridView1.DataSource =ds.Tables[0].DefaultView;//DefaultView默认视图
窗体初始化liewtree
private voidfrmBrowseCourse_Load(object sender, EventArgs e)
{
//读取专业信息
stringstrsqlspecialityinfo = "select specialityname from specialityinfo";
//实例化cmd
cmd = new SqlCommand(strsqlspecialityinfo,sqlcon);
//打开数据库
sqlcon.Open();
//读取专业信息并绑定到treeview 上
dr = cmd.ExecuteReader();
//判断是否有数据
if(dr.HasRows)
{
while(dr.Read())
{
//通过循环绑定treeview的节点
TreeNode sNode = new TreeNode();//是通过new实例化
sNode.Text = dr["specialityname"].ToString();//给节点赋值
this.treeView1.Nodes.Add(sNode);
//根据专业读取班级信息
string strsqlclassinfo = "selectclassid, classname from classinfo where specialtyname ='" +sNode.Text.ToString().Trim() + "'";
// sqlcon.Close();
// stringstrsqlclassinfo = "select classid, classname from classinfo where specialtyname ='" +dr["specialityname"].ToString() + "'";
//执行班级信息查询
cmdclass = new SqlCommand(strsqlclassinfo,sqlconclass);
sqlconclass.Open();
drclass =cmdclass.ExecuteReader();
if (drclass.HasRows)
{
while (drclass.Read())
{
TreeNode sNodeclass = new TreeNode();//是通过new实例化
sNodeclass.Text =drclass["classname"].ToString().Trim();
sNode.Nodes.Add(sNodeclass);
}
drclass.Close();
}
sqlconclass.Close();
}
dr.Close();//必须立即关闭 非常重要
}
//关闭数据库
sqlcon.Close();
//点击课程信息后把学生信息传递到右边显示框
private voidlvCourse_SelectedIndexChanged(object sender, EventArgs e)
{
tbCourse.Text =lvCourse.Items[lvCourse.FocusedItem.Index].Text.Trim();
}