⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 原代码详细说明是关于c++方面的希望可以帮助大家使用
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Exam6_4
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.button3 = new System.Windows.Forms.Button();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(48, 10);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(92, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 14);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(32, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "学号";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 42);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(39, 23);
			this.label2.TabIndex = 3;
			this.label2.Text = "姓名";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(47, 38);
			this.textBox2.Name = "textBox2";
			this.textBox2.ReadOnly = true;
			this.textBox2.Size = new System.Drawing.Size(92, 21);
			this.textBox2.TabIndex = 2;
			this.textBox2.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 70);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(34, 23);
			this.label3.TabIndex = 5;
			this.label3.Text = "性别";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(48, 66);
			this.textBox3.Name = "textBox3";
			this.textBox3.ReadOnly = true;
			this.textBox3.Size = new System.Drawing.Size(37, 21);
			this.textBox3.TabIndex = 4;
			this.textBox3.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 98);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(38, 23);
			this.label4.TabIndex = 7;
			this.label4.Text = "地址";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(47, 94);
			this.textBox4.Name = "textBox4";
			this.textBox4.ReadOnly = true;
			this.textBox4.Size = new System.Drawing.Size(126, 21);
			this.textBox4.TabIndex = 6;
			this.textBox4.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 128);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(56, 28);
			this.label5.TabIndex = 9;
			this.label5.Text = "入学成绩";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(66, 122);
			this.textBox5.Name = "textBox5";
			this.textBox5.ReadOnly = true;
			this.textBox5.Size = new System.Drawing.Size(67, 21);
			this.textBox5.TabIndex = 8;
			this.textBox5.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(4, 153);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(50, 22);
			this.button1.TabIndex = 19;
			this.button1.Text = "添加";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(58, 153);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(50, 22);
			this.button2.TabIndex = 20;
			this.button2.Text = "修改";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2,
																						this.columnHeader3,
																						this.columnHeader4,
																						this.columnHeader5});
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(177, 3);
			this.listView1.MultiSelect = false;
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(291, 172);
			this.listView1.TabIndex = 21;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(112, 153);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(50, 22);
			this.button3.TabIndex = 22;
			this.button3.Text = "删除";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "学号";
			this.columnHeader1.Width = 56;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "姓名";
			this.columnHeader2.Width = 66;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "性别";
			this.columnHeader3.Width = 44;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "地址";
			this.columnHeader4.Width = 57;
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "入学成绩";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(461, 180);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "学生数据维护";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		
	    SqlConnection myConnection;
        SqlCommand sqlCommand;        
		private void Form1_Load(object sender, System.EventArgs e)
		{
			myConnection = new SqlConnection();
			myConnection.ConnectionString = "server=localhost;uid=sa;pwd=;database=xsgl";
			sqlCommand=new SqlCommand();
			sqlCommand.Connection=myConnection;
			sqlCommand.CommandType = CommandType.Text;
			sqlCommand.CommandText = "select * from student";
			myConnection.Open();
			SqlDataReader dReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
			while(dReader.Read())
			{  
				ListViewItem newItem=listView1.Items.Add(dReader["studID"].ToString().Trim());
				newItem.SubItems.Add(dReader["studName"].ToString().Trim());
				newItem.SubItems.Add(dReader["studSex"].ToString().Trim());
				newItem.SubItems.Add(dReader["studAddress"].ToString().Trim());
				newItem.SubItems.Add(dReader["enterScore"].ToString().Trim());
			}
			dReader.Close();
		}

		private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems.Count>0)
			{
				textBox1.Text=listView1.SelectedItems[0].Text;
				textBox2.Text=listView1.SelectedItems[0].SubItems[1].Text;
				textBox3.Text=listView1.SelectedItems[0].SubItems[2].Text;
				textBox4.Text=listView1.SelectedItems[0].SubItems[3].Text;
				textBox5.Text=listView1.SelectedItems[0].SubItems[4].Text;
			}
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			if(button1.Text=="添加")
			{
				button1.Text="保存";
				textBox1.Text="";textBox2.Text="";
				textBox3.Text="";textBox4.Text="";
				textBox5.Text="";textBox1.Focus();
				textBox1.ReadOnly=false;
				textBox2.ReadOnly=false;
				textBox3.ReadOnly=false;
				textBox4.ReadOnly=false;
				textBox5.ReadOnly=false;
				button2.Enabled=false;
				button3.Enabled=false;
			}
			else
			{
				sqlCommand.CommandText = "insert into student values('"
					+textBox1.Text+"','"
					+textBox2.Text+"','"
					+textBox3.Text+"','"
					+textBox4.Text+"',"
					+textBox5.Text+")";
				myConnection.Open();
				int cmdresults = sqlCommand.ExecuteNonQuery();
				myConnection.Close();
				if(cmdresults==1)
				{
					textBox1.ReadOnly=true;
					textBox2.ReadOnly=true;
					textBox3.ReadOnly=true;
					textBox4.ReadOnly=true;
					textBox5.ReadOnly=true;
					button2.Enabled=true;
					button3.Enabled=true;
					ListViewItem newItem=listView1.Items.Add(textBox1.Text);
					newItem.SubItems.Add(textBox2.Text);
					newItem.SubItems.Add(textBox3.Text);
					newItem.SubItems.Add(textBox4.Text);
					newItem.SubItems.Add(textBox5.Text);
					button1.Text="添加";
					MessageBox.Show("插入数据成功!");
				}
				else
					MessageBox.Show("插入数据失败!");
			}
		}
		private void button2_Click(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems.Count>0)
			{
				if(button2.Text=="修改")
				{
					button2.Text="保存";
					textBox2.ReadOnly=false;
					textBox3.ReadOnly=false;
					textBox4.ReadOnly=false;
					textBox5.ReadOnly=false;
					button2.Enabled=false;
					button3.Enabled=false;
					textBox2.Focus();
				}  
				else
				{
					string studID=listView1.SelectedItems[0].Text;
					sqlCommand.CommandText = "update student set studName='"+textBox2.Text+"',"+
						"studSex='"+textBox3.Text+"',"+
						"studAddress='"+textBox4.Text+"',"+
						"enterScore="+textBox5.Text+ 
						" where studID='"+studID+"'";
					myConnection.Open();
					int cmdresults=sqlCommand.ExecuteNonQuery();
					myConnection.Close();
					if(cmdresults==1)
					{
						textBox2.ReadOnly=true;
						textBox3.ReadOnly=true;
						textBox4.ReadOnly=true;
						textBox5.ReadOnly=true;
						button2.Enabled=true;
						button3.Enabled=true;
						listView1.SelectedItems[0].SubItems[1].Text=textBox2.Text;
						listView1.SelectedItems[0].SubItems[2].Text=textBox3.Text;
						listView1.SelectedItems[0].SubItems[3].Text=textBox4.Text;
						listView1.SelectedItems[0].SubItems[4].Text=textBox5.Text;
						button2.Text="修改";
						MessageBox.Show("修改数据成功!");
					}
					else
						MessageBox.Show("修改数据失败!");
				}
			}
		}
		private void button3_Click(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems.Count>0)
			{
				string studID=listView1.SelectedItems[0].Text;
				sqlCommand.CommandText = "delete from student where studID='"+studID+"'";
				myConnection.Open();
				sqlCommand.ExecuteNonQuery();
                myConnection.Close();
				listView1.Items.Remove(listView1.SelectedItems[0]);
				textBox1.Text="";
				textBox2.Text="";
				textBox3.Text="";
				textBox4.Text="";

			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -