当前位置:网站首页>C # learning notes ----- C # connect to MySQL database
C # learning notes ----- C # connect to MySQL database
2022-07-28 01:45:00 【Autumn close little goose guest】
C# Learning notes ----C# Connect MySQL database
using System.Linq;
using System.Text;
using System.Data;// Namespace of reference table
using System.Data.SqlClient;
namespace ConnectMySQL
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Enter the student id :");
// First step , Declare a connected object string
// Connect to database
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "127.0.0.1"; // Address
scsb.UserID = "bigEric"; // Account name
scsb.Password = "123456";// The login password
scsb.InitialCatalog = "table1";// Table name
SqlConnection sqlCon = new SqlConnection(scsb.ToString());
// Judge whether the connection has been opened
if (sqlCon.State==ConnectionState.Closed)
{
sqlCon.Open();
}
string stu_no = Console.ReadLine();
// To be used SQL sentence
string sqlStr = "SELECT * FROM students WHERE Student number = '" + stu_no + "'";
// establish SQL Execution of statements
SqlCommand sqlCommand =new SqlCommand(sqlStr,sqlCon);
SqlDataReader sdr = null;
try
{
sdr = sqlCommand.ExecuteReader();
while (sdr.Read())
{
Console.WriteLine(" full name :" + sdr[" full name "].ToString());
Console.WriteLine(" class :" + sdr[" class "].ToString());
Console.WriteLine(" Gender :" + sdr[" Gender "].ToString());
Console.WriteLine(" Age :" + sdr[" Age "].ToString());
Console.WriteLine("----------------");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
// Finally, remember to close the database
finally
{
sqlCon.Close();
}
}
}
}
边栏推荐
- Thoroughly understand kubernetes scheduling framework and plug-ins
- Summary: Prometheus storage
- C#学习笔记----C#连接MySQL数据库
- JUC concurrent programming learning
- Realize OCR language recognition demo (II) - display and interaction of pictures and recognition content
- 递归相关习题
- Introduction to QT drawing system
- 我的富二代朋友
- docker 本地搭建mysql主从
- 还在用WIFI你就OUT了:LI-FI更牛!!!
猜你喜欢
随机推荐
Advanced MySQL -- stored procedures and custom functions
String
【样式集合1】tab 栏
Token is used in nodejs
Transplant QT system for i.mx6ull development board - cross compile QT code
VLAN experiment
软件测试面试题:如何准备测试数据?如何防止数据污染?
Storage practices for high-performance computing scenarios, see here
彻底搞懂kubernetes调度框架与插件
梳理 SQL 性能优化,收藏经典!
Opengauss active / standby architecture works with keeplive
还在用WIFI你就OUT了:LI-FI更牛!!!
数仓搭建——DWS层
LeetCode 2347. 最好的扑克手牌
QT setting Icon
Icml2022 | online decision transformer
HCIP第十五天
Interview question 01.07. rotation matrix
Redis 5 种基本数据结构
自定义事件








