ado_基本连接操作【四】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ado_基本连接操作
{
class Program
{
static void Main(string[] args)
{
//连接数据库的步骤
//1. 创建连接字符串
string constr = @"server=IT-WANGJ\WANGJIN;user=sa;pwd=sa;database=Demo";
//2. 创建连接对象,需要传入需要打开的数据库
using (SqlConnection con = new SqlConnection(constr))
{

//4.编写sql语句
string sql = "insert into Employee(EmployeeName,EmployeeId,Age,PostId,Country) values('wangjin','test',22,123,'test')";
//5. 创建一个执行sql语句的(命令对象)sqlcommand,需要传入需要执行的sql语句和连接对象
using (SqlCommand cmd = new SqlCommand(sql,con))
{
//3. 打开连接(如果打开没有问题,表示连接成功) 因为要做到最晚打开,最早关闭,所以把打开连接放在后面
con.Open();
Console.Write("连接成功");
//Console.ReadKey();
int r=cmd.ExecuteNonQuery();
Console.Write("您插入了{0}条数据", r);
Console.ReadKey();
//cmd.ExecuteNonQuery(); //当执行insert,delect,update语句时使用,执行成功以后返回受影响的行数,执行查询SQL语句的时候返回-1
//cmd.ExecuteScalar(); //当执行返回单个值得时候使用,一般用于聚合函数中使用
//cmd.ExecuteReader(); //当执执行返回多行多列的时候使用
}

}
}
}
}

posted @ 2019-01-12 14:43  锦大大的博客呀!  阅读(228)  评论(0编辑  收藏  举报