在 C# 上连接到 SQL 数据库
本教程将演示如何使用 SqlConnection
对象连接到 C# 上的 SQL 数据库。
使用 SqlConnection
对象连接到 C#
上的 SQL 数据库
SqlConnection
类是一个对象,表示与传递的连接字符串指定的 SQL Server 数据库的连接。它包含在命名空间 System.Data.SqlClient
中。
SqlConnection connection = new SqlConnection(connectionString);
连接字符串包含有关数据源、如何连接到它以及连接配置详细信息的信息。你可以在连接字符串中包含许多不同的参数,但我们将讨论一些最常用的参数。
将连接字符串传递给 SqlConnection
对象后,你可以使用其方法管理连接。
-
Open()
:打开连接。 -
Close()
:关闭连接。 -
Dispose()
:释放连接使用的资源。 -
ChangeDatabase()
:为打开的SqlConnection
更改当前数据库。
例子:
using System;
using System.Data.SqlClient;
namespace SQLConnection_Sample {
class Program {
static void Main(string[] args) {
// The server's name that holds the database
string DataSource = "MSI\\SQLEXPRESS";
// The name of the database
string InitialCatalog = "SampleDB";
// Sets if the connection should use integrated security.
// If this value is set to "SSPI", the user's Windows Authentication will be used
string IntegratedSecurity = "SSPI";
// Should the database require a specific log in
string UserID = "";
string Password = "";
string connectionString = "Data Source =" + DataSource +
"; Initial Catalog =" + InitialCatalog +
"; Integrated Security=" + IntegratedSecurity
//+ "; User ID=" + UserID
//+ "; Password=" + Password
;
try {
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
Console.WriteLine("The database has been opened!");
Console.WriteLine("Connection State: " + connection.State.ToString());
connection.Close();
Console.WriteLine("The database has been closed!");
connection.Dispose();
Console.WriteLine("The database connection has been disposed!");
Console.WriteLine("Connection State: " + connection.State.ToString());
} catch (Exception ex) {
Console.WriteLine("There's an error connecting to the database!\n" + ex.Message);
}
Console.ReadLine();
}
}
}
在上面的示例中,我们首先通过输入服务器、数据库名称和集成的安全参数来创建连接字符串。将其传递给 SqlConnection
对象后,我们通过打开、关闭和最后释放连接来演示不同的状态。
所有这些都打印在控制台中。
输出:
The database has been opened!
Connection State: Open
The database has been closed!
The database connection has been disposed!
Connection State: Closed
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径