当前位置:网站首页>Self use learning notes - connected and non connected access to database
Self use learning notes - connected and non connected access to database
2022-04-23 17:10:00 【Tomato Warrior】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//sql
using System.Data;//ConnectionState
using System.Configuration;// Used to get the string in the configuration file
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//----------
string ConnStr = "server=(local);database=SystemCourse;integrated security=true";
// string ConnStr = System.Configuration.ConfigurationManager
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnStr;
if(conn.State ==ConnectionState.Closed)
conn.Open();
//----------
string sql = "select * from students";
SqlCommand cmd = new SqlCommand(sql,conn);
// establish Command After the object , You can execute SQL command , After execution, complete and close the data connection , The sample code is as follows .
// cmd.ExecuteNonQuery(); // perform SQL command
//con.Close(); // Close the connection
//----------
SqlDataReader reader = cmd.ExecuteReader();// take sql The statement is sent to sqlconnection, And produce a sqldatareader object
//reader Is read-only , Can't modify data ,reader Read one and release one , You can only move forward, not backward
while (reader.Read())
{
Console.WriteLine(" full name :{0}",reader[2].ToString());
}
reader.Close();
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
}
版权声明
本文为[Tomato Warrior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553458195.html
边栏推荐
猜你喜欢
ASP. NET CORE3. 1. Solution to login failure after identity registers users
. net cross platform principle (Part I)
Shell script -- shell programming specification and variables
Detailed explanation of Niuke - Gloves
Detailed explanation of the penetration of network security in the shooting range
Grpc gateway based on Ocelot
ASP. Net core dependency injection service life cycle
Perception of linear algebra 2
Lock lock
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
随机推荐
Paging the list collection
Talk about browser cache control
Feign report 400 processing
Interface document yaml
Get the column name list of the table quickly in Oracle
C# Task. Delay and thread The difference between sleep
. net cross platform principle (Part I)
Calculation formula related to tolerance analysis
Clickhouse - data type
VsCode-Go
Quick install mongodb
EF core in ASP Generate core priority database based on net entity model
Bytevcharts visual chart library, I have everything you want
Tencent resolves the address according to the IP address
Detailed explanation of C webpai route
ASP. Net core reads the configuration file in the class library project
PostgreSQL列存与行存
Shell-awk命令的使用
Log4j output log information to file
RTKLIB 2.4.3源码笔记