📅  最后修改于: 2023-12-03 15:41:32.598000             🧑  作者: Mango
表 C# 是一个基于 .NET 平台的数据库访问组件,它是 ADO.NET 的一部分,提供了一种简单的方式来访问数据库。
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=(local);Initial Catalog=DemoDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("数据库连接已打开");
connection.Close();
}
}
}
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=(local);Initial Catalog=DemoDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM Employee";
using (SqlCommand command = new SqlCommand(sql, connection))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
}
}
connection.Close();
}
}
}
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=(local);Initial Catalog=DemoDB;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "INSERT INTO Employee (Name, Age) VALUES (@name, @age)";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", "Tom");
command.Parameters.AddWithValue("@age", 30);
int result = command.ExecuteNonQuery();
Console.WriteLine("{0} 行数据受影响", result);
}
connection.Close();
}
}
}
表 C# 是一个高效简便的数据库访问组件,它可以轻松访问多种类型的数据库,提供了强类型数据类和 LINQ 查询语言的支持,适合快速开发小规模的应用程序。