All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ADO.NET打开数据库链接

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/9 1:35:09 Browse times: 326 Comment times: 0

ADO.NET打开数据库链接


Head photo

Go homepage
Upload pictures
Write articles

ADO.NET打开数据库链接

打开数据库连接
通过SqlConnection类完成数据库(SQL Server)的连接。将与后台数据库有关的操作放在一个类内。程序运行结果如图1与图2所示。

图1 连接数据库窗体

图2 数据库状态提示窗体
程序开发步骤如下。
(1)打开VS2005,然后新建一个应用程序(OptDB)。
(2)在解决方案管理器内添加一个文件夹(ClsDB)用于存放操作数据库的类,并在该文件夹中添加一个类(ClsDBControl.cs),用于返回一个打开的数据库连接对象。
(3)主要程序程序代码如下。
在类文件ClsDBControl中,自定义一个ConDB方法,用于打开与数据库的连接并返回一个SqlConnection对象。其程序代码如下:
private SqlConnection con;
public SqlConnection ConDB()
{
con = new SqlConnection("server=.;uid=sa;pwd=;database= DB_ADONET");
if (con.State == ConnectionState.Closed)
{
con.Open();
}
return con;//返回SqlConnection对象
}
在表示层中,通过调用ConDB方法来实现数据库的连接。程序代码如下:
private void button1_Click(object sender, EventArgs e)
{
ClsDB.ClsDBControl Con = new OptDB.ClsDB.ClsDBControl();
SqlConnection conTry=Con.ConDB();
MessageBox.Show(conTry.State.ToString());//显示当前连接对象的状态
}
注意:在调用数据层ConDB方法时,需要引入SqlClient命名空间,其程序代码如下:
using System.Data
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace OptDB
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ClsDB.ClsDBControl Con = new OptDB.ClsDB.ClsDBControl();
SqlConnection conTry=Con.ConDB();
MessageBox.Show(conTry.State.ToString());//显示当前连接对象的状态
}
}
}
★ ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★
namespace OptDB
{
partial class Form1
{
///


/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(129, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 23);
this.button1.TabIndex = 0;
this.button1.Text = "测试连接";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(232, 51);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
★ ★★★★Program.cs主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace OptDB




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.