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

📄 form1.cs

📁 这是用C#数据库编程的实例
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//引入数据库操作类命名空间
using System.Data.OleDb;
//引入ADO.NET操作命名空间

namespace 图书管理系统
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.TextBox TxtBookName;
		internal System.Windows.Forms.TextBox TxtState;
		internal System.Windows.Forms.TextBox TxtBookID;
		internal System.Windows.Forms.Label Label3;
		internal System.Windows.Forms.Label Label2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.ToolBar ToolBar1;
		internal System.Windows.Forms.ToolBarButton TBfirst;
		internal System.Windows.Forms.ToolBarButton TBpre;
		internal System.Windows.Forms.ToolBarButton TBNext;
		internal System.Windows.Forms.ToolBarButton TBlast;
		internal System.Windows.Forms.ToolBarButton ToolBarButton5;
		internal System.Windows.Forms.ToolBarButton ToolBarButton6;
		internal System.Windows.Forms.ToolBarButton TBExit;
		internal System.Windows.Forms.ImageList ImageList1;
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Button BtSave;
		internal System.Windows.Forms.Button BtEdit;
		internal System.Windows.Forms.Button BtDel;
		internal System.Windows.Forms.Button BtAdd;
		internal System.Windows.Forms.GroupBox GroupBox3;
		internal System.Windows.Forms.GroupBox GroupBox5;
		internal System.Windows.Forms.DataGrid GRDbooks;
		internal System.Windows.Forms.GroupBox GroupBox6;
		internal System.Windows.Forms.Button BtSearchID;
		internal System.Windows.Forms.TextBox txtSearchID;
		internal System.Windows.Forms.Label Label6;
		internal System.Windows.Forms.GroupBox GroupBox4;
		internal System.Windows.Forms.Label Label5;
		internal System.Windows.Forms.Label Label4;
		internal System.Windows.Forms.Button BtSearch;
		internal System.Windows.Forms.TextBox txtSearch;
		internal System.Windows.Forms.ComboBox Cmbtype;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			OpenDb();
			//应用数据库连接函数,打开数据连接
			TxtBookID.Text = myrow[0].ToString();
			//显示图书编号
			TxtBookName.Text = myrow[1].ToString();
			//显示图书名称
			TxtState.Text = myrow[2].ToString();
			//显示图书当前状态
		}

		OleDbConnection myConnection;
		//建立OleDbConnection连接对象
		OleDbDataAdapter myAdapter;
		//建立OleDbDataAdapter对象 
		OleDbCommand myCmd;
		//建立OleDbCommand对象
		DataSet ds;
		//建立DataSet对象 
		DataTable mytable ;
		//建立表单对象
		DataRow myrow;
		//建立数据行对象
		int rownumber;
		//定义一个整型变量来存放当前行数
		public bool OpenDb()
		{
			String SQLstr = "";
			//定义SQL查询字符串
			SQLstr = "select * from tblbooks";
			//建立SQL查询语句
			try
			{
				myConnection=new OleDbConnection("Provider="
					+"Microsoft.Jet.OLEDB.4.0;Data Source=lib.mdb");
				//建立连接对象
				myAdapter = new OleDbDataAdapter(SQLstr,myConnection);
				//建立ADODataSetCommand对象
				ds=new DataSet();
				//创建数据集
				myAdapter.Fill(ds, "tblbooks");
				//取得表单
				mytable = ds.Tables[0];
				//取得tblbooks表
				rownumber = 0;
				//设置为起始行
				myrow = mytable.Rows[rownumber];
				//取得第1行数据
			}
			catch(Exception e)
			{
				MessageBox.Show(e.ToString(),"错误",MessageBoxButtons.OK);
				return false;
			}
			return true;
		}

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

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.TxtBookName = new System.Windows.Forms.TextBox();
			this.TxtState = new System.Windows.Forms.TextBox();
			this.TxtBookID = new System.Windows.Forms.TextBox();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.ToolBar1 = new System.Windows.Forms.ToolBar();
			this.TBfirst = new System.Windows.Forms.ToolBarButton();
			this.TBpre = new System.Windows.Forms.ToolBarButton();
			this.TBNext = new System.Windows.Forms.ToolBarButton();
			this.TBlast = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButton5 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButton6 = new System.Windows.Forms.ToolBarButton();
			this.TBExit = new System.Windows.Forms.ToolBarButton();
			this.ImageList1 = new System.Windows.Forms.ImageList(this.components);
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.BtSave = new System.Windows.Forms.Button();
			this.BtEdit = new System.Windows.Forms.Button();
			this.BtDel = new System.Windows.Forms.Button();
			this.BtAdd = new System.Windows.Forms.Button();
			this.GroupBox3 = new System.Windows.Forms.GroupBox();
			this.GroupBox5 = new System.Windows.Forms.GroupBox();
			this.GRDbooks = new System.Windows.Forms.DataGrid();
			this.GroupBox6 = new System.Windows.Forms.GroupBox();
			this.BtSearchID = new System.Windows.Forms.Button();
			this.txtSearchID = new System.Windows.Forms.TextBox();
			this.Label6 = new System.Windows.Forms.Label();
			this.GroupBox4 = new System.Windows.Forms.GroupBox();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.BtSearch = new System.Windows.Forms.Button();
			this.txtSearch = new System.Windows.Forms.TextBox();
			this.Cmbtype = new System.Windows.Forms.ComboBox();
			this.GroupBox1.SuspendLayout();
			this.GroupBox2.SuspendLayout();
			this.GroupBox3.SuspendLayout();
			this.GroupBox5.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.GRDbooks)).BeginInit();
			this.GroupBox6.SuspendLayout();
			this.GroupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.TxtBookName,
																					this.TxtState,
																					this.TxtBookID,
																					this.Label3,
																					this.Label2,
																					this.Label1});
			this.GroupBox1.Location = new System.Drawing.Point(8, 40);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(272, 120);
			this.GroupBox1.TabIndex = 2;
			this.GroupBox1.TabStop = false;
			// 
			// TxtBookName
			// 
			this.TxtBookName.Location = new System.Drawing.Point(112, 48);
			this.TxtBookName.Name = "TxtBookName";
			this.TxtBookName.Size = new System.Drawing.Size(96, 21);
			this.TxtBookName.TabIndex = 1;
			this.TxtBookName.Text = "";
			// 
			// TxtState
			// 
			this.TxtState.Location = new System.Drawing.Point(112, 80);
			this.TxtState.Name = "TxtState";
			this.TxtState.Size = new System.Drawing.Size(96, 21);
			this.TxtState.TabIndex = 1;
			this.TxtState.Text = "";
			// 
			// TxtBookID
			// 
			this.TxtBookID.Location = new System.Drawing.Point(112, 16);
			this.TxtBookID.Name = "TxtBookID";
			this.TxtBookID.Size = new System.Drawing.Size(96, 21);
			this.TxtBookID.TabIndex = 1;
			this.TxtBookID.Text = "";
			// 
			// Label3
			// 
			this.Label3.Location = new System.Drawing.Point(32, 80);
			this.Label3.Name = "Label3";
			this.Label3.Size = new System.Drawing.Size(56, 16);
			this.Label3.TabIndex = 0;
			this.Label3.Text = "图书状态";
			// 
			// Label2
			// 
			this.Label2.Location = new System.Drawing.Point(32, 56);
			this.Label2.Name = "Label2";
			this.Label2.Size = new System.Drawing.Size(56, 16);
			this.Label2.TabIndex = 0;
			this.Label2.Text = "图书名称";
			// 
			// Label1
			// 
			this.Label1.Location = new System.Drawing.Point(32, 24);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(56, 16);
			this.Label1.TabIndex = 0;
			this.Label1.Text = "图书编号";
			// 
			// ToolBar1
			// 
			this.ToolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.TBfirst,
																						this.TBpre,
																						this.TBNext,
																						this.TBlast,
																						this.ToolBarButton5,
																						this.ToolBarButton6,
																						this.TBExit});
			this.ToolBar1.DropDownArrows = true;
			this.ToolBar1.ImageList = this.ImageList1;
			this.ToolBar1.Name = "ToolBar1";
			this.ToolBar1.ShowToolTips = true;
			this.ToolBar1.Size = new System.Drawing.Size(292, 25);
			this.ToolBar1.TabIndex = 3;
			this.ToolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.ToolBar1_ButtonClick);
			// 
			// TBfirst
			// 
			this.TBfirst.ImageIndex = 0;
			this.TBfirst.ToolTipText = "显示第一条";
			// 
			// TBpre
			// 
			this.TBpre.ImageIndex = 1;
			this.TBpre.ToolTipText = "显示上一条";
			// 
			// TBNext
			// 
			this.TBNext.ImageIndex = 2;
			this.TBNext.ToolTipText = "显示下一条";
			// 
			// TBlast
			// 
			this.TBlast.ImageIndex = 3;
			// 
			// ToolBarButton5
			// 
			this.ToolBarButton5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButton6
			// 
			this.ToolBarButton6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// TBExit
			// 
			this.TBExit.ImageIndex = 4;
			// 
			// ImageList1
			// 
			this.ImageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.ImageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.ImageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ImageList1.ImageStream")));
			this.ImageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.BtSave,
																					this.BtEdit,
																					this.BtDel,
																					this.BtAdd});
			this.GroupBox2.Location = new System.Drawing.Point(8, 176);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(272, 48);
			this.GroupBox2.TabIndex = 4;
			this.GroupBox2.TabStop = false;
			// 
			// BtSave
			// 
			this.BtSave.Location = new System.Drawing.Point(200, 16);
			this.BtSave.Name = "BtSave";
			this.BtSave.Size = new System.Drawing.Size(64, 23);
			this.BtSave.TabIndex = 0;
			this.BtSave.Text = "保存";
			this.BtSave.Click += new System.EventHandler(this.BtSave_Click);
			// 
			// BtEdit
			// 
			this.BtEdit.Location = new System.Drawing.Point(136, 16);
			this.BtEdit.Name = "BtEdit";
			this.BtEdit.Size = new System.Drawing.Size(64, 23);
			this.BtEdit.TabIndex = 0;
			this.BtEdit.Text = "编辑";
			this.BtEdit.Click += new System.EventHandler(this.BtEdit_Click);
			// 
			// BtDel
			// 
			this.BtDel.Location = new System.Drawing.Point(72, 16);
			this.BtDel.Name = "BtDel";
			this.BtDel.Size = new System.Drawing.Size(64, 23);
			this.BtDel.TabIndex = 0;
			this.BtDel.Text = "删除";
			this.BtDel.Click += new System.EventHandler(this.BtDel_Click);
			// 
			// BtAdd
			// 
			this.BtAdd.Location = new System.Drawing.Point(8, 16);
			this.BtAdd.Name = "BtAdd";
			this.BtAdd.Size = new System.Drawing.Size(64, 23);
			this.BtAdd.TabIndex = 0;
			this.BtAdd.Text = "添加";
			this.BtAdd.Click += new System.EventHandler(this.BtAdd_Click);
			// 
			// GroupBox3
			// 
			this.GroupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.GroupBox5,
																					this.GroupBox4});
			this.GroupBox3.Location = new System.Drawing.Point(8, 232);
			this.GroupBox3.Name = "GroupBox3";
			this.GroupBox3.Size = new System.Drawing.Size(272, 256);
			this.GroupBox3.TabIndex = 5;
			this.GroupBox3.TabStop = false;
			// 
			// GroupBox5
			// 
			this.GroupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.GRDbooks,
																					this.GroupBox6});
			this.GroupBox5.Location = new System.Drawing.Point(8, 80);
			this.GroupBox5.Name = "GroupBox5";
			this.GroupBox5.Size = new System.Drawing.Size(256, 168);
			this.GroupBox5.TabIndex = 1;
			this.GroupBox5.TabStop = false;
			// 
			// GRDbooks
			// 
			this.GRDbooks.DataMember = "";
			this.GRDbooks.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.GRDbooks.Location = new System.Drawing.Point(8, 64);
			this.GRDbooks.Name = "GRDbooks";
			this.GRDbooks.Size = new System.Drawing.Size(240, 96);
			this.GRDbooks.TabIndex = 1;
			// 
			// GroupBox6
			// 
			this.GroupBox6.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.BtSearchID,
																					this.txtSearchID,
																					this.Label6});
			this.GroupBox6.Location = new System.Drawing.Point(8, 8);
			this.GroupBox6.Name = "GroupBox6";
			this.GroupBox6.Size = new System.Drawing.Size(240, 48);
			this.GroupBox6.TabIndex = 0;
			this.GroupBox6.TabStop = false;
			// 
			// BtSearchID
			// 
			this.BtSearchID.Location = new System.Drawing.Point(168, 16);
			this.BtSearchID.Name = "BtSearchID";
			this.BtSearchID.Size = new System.Drawing.Size(64, 24);
			this.BtSearchID.TabIndex = 2;
			this.BtSearchID.Text = "查询";
			this.BtSearchID.Click += new System.EventHandler(this.BtSearchID_Click);
			// 
			// txtSearchID
			// 
			this.txtSearchID.Location = new System.Drawing.Point(80, 16);
			this.txtSearchID.Name = "txtSearchID";
			this.txtSearchID.Size = new System.Drawing.Size(72, 21);
			this.txtSearchID.TabIndex = 1;
			this.txtSearchID.Text = "";
			// 
			// Label6
			// 
			this.Label6.Location = new System.Drawing.Point(8, 17);
			this.Label6.Name = "Label6";
			this.Label6.Size = new System.Drawing.Size(72, 16);
			this.Label6.TabIndex = 0;
			this.Label6.Text = "安图书编号查询";
			// 
			// GroupBox4
			// 
			this.GroupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.Label5,
																					this.Label4,

⌨️ 快捷键说明

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