当前位置:网站首页>自用学习笔记-连接式与非连接式访问数据库
自用学习笔记-连接式与非连接式访问数据库
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
边栏推荐
- 微信小程序之点击取消,返回上页,修改上页的参数值,let pages=getCurrentPages() let prevPage=pages[pages.length - 2] // 上一页的数据
- FOC SVPWM函数PWMC_SetPhaseVoltage解析
- 关于软件的空间占用,安装目录
- Navicat 连接 oracle library is not loaded的解决方法
- 邮箱字符串判断
- 信息学一本通-小球
- Analysis of fixed point PID code of FOC motor Library
- FOC电机库 定点PID代码分析
- 获取当前一周的时间范围
- FOC SVPWM function pwmc_ Setphasevoltage parsing
猜你喜欢
随机推荐
如何使用input表单向服务发送(占用较小)图片文件(body传输)?涉及到FileReader内置对象
死区时间的分析与设置
欢迎使用Markdown编辑器
Mysql中的索引与视图
服务器常见错误代码 总结
Node模版引擎(ejs, art-template)
Use of shell scripts & and 𞓜
Arm common assembly instructions
Router对象、Route对象、声明式导航、编程式导航
监听除某元素之外点击事件
记第一次使用阿里字体图标库
js面试题:fn.call.call.call.call(fn2) 解析
Set up a personal blog of jpress
HDU-Memory Control
PN结、二极管原理详解与应用
元编程,代理Proxy与反射Reflect
查漏补缺(一)
HDU-Tunnel Warfare
C language structure specifying initialization
元素计算距离与event事件对象