SQL Server链接服务器查询可以通过ADO.NET来实现,是一个示例代码:using System;
SQL Server链接服务器查询可以通过ADO.NET来实现,是一个示例代码:
using System;
using System.Data;
using System.Data.Sql;
namespace SqlServerConnection
{
class Program
{
static void Main(string[] args)
{
// 创建一个连接字符串
string connectionString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True";
// 使用using关键字来创建一个数据库连接对象
using (SqlConnection connection = new SqlConnection(connectionString))
{
// 创建一个SqlCommand对象
SqlCommand command = new SqlCommand("SELECT * FROM Customer", connection);
// 打开连接
connection.Open();
// 使用ExecuteReader方法来执行查询操作
SqlDataReader reader = command.ExecuteReader();
// 读取查询结果
while (reader.Read())
{
Console.WriteLine("ID: {0}, Name: {1}", reader["ID"], reader["Name"]);
}
// 关闭连接
connection.Close();
}
}
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(42条)