c#和sqlserver数据库:第一课 连接

一、操作步骤

(1)下载Microsoft.Data.SqlClient包

1、右击“依赖项”

2、点击“管理NuGet资源包”

3、搜索SqlClient,选择第一个点击下载

(2)连接步骤(代码)

1、构建连接字符串:指定DataSource、UserID、InitialCatalog、Password、Encrypt字段。

2、通过连接字符串创建连接

3、打开连接

4、使用

using Microsoft.Data.SqlClient;
using System.Data;
using System.Data.Common;
//构建连接字符串
var connectionString = new SqlConnectionStringBuilder();
connectionString.DataSource = "127.0.0.1";
connectionString.UserID = "sa";
connectionString.InitialCatalog = "ScoreDB";
connectionString.Password = "1024";
connectionString.Encrypt = false;
//创建连接
IDbConnection connection = new SqlConnection(connectionString.ToString());
//打开连接
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "INSERT INTO Students(Name,No) VALUES ('zs','123')";
var row = command.ExecuteNonQuery();
Console.WriteLine("Hello, World!");

二、工具

navicat premium 15(数据库客户端)

vs 

sqlserver

三、核心对象

1、connectionString

2、IDbConnection

3、IDbCommand

四、基本原理

        Microsoft.Data.SqlClient包中实现了微软提供的连接接口。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值