当前位置:网站首页>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();
}
}
}
}
边栏推荐
- 软件测试面试题:性能测试指标有哪些?
- Develop plug-ins for the recording function of flutter
- Codeforces暑期训练周报(7.21~7.27)
- Unknown database ‘xxxxx‘
- ICML2022 | 在线决策Transformer
- Cesium add dynamic pop-up
- Realize ABCD letter increment
- Codeforces暑期训练周报(7.14~7.20)
- Nokia announces cooperation with Broadcom to develop 5g chip
- In April, global smartphone shipments fell 41% year-on-year, and Huawei surpassed Samsung to become the world's first for the first time
猜你喜欢

String

Storage practices for high-performance computing scenarios, see here

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

Transplant QT system for i.mx6ull development board - cross compile QT code

Three instance
![[C language] file operation](/img/6e/b8f3466ca0a5f7424afcab561124af.png)
[C language] file operation

idea常用的快捷键汇总

Count the number of given strings in a string

BAT大厂测试架构师如何解读测试平台的各种争议

Fluent call interface UI
随机推荐
Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"
MySQL进阶--存储过程以及自定义函数
BYD semiconductor completed the a+ round financing of 800million yuan: 30 well-known investment institutions entered the market, with a valuation of 10.2 billion yuan!
【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)
2022/07/27 学习笔记 (day17) 代码块和内部类
Qt 绘制系统简介
The understanding of domain adaptation in transfer learning and the introduction of three technologies
Centralized management of clusters
Lua快速上手
二维数组相关知识
Wu xiongang sent an internal letter: arm's allegations are unwarranted, and no damage is allowed to the existing achievements!
Introduction and configuration of vsftpd
How to calculate the profit and loss of spot Silver
Meguiar sued liandian for secret theft and sentenced: liandian was fined NT $100million and three employees were sentenced!
Icml2022 | online decision transformer
登录功能实现
Jingfang Technology: ASML, a lithography machine manufacturer, is one of the main customers of Anterion company, which participated in the merger and acquisition of the company
彻底搞懂kubernetes调度框架与插件
Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
Codeforces summer training weekly (7.21~7.27)