当前位置:网站首页>自用学习笔记-连接式与非连接式访问数据库
自用学习笔记-连接式与非连接式访问数据库
2022-04-23 05:55:00 【番茄大侠本尊】
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;//用于获取配置文件中的字符串
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);
//创建Command对象后,就可以执行SQL命令,执行后完成并关闭数据连接,示例代码如下所示。
// cmd.ExecuteNonQuery(); //执行SQL命令
//con.Close(); //关闭连接
//----------
SqlDataReader reader = cmd.ExecuteReader();//将sql语句发送给sqlconnection,并产生一个sqldatareader对象
//reader是只读的,不能修改数据,reader读取一条就释放一条,只能向前不能后退
while (reader.Read())
{
Console.WriteLine("姓名:{0}",reader[2].ToString());
}
reader.Close();
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
}
版权声明
本文为[番茄大侠本尊]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Tomato2313/article/details/53764511
边栏推荐
猜你喜欢
el-table添加序号
解析psd文件,并映射成组件
Overview of node file system and buffer
查漏补缺(三)
Aperçu de Redux
Makefile foundation, common functions and general makefile
微信小程序之点击取消,返回上页,修改上页的参数值,let pages=getCurrentPages() let prevPage=pages[pages.length - 2] // 上一页的数据
Analysis of fixed point PID code of FOC motor Library
Makefile基础、常用函数及通用Makefile
Assembler 32-bit unsigned addition calculator
随机推荐
FOC 单电阻采样 位置环控制伺服电机
小程序学习笔记(一)
Collection of practical tips for C language (continuously updated)
查漏补缺(三)
若依框架从零开始
Principle and characteristic analysis of triode
Node的数据库编程
QT add qserialport class to realize serial port operation
手动实现简单的Promise及其基础功能
2020 Jiangsu Collegiate Programming Contest-A.Array
Shell脚本的通配符和特殊符号
微信小程序之点击取消,返回上页,修改上页的参数值,let pages=getCurrentPages() let prevPage=pages[pages.length - 2] // 上一页的数据
Generate shortcut
el-table添加序号
Notes on advanced points of C language 2
JS的解析与执行过程
赛氪-zeal
元编程,代理Proxy与反射Reflect
信息学一本通-小球
低代码开发平台整理