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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
SqlConnection mycon = new SqlConnection(strCon);
mycon.Open();
string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";
SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
DataTable dt = new DataTable();
myda.Fill(dt);
dataGridView1.DataSource = dt;
mycon.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "增加")
{
try
{
// string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
string cno = textcno.Text;
string cname = textcname.Text;
string Ccode = textcode.Text;
SqlConnection mycon = new SqlConnection(strCon);
mycon.Open();
//string StrUpd = "update T_Student set sage='" + sage + "' where sname='" + sname + "'";
string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
//string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept) values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";
SqlCommand mycmd = new SqlCommand(strins, mycon);
mycmd.ExecuteNonQuery();
string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";
SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
DataTable dt = new DataTable();
myda.Fill(dt);
dataGridView1.DataSource = dt;
mycon.Close();
MessageBox.Show("增加成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
try
{
// string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
string cno = textcno.Text;
string cname = textcname.Text;
string Ccode = textcode.Text;
SqlConnection mycon = new SqlConnection(strCon);
mycon.Open();
string StrUpd = "update T_course set cname='" + cname + "' , Ccode='" + Ccode + "' where cno='" + cno + "'";
//string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
//string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept) values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";
SqlCommand mycmd = new SqlCommand(StrUpd, mycon);
mycmd.ExecuteNonQuery();
string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";
SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
DataTable dt = new DataTable();
myda.Fill(dt);
dataGridView1.DataSource = dt;
mycon.Close();
MessageBox.Show("修改成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
textcno.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textcname.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textcode.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
button1.Text = "修改";
textcno.Enabled = false;
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (DialogResult.Yes == MessageBox.Show("确定要删除这条记录吗?","", MessageBoxButtons.YesNo))
{
// string strCon = "Data Source =.\\sqlexpress;Initial Catalog=XSGL;Integrated Security=True";
//string cno = textcno.Text;
//string cname = textcname.Text;
//string Ccode = textcode.Text;
SqlConnection mycon = new SqlConnection(strCon);
mycon.Open();
string cno = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string strins = "delete from T_course where cno ='"+cno+"'";
//string StrUpd = "update T_course set cname='" + cname + "' , Ccode='" + Ccode + "' where cno='" + cno + "'";
//string strins = "insert into T_course(cno,cname,Ccode) values ('" + cno + "','" + cname + "','" + Ccode + "')";
//string StrUpd = "insert into T_Student(Sno,Sname,Ssex,Sage,Sdept) values ('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sdept+"')";
SqlCommand mycmd = new SqlCommand(strins, mycon);
mycmd.ExecuteNonQuery();
string strSel = "select Cno as 课程号,Cname as 课程名,Ccode as 学分 from T_Course";
SqlDataAdapter myda = new SqlDataAdapter(strSel, mycon);
DataTable dt = new DataTable();
myda.Fill(dt);
this.dataGridView1.DataSource = dt;
mycon.Close();
MessageBox.Show("删除成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
第三次实验课
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...
- 怎样反思自己的教学——学习杜威《我们怎样思维》有感 2015-01-13 14:56:52 来源:普宁市第三中学网...