当前位置:网站首页>C#的学习笔记【八】SQL【一】
C#的学习笔记【八】SQL【一】
2022-04-23 11:15:00 【BoomBiuBiu】
此处需要下载SQL,我下载的是2019版本,安装链接放在下面:
在安装的过程中会遇到一些问题,可以看下面这个链接,基本上能解决问题:
【实例 1】创建与本机 SQL Server 数据库的连接,并使用异常处理。
根据题目要求,连接 SQL Server 数据库时使用的用户名为 sa、密码为 123456789,【此处的账号密码是可以变成你自己设置的】连接的数据库为Test。【注意看自己数据库上的名称】,此处登录遇到问题,可以看以下的链接:
需要在SQL上新建一个数据库名称为Test的数据库,才能显示链接成功
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//编写数据库连接串
string connStr = "Data source=.;Initial Catalog=Test;User ID=sa;Password=123456789";
//创建SqlConnection的实例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打开数据库连接
conn.Open();
MessageBox.Show("数据库连接成功!");
}
catch (Exception ex)
{
MessageBox.Show("数据库连接失败!" + ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
}
}
}
版权声明
本文为[BoomBiuBiu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/BoomBiuBiu/article/details/124355008
边栏推荐
- MySQL索引优化之分页探索详细介绍
- Jupyter Lab 十大高生产力插件
- MySQL面试题讲解之如何设置Hash索引
- STM32接电机驱动,杜邦线供电,然后反烧问题
- 语雀文档编辑器将开源:始于但不止于Markdown
- 26. 删除有序数组中的重复项
- GO接口使用
- Detailed explanation of integer data type tinyint in MySQL
- Learn go language 0x05: the exercise code of map in go language journey
- Detailed explanation of how to smoothly go online after MySQL table splitting
猜你喜欢
Typora operation skill description (I) md
More reliable model art than deep learning
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
Get things technology network optimization - CDN resource request Optimization Practice
年度最尴尬的社死瞬间,是Siri给的
Use of SVN:
Promise details
升级cpolar内网穿透能获得的功能
得物技术网络优化-CDN资源请求优化实践
Visual common drawing (III) area map
随机推荐
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
软件测试人员,如何优秀的提Bug?
Upgrade the functions available for cpolar intranet penetration
Solve the problem of "suncertpathbuilderexception: unable to find valid certification path to requested target"
Visual common drawing (IV) histogram
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
Visual common drawing (V) scatter diagram
redis优化系列(二)Redis主从原理、主从常用配置
STM32接电机驱动,杜邦线供电,然后反烧问题
Visual common drawing (I) stacking diagram
JDBC – PreparedStatement – 如何设置 Null 值?
FileProvider 路径配置策略的理解
Implementation of partition table of existing data table by MySQL
MySQL分区表实现按月份归类
Three web components (servlet, filter, listener)
C语言之结构体(进阶篇)
Excel·VBA数组冒泡排序函数
我的创作纪念日
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Typora operation skill description (I) md