C#配置数据库、配置数据源、DataReader方式连接数据库
一、 创建好一个新的Web项目(我用的VS2019,其他版本类似)

二、 右键项目–>添加–>添加ASP.NET文件夹–>App_Data

三、 右键App_Data–>添加–>新建项

四、 选择数据–>SQL Server 数据库–>给数据库起名–>添加

五、 添加好后,项目变化如下

六、 右键表–>添加新表

七、 随便创建一个表(配置连接字符串用,有表的库就不用创了,我是新创建的项目和数据库所以需要一张表,这里只是为了演示连接字符串的配置方法)

八、 更新数据库

九、 右键项目,随便创一个Web页面


十、 创建好后点击拆分视图

十一、 左侧点击工具箱–>数据–>SqlDataSource

十二、 把光标定位在div中间

十三、 双击SqlDataSource,将SqlDataSource添加进来

十四、 Ctrl+S保存

十五、 点击拆分视图的SqlDataSource右上角的小三角,选择配置数据源

十六、 选择刚刚创建的数据库,下一步


十七、 自定义一个好记的连接名(一会获取连接字符串的时候要用)

十八、 一直下一步直到完成


十九、 配好之后如下图,然后Ctrl+S保存


二十、 删除数据源,Ctrl+S保存


二十一、 找到Web.config文件打开

二十二、 找到配置的连接字符串,复制下来

二十三、 打开Web窗体对应的cs文件

二十四、 引用命名空间,读取Web.config获取连接字符串

二十五、 使用DataReader的方式读取数据

