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());
}
}
}
}
网友评论