当前位置:网站首页>C#学习笔记----C#连接MySQL数据库
C#学习笔记----C#连接MySQL数据库
2022-07-27 23:35:00 【秋瞑小雁客】
C#学习笔记----C#连接MySQL数据库
using System.Linq;
using System.Text;
using System.Data;// 引用表的命名空间
using System.Data.SqlClient;
namespace ConnectMySQL
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入学号:");
//第一步,声明一个连接的对象字符串
//连接数据库
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "127.0.0.1"; //地址
scsb.UserID = "bigEric"; // 账号名
scsb.Password = "123456";//登录密码
scsb.InitialCatalog = "table1";//表名
SqlConnection sqlCon = new SqlConnection(scsb.ToString());
//判断是否已有连被打开接
if (sqlCon.State==ConnectionState.Closed)
{
sqlCon.Open();
}
string stu_no = Console.ReadLine();
//要使用的SQL语句
string sqlStr = "SELECT * FROM students WHERE 学号= '" + stu_no + "'";
//创建SQL语句的执行
SqlCommand sqlCommand =new SqlCommand(sqlStr,sqlCon);
SqlDataReader sdr = null;
try
{
sdr = sqlCommand.ExecuteReader();
while (sdr.Read())
{
Console.WriteLine("姓名:" + sdr["姓名"].ToString());
Console.WriteLine("班级:" + sdr["班级"].ToString());
Console.WriteLine("性别:" + sdr["性别"].ToString());
Console.WriteLine("年龄:" + sdr["年龄"].ToString());
Console.WriteLine("----------------");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
//最后记得关闭数据库
finally
{
sqlCon.Close();
}
}
}
}
边栏推荐
- 3000 words and 11 pictures hard core popular science: what is edge computing? What are the connections and differences with cloud computing?
- Storage practices for high-performance computing scenarios, see here
- 梳理 SQL 性能优化,收藏经典!
- Gossip: an initially perfect FS is as impractical as the first version of the program requiring no bugs
- BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
- 从功能测试到自动化测试,月薪突破30K+,我的6年测开经验。
- 面试题 01.08. 零矩阵
- Adding custom dynamic arts and Sciences to cesium
- Icml2022 | online decision transformer
- 8000字讲透OBSA原理与应用实践
猜你喜欢

在一个字符串里面统计给定字符串的个数

Three instance

“蔚来杯“2022牛客暑期多校训练营3 补题题解(A、C、J)

数仓搭建——DWS层

Gossip: an initially perfect FS is as impractical as the first version of the program requiring no bugs

Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"

内容bypass分享

Oxygen temperature and humidity module

LeetCode 2351. 第一个出现两次的字母

现货白银如何计算盈亏
随机推荐
20 bad habits of bad programmers
Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium
Thoroughly understand kubernetes scheduling framework and plug-ins
Day 013 一维数组练习
Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"
面试题 01.06. 字符串压缩
Data problems can also be found if there is a space at the end of the field value of MySQL query criteria
HarmonyOS 3正式发布:鸿蒙手机流畅安全,鸿蒙终端常用常新
Rviz uses arbotix to control robot motion
The cooperation between starfish OS and metabell is just the beginning
登录功能实现
彻底搞懂kubernetes调度框架与插件
Neuron 2.1.0 release: it supports sparkplug B specification and more complete industrial protocol support
How to make digital retail undertake the development task of the era of traffic and retention may be the key
C language main function transfer parameters
Software process that testers need to know
Spreadsheet export excel table
Three basic teaching
S-RPN: Sampling-balanced region proposal network for small crop pest detection
《安富莱嵌入式周报》第275期:2022.07.18--2022.07.24