C#连接SQL Server数据库的实例讲解
作者:人生、蜕变 时间:2024-01-28 04:14:01
C#连接数据库:Connection对象
1.Connection对象概述
Connection对象是一个连接对象,主要功能是建立与物理数据库的连接。其主要包括4种访问数据库的对象类,也可称为数据提供程序,分别介绍如下。
SQL Server数据提供程序,位于System.Data.SqlClient命名空间。
ODBC数据提供程序,位于System.Data.Odbc命名空间。
OLEDB数据提供程序,位于System.Data.OleDb命名空间。
Oracle数据提供程序,位于System.Data.OracleClient命名空间。
说明:根据使用数据库的不同,引入不同的命名空间,然后通过命名空间中的Connection对象连接类连接数据库。例如,连接SQL Server数据库,首先要通过using System.Data.SqlClient命令引用SQL Server数据提供程序,然后才能调用空间下的SqlConnection类连接数据库。
2.连接数据库
以SQL Server数据库为例,如果要连接SQL Server数据库,必须使用System.Data.SqlClient命名空间下的SqlConnection类。所以首先要通过using System.Data.SqlClient命令引用命名空间,连接数据库之后,通过调用SqlConnection对象的Open方法打开数据库。通过SqlConnection对象的State属性判断数据库的连接状态。
界面:
![C#连接SQL Server数据库的实例讲解](https://img.aspxhome.com/file/2023/9/104319_0.png)
代码:
private void btn1_Click(object sender, EventArgs e)
{
if (txt1.Text == "")
{
MessageBox.Show("请输入要连接的数据库名称!");
}
else
{
try
{
string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //**
SqlConnection sqlConnection = new SqlConnection(connString); //**
sqlConnection.Open(); //**
if (sqlConnection.State == ConnectionState.Open)
{
lab2.Text = "数据库【" + txt1.Text.Trim() + "】已经连接并打开!";
}
}
catch
{
MessageBox.Show("数据库连接失败!");
}
}
}
3.关闭连接
当对数据库操作完毕后,要关闭与数据库的连接,释放占用的资源。可以通过调用SqlConnection对象的Close方法或Dispose方法关闭与数据库的连接。这两种方法的主要区别是:Close方法用于关闭一个连接,而Dispose方法不仅关闭一个连接,而且还清理连接所占用的资源。当使用Close方法关闭连接后,可以再调用Open方法打开连接,不会产生任何错误。而如果使用Dispose方法关闭连接,就不可以直接用Open方法打开连接,必须再次重新初始化连接再打开。
界面:
代码:
SqlConnection sqlConnection; //***
/// <summary>
/// 连接数据库
/// </summary>
private void btn1_Click_1(object sender, EventArgs e)
{
if (txt1.Text == "")
{
MessageBox.Show("请输入数据库名称:");
}
else
{
try
{
string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //***
sqlConnection = new SqlConnection(connString); //***
sqlConnection.Open(); //***
if (sqlConnection.State == ConnectionState.Open)
{
btn1.Text = "连接成功";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
txt1.Text = "";
}
}
}
/// <summary>
/// 使用Close方法关闭连接并重新调用Open方法连接数据库
/// </summary>
private void btn2_Click(object sender, EventArgs e)
{
try
{
string str = "";
sqlConnection.Close(); //***
if (sqlConnection.State == ConnectionState.Closed)
{
str = "数据库已经成功关闭\n";
}
sqlConnection.Open(); //***
if (sqlConnection.State == ConnectionState.Open)
{
str += "数据库已经成功打开\n";
}
rtbox1.Text = str;
}
catch (Exception ex)
{
rtbox1.Text = ex.Message;
}
}
/// <summary>
/// 使用Dispose方法关闭连接并重新调用Open方法连接数据库
/// </summary>
private void btn3_Click(object sender, EventArgs e)
{
try
{
sqlConnection.Dispose(); //***
sqlConnection.Open(); //***
}
catch (Exception ex)
{
rtbox1.Text = ex.Message;
}
}
来源:https://www.cnblogs.com/xggbest/p/12244882.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL8.0内存相关参数总结
浅析网页色彩应用
![](https://img.aspxhome.com/file/UploadPic/20101/11/web-color-01-87s.jpg)
element-ui表格合并span-method的实现方法
![](https://img.aspxhome.com/file/2023/7/123107_0s.png)
使用ajax开发的五大误区
WAP页面设计与实现小贴士
![](https://img.aspxhome.com/file/UploadPic/20111/10/01-19s.jpg)
JS实现图片延迟加载并淡入淡出效果的简单方法
Python+Opencv实现图像匹配功能(模板匹配)
![](https://img.aspxhome.com/file/2023/3/98933_0s.jpg)
python实现连连看游戏
使用Python为中秋节绘制一块美味的月饼
![](https://img.aspxhome.com/file/2023/2/96452_0s.jpg)
python boto和boto3操作bucket的示例
CentOS7 LNMP+phpmyadmin环境搭建 第三篇phpmyadmin安装
![](https://img.aspxhome.com/file/2023/0/75060_0s.png)
SQL注入宽字节注入由浅到深学习
![](https://img.aspxhome.com/file/2023/2/55512_0s.jpg)
Go语言在Linux环境下输出彩色字符的方法
![](https://img.aspxhome.com/file/2023/7/109757_0s.png)
Java常用正则表达式验证类完整实例【邮箱、URL、IP、电话、身份证等】
selenium执行js并绕过webdriver监测常见方法
python itchat实现微信好友头像拼接图的示例代码
![](https://img.aspxhome.com/file/2023/2/101482_0s.jpg)
js与jquery获取父级元素,子级元素,兄弟元素的实现方法
python人民币小写转大写辅助工具
js实现百度地图定位于地址逆解析,显示自己当前的地理位置
Python pygame 项目实战事件监听
![](https://img.aspxhome.com/file/2023/8/112908_0s.png)