在访问SQL Server数据库中的数据时,需要 ADO.NET给我们提供的类库支持。所以我们需要导入2个命名空间:using System.Data;
,using System.Data.SqlClient;
这是SqlConnection对象:SqlConnection connection = new SqlConnection(connStr)
连接对象的数据类型是SqlConnection
,我们需要new
一个SqlConnection
的对象,此类型的构造函数需要一个string类型的参数,这个参数叫做连接字符串。连接字符串简单的说就是告诉程序,要访问的数据库的具体位置。
这是一连接字符串:string connStr = "server= .;database=net;integrated security = true;";
连接字符串是有语法规则的。必须包含3个部分,
第一部分是待访问的数据库服务器的位置,
第二部分是想要访问数据库服务器上的具体的数据库,
第三部分是访问数据库服务器的登录方式。
string connStr = "server= .;database=net;integrated security = true;";
server=localhost;
表示要访问的数据库引擎是在localhost。.
来代替,两者完全等价。使用SQL Server身份验证的方式登录,说白了就是要用SQL Server分配的登录账号和密码。
数据库系统管理员给你分配了一个账号叫dg007,密码是dg123456,那么连接字符串的第3部分应该写作:user id=dg007;password=dg123456。user和id之间有个空格,账号和密码之间用;隔开。
SqlConnection connection = new SqlConnection(connStr)
由于connection对象在跟数据库创建了连接后,会一直保持这种连接,这样会消耗很多服务器性能,所以当我们访问数据库结束后,要主动去销毁这个连接,以释放资源。销毁连接,释放资源的代码是:
connection.Close();
connection.Dispose();
借助using语句,来替我们自动完成这个操作
```csharp
SqlConnection connection = new SqlConnection(connString);
这行代码,我们将修改为
using(SqlConnection connection = new SqlConnection(connString))
using语句有两种用法,其1就是导入命名空间,比如我们最开始所写的using System.Data
其2就是像这样,写在代码中,它能在合适的实际,自动帮我们释放掉被using的()所包住的语句创建出来的对象。
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code01
{
class Program
{
static void Main(string[] args)
{
new Program().CreateTable();
new Program().InsertTable();
new Program().SelectTable();
new Program().DeleteFromTable();
}
/// <summary>
/// 1. 打开数据库,新建数据库
/// </summary>
public void CreateTable()
{
SqlConnection con = null;
try
{
// 1. 创建连接
con = new SqlConnection("data source=.; database=stu; integrated security=SSPI");
// 2. 写入要执行的sql语句
SqlCommand cm = new SqlCommand("create table student_info(id int not null,name varchar(100), email varchar(50), join_date date)", con);
// 3. 打开与数据库的连接
con.Open();
// 4. 执行sql
cm.ExecuteNonQuery();
// 5. 返回提示语句
Console.WriteLine("Table created Successfully");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong." + e);
}
// 6. 关闭连接
finally
{
con.Close();
}
}
/// <summary>
/// 2. 将数据插入表中
/// </summary>
public void InsertTable()
{
SqlConnection con = null;
try
{
con = new SqlConnection("data source=.; database=stu; integrated security=SSPI");
String sql = "insert into student_info(id, name, email, join_date)values('101', 'Hiniu Su', '[email protected]', '2017-11-18')";
SqlCommand cm = new SqlCommand(sql, con);
con.Open();
cm.ExecuteNonQuery();
Console.WriteLine("插入数据记录成功~!");
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong." + e);
}
finally
{
con.Close();
}
}
/// <summary>
/// 3.查询数据
/// </summary>
public void SelectTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=stu; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("SELECT * FROM student_info", con);
// Opening Connection
con.Open();
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
Console.WriteLine("当前 student_info 表中的记录为:");
// Iterating Data
while (sdr.Read())
{
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); // Displaying Record
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally
{
con.Close();
}
}
/// <summary>
/// 4. 删除数据
/// </summary>
public void DeleteFromTable()
{
SqlConnection con = null;
try
{
// Creating Connection
con = new SqlConnection("data source=.; database=stu; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("delete from student_info where id = '101'", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
Console.WriteLine("已经成功地删除了编号为:101 的学生数据信息~!");
// 重新查询数据库中的记录信息
SqlCommand cm2 = new SqlCommand("SELECT * FROM student_info", con);
// Executing the SQL query
SqlDataReader sdr = cm2.ExecuteReader();
Console.WriteLine("当前 student_info 表中的记录为:");
// Iterating Data
while (sdr.Read())
{
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); // Displaying Record
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
// Closing the connection
finally
{
con.Close();
}
}
}
}
1、在github上的仓库建立一个存放图片的库,库的名字随意。如:Images-blog2、将需要在.md文件中显示的图片整理在一个文件夹中,然后push到Images-blog库中3、然后打开github官网,进入仓库的Images-blog库中,打开图片点击红框所示的按钮,copy地址4、在.md文件中填入:组件化 (复用现有的UI结构、样式、行为)规范化 (目录结构的划分、编码规范化、接口规范化、文档规范化、Git分支管理)自动化 (自
安装使用运行命令yarn add reduxredux 原理图主要流程组件 通过 Action Creators 分发(dispatch) action 给 StoreStore 将 之前的状态(previousState)、action,传递给 ReducersReducers 对状态处理后,返回给 Store 新的状态组件通过 getState() 获取状态各部分职责Action Creators: 负责生成 action 对象,并分发给 StoreStore:负
linux 如何查看硬盘大小,内存大小等系统信息及硬件信息 top 可以看到不少信息 fdisk & disk - l & df 查看系统硬盘信息和使用情况 lspci 查看主板信息等 cat /proc/cpuinfo CPU信息 cat /proc/meminfo 内存信息
#include using namespace std;int main(){ int i,j; for(i=1; i<=9; i++) { for(j=1; j<=i; j++) { cout<<j<<"*"<<i<<"="<<j*i<<" "; }
查看更多https://www.yuque.com/docs/share/11c76c45-8b80-48a1-9987-2772cec86e7d
目前的环境是,把jenkins.war 放在MAC 的tomcat webapps里尝试修改tomcat,添加参数:vim /Library/Tomcat//conf/context.xml <Context> ... <Environment name="JENKINS_JAVA_OPTIONS" value="-Dhudson.model.UpdateCenter.pluginDownloadReadTimeoutSeconds=120 -Dhudson
树状结构改变某个单元格颜色,根据层级判断是否展示数据<el-table :data="tableData" style="width: 100%;" :row-key="tableRowKey" border :highlight-current-row="true" :cell-style="tableCellStyle" :tree-props="treeProps" v-on:row-click="rowClick" v-loading="loading" height="100%">
文章目录检验原理西瓜书2.4节提到了二项检验,看不太懂。参考网上其他人的想法后,记录一下自己的理解。以下内容也包含着自己对假设检验的理解,内容会比较冗长。检验原理对于一个学习器的泛化错误率ϵ\epsilonϵ,我们做出一个猜想(假设):ϵ≤ϵ0\epsilon\le\epsilon_0ϵ≤ϵ0。那么如何知道这个猜想对不对呢?假如我们已知ϵ\epsilonϵ的值,那么只需将ϵ\epsil...
数据库系统常问面试题汇总1.数据库索引 索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。如果想按特定职员的姓来查找他或她,则与在表中搜索所有的行相比,索引有助于更快地获取信息。索引的一个主要目的就是加快检索表中数据的方法,亦即能协助信息搜索者尽快的找到符合限制条件的记录ID的辅助数据结构。 优
#搭建WebMail邮件服务器#1.关闭系统默认安装的 sendmail:[[email protected] ~]# service sendmail stop关闭 sendmail: [失败][[email protected] ~]#[[email protected] ~]# chkconfig --level 35 sendmail of
sonic 远程连接真机、设备异地连接、内网穿透