📄 form8.cs
字号:
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 团委组织部信息系统
{
public partial class Form8 : Form
{
SqlDataAdapter da;
DataSet ds;
string connectionstring = @"data source=localhost;Initial catalog=tuanweizuzhibu_inf;integrated security=SSPI;";
string commandstring = @"select * from work_inf";
public Form8()
{
InitializeComponent();
}
private void work_infBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.work_infBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.tuanweizuzhibu_infDataSet);
}
private void Form8_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“tuanweizuzhibu_infDataSet.work_inf”中。您可以根据需要移动或移除它。
this.work_infTableAdapter.Fill(this.tuanweizuzhibu_infDataSet.work_inf);
}
private void button4_Click(object sender, EventArgs e)
{
string connectionstring = @"data source=localhost;Initial catalog=tuanweizuzhibu_inf;integrated security=SSPI;";
string commandstring = @"select * from work_inf where Year = '" + comboBox1.Text + "' and Months = '" + comboBox4.Text + "' ";
da = new SqlDataAdapter(commandstring, connectionstring);
ds = new DataSet();
da.Fill(ds, "work_inf");
work_infDataGridView.DataSource = ds.Tables["work_inf"];
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
private void button6_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button3_Click_1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connectionstring);
string sqlStatement = "UPDATE work_inf SET " +
"Year='" + yearTextBox.Text + "'," +
"Months='" + monthsTextBox.Text + "'," +
"Day='" + dayTextBox.Text + "'," +
"Event='" + eventTextBox.Text + "'," +
"Maneger='" + manegerTextBox.Text + "'" +
"WHERE Year = '" + yearTextBox.Text + "'";
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
try
{
conn.Open();
int rowAffected = cmd.ExecuteNonQuery();
if (rowAffected == 1)
{
da = new SqlDataAdapter(commandstring, connectionstring);
ds = new DataSet();
da.Fill(ds, "work_inf");
MessageBox.Show("修改成功!");
work_infDataGridView.DataSource = ds.Tables["work_inf"];
yearTextBox.Text = "";
monthsTextBox.Text = "";
dayTextBox.Text = "";
eventTextBox.Text = "";
manegerTextBox.Text = "";
}
}
catch (SqlException ex)
{
MessageBox.Show("修改失败:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (yearTextBox.Text == "" || monthsTextBox.Text == "" || dayTextBox.Text == "" || eventTextBox.Text == "" || manegerTextBox.Text == "" )
{ MessageBox.Show("请选择要删除的对象!"); }
else
{
da = new SqlDataAdapter(commandstring, connectionstring);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "work_inf");
work_infDataGridView.DataSource = ds.Tables["work_inf"];
DataTable mytable = ds.Tables["work_inf"];
mytable.Rows[work_infDataGridView.CurrentRow.Index].Delete();
da.Update(ds, "work_inf");
ds.AcceptChanges();
MessageBox.Show("信息删除成功!");
work_infDataGridView.DataSource = ds.Tables["work_inf"];
yearTextBox.Text = "";
monthsTextBox.Text = "";
dayTextBox.Text = "";
eventTextBox.Text = "";
manegerTextBox.Text = "";
}
}
private void button6_Click_1(object sender, EventArgs e)
{
if (manegerTextBox.Text == "")
{ MessageBox.Show(" 内容不能为空!"); }
else
{
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(commandstring, conn);
SqlDataReader dr;
int prime = 0;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.GetString(0) == manegerTextBox.Text)
{
MessageBox.Show("不能添加相同的工作信息!");
prime = 1;
break;
}
}
if (prime == 0)
try
{
da = new SqlDataAdapter(commandstring, connectionstring);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "work_inf");
DataTable mytable = ds.Tables["work_inf"];
DataRow myrow = ds.Tables["work_inf"].NewRow();
myrow[0] = yearTextBox.Text.ToString();
myrow[1] = monthsTextBox.Text.ToString();
myrow[2] = dayTextBox.Text.ToString();
myrow[3] = eventTextBox.Text.ToString();
myrow[4] = manegerTextBox.Text.ToString();
mytable.Rows.Add(myrow);
da.Update(ds, "work_inf");
ds.AcceptChanges();
MessageBox.Show("添加成功!");
work_infDataGridView.DataSource = ds.Tables["work_inf"];
}
catch (SqlException ex)
{
MessageBox.Show("添加失败:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
dr.Close();
conn.Close();
}
}
}
private void button5_Click_1(object sender, EventArgs e)
{
yearTextBox.Text = "";
monthsTextBox.Text = "";
dayTextBox.Text = "";
eventTextBox.Text = "";
manegerTextBox.Text = "";
}
private void work_infDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
yearTextBox.Text = work_infDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
monthsTextBox.Text = work_infDataGridView.Rows[e.RowIndex].Cells[1].Value.ToString();
dayTextBox.Text = work_infDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString();
eventTextBox.Text = work_infDataGridView.Rows[e.RowIndex].Cells[3].Value.ToString();
manegerTextBox.Text = work_infDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
}
private void yearTextBox_TextChanged(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -