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

📄 form1.cs

📁 这是用C#数据库编程的实例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace databrowser
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		/// <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 Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(80, 24);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(80, 64);
			this.button2.Name = "button2";
			this.button2.TabIndex = 1;
			this.button2.Text = "button2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(80, 104);
			this.button3.Name = "button3";
			this.button3.TabIndex = 2;
			this.button3.Text = "button3";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(80, 144);
			this.button4.Name = "button4";
			this.button4.TabIndex = 3;
			this.button4.Text = "button4";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button4,
																		  this.button3,
																		  this.button2,
																		  this.button1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion



		private void button1_Click(object sender, System.EventArgs e)
		{
			CAccessDatabase temp=new CAccessDatabase();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			CAccessDatabase temp=new CAccessDatabase("Provider=" 
				+"Microsoft.Jet.OLEDB.4.0;Data Source=C:\\school.mdb;"
				+"Jet OLEDB:System database=F:\\Program Files\\Microsoft"
				+" Office\\Office\\SYSTEM.MDW;User ID =Admin;Password =;");
			//Access类对象
			if(temp.Connect())
			{
				//如果连接成功则显示对话框
				MessageBox.Show("连接成功", "连接试验", MessageBoxButtons.OK);
			}
			System.Data.DataSet ds=null;
			String str="";
			//定义字符串,用来存取表单
			if(temp.ExecuteSQL("SELECT 课程信息表.科目编号, 课程信息表.科目名称,"
				+" 课程信息表.分数 FROM 课程信息表;", ref ds, ref str))
			{
				MessageBox.Show("查询建立成功", "建立查询",MessageBoxButtons.OK);
				String str1;
				//定义字符串,用来存取表单
				System.Data.DataTable table;
				//定义表单变量
				table = ds.Tables[0];
				//读取第一张表单
				System.Data.DataRow row;
				//定义数据行变量
				try
				{
					row = table.Rows[0];
				}
				catch
				{
					MessageBox.Show(e.ToString());
					//显示错误信息
					return;
				}
				//读取第一行
				str1 = row.ItemArray[1].ToString();
				//读取第一行的第二列
				MessageBox.Show(str1
					, "读取表单一中的第一行第二列",MessageBoxButtons.OK);
				//显示表单数据
			}
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			CDatabase cd1=new CDatabase();
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			CDatabase cd1=new CDatabase("Provider=SQLOLEDB.1;"
				+"Integrated Security=SSPI;Persist Security Info=False;"
				+"User ID=administrator;Initial Catalog=Northwind;"
				+"Data Source=JACK;Use Procedure for Prepare=1;"
				+"Auto Translate=True;Packet Size=4096;Workstation ID=JACK;"
				+"Use Encryption for Data=False;Tag with column "
				+"collation when possible=False");
			if(cd1.Connect())
			{
				//如果连接成功则显示对话框
				MessageBox.Show("连接成功", "连接试验", MessageBoxButtons.OK);
			}
		}
		 
	}
}

⌨️ 快捷键说明

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