All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP连接远程MYSQL和MYSQL5.1中文乱码处理方法_php资料_编程技术
技巧:PHP开发网站程序代码的优化方法_php资料_编程技术
ASP.NET程序错误“HTTP1.0,500,Server,Error”的一个解决办法
初学网页制应知道HTML和XHTML的特点_[Html教程]
深入ASP.NET数据绑定(上)_[Asp.Net教程]
X-Space:日志发布后首页为何不更新-_php资料_编程技术
delphi设置提示信息
PHP判断搜索引擎机器人Robot_[PHP教程]
在PHP中执行系统外部命令_[PHP教程]
php+mysq 修改用户密码(用password加密)_[PHP教程]
Reading number is top 10 pictures
Absolutely shocked. National geographic 50 animal photographys7
白嫩丰满美女照片
支持判处贩卖儿童者死刑
Most cow mistress ZhaoGongXia face exposure
The hot big eye big breast beauty3
The money of more than 100 countries and regions14
The money of more than 100 countries and regions3
No trading, no killing
Sell the barbecue as says father du breul4
Discharge accidentally Actresses by the breast3
Download software ranking
Boxer Classic video2
Boxer's Top ten classic battle1
Unix video tutorial14
WebService在.NET中的实战应用教学视频 → 第4集
Macromedia Dreamweaver 8
1400篇各类破解文章
网络管理员第三版
Unix video tutorial18
Visual C++界面编程技术
Wild things 2
qq published in(发表于) 2014/7/9 1:35:09 Edit(编辑)
ADO.NET打开数据库链接

ADO.NET打开数据库链接

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


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.