下面以一次数据库插入为例
1.定义连接字符串:Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=admin,如果是连接非本机的数据库,在Server之后填写IP地址,Server服务器地址,DataBase数据库名,Uid用户名,Pwd密码
2.创建连接对象 SqlConnection conn = new SqlConnection(connString);
3.打开连接conn.Open();
4.编写sql
5.创建Command对象,并封装Connection和Sql语句SqlCommand cmd = new SqlCommand(sql, conn);
创建Command对象的方法很多这里我们使用个人觉得比较简便的方法
6.执行操作ExecuteNonQuery方法int result = cmd.ExecuteNonQuery();
result的值为受影响的行数
7.关闭连接conn.Close();
namespace ADO.NETDemo
{
class Program
{
static void Main(string[] args)
{
//定义连接字符串
string connString = "Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=admin";
//创建连接对象
SqlConnection conn = new SqlConnection(connString);
//打开连接
conn.Open();
if(ConnectionState.Open == conn.State)
{
Console.WriteLine("Connection is Opened");
}
//编写Sql
string sql = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)";
sql += "values('{0}','{1}','{2}',{3},{4},'{5}','{6}',{7})";
sql = string.Format(sql, "呵呵", "男", "1990-09-12", 120225199609121213, 25, "15029635631", "天津", 1);
//创建Command对象
SqlCommand cmd = new SqlCommand(sql, conn);
//执行操作
int result = cmd.ExecuteNonQuery();
conn.Close();
if(ConnectionState.Closed == conn.State)
{
Console.WriteLine("Connection is Closed");
}
if(result == 1)
{
Console.WriteLine("添加成功");
}
else
{
Console.WriteLine("添加失败");
}
Console.ReadKey();
}
}
}
二、提交多条Sql语句 将多条sql语句中间通过分号连接“;”,例子如下
namespace ADO.NETDemo
{
class Program
{
static void Main(string[] args)
{
//定义连接字符串
string connString = "Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=admin";
//创建连接对象
SqlConnection conn = new SqlConnection(connString);
//打开连接
conn.Open();
if(ConnectionState.Open == conn.State)
{
Console.WriteLine("Connection is Opened");
}
//编写Sql
string sql = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)";
sql += "values('{0}','{1}','{2}',{3},{4},'{5}','{6}',{7})";
string sql1 = string.Format(sql, "呵呵1", "男", "1990-09-12", 120225199609101213, 25, "15029635631", "天津", 1);
string sql2 = string.Format(sql, "呵呵2", "男", "1990-09-11", 120225199609111215, 25, "15029635651", "天津", 1);
string sql3 = string.Format(sql, "呵呵3", "男", "1990-09-14", 120225199609121214, 25, "15029635631", "天津", 1);
string Sql = sql1 + ";" + sql2 + ";" + sql3;
//创建Command对象
SqlCommand cmd = new SqlCommand(Sql, conn);
//执行操作
int result = cmd.ExecuteNonQuery();
conn.Close();
if(ConnectionState.Closed == conn.State)
{
Console.WriteLine("Connection is Closed");
}
if(result == 1)
{
Console.WriteLine("添加成功");
}
else
{
Console.WriteLine("添加失败");
}
Console.ReadKey();
}
}
}
这样通过分号连接后,我们通过断点调试可以看到当前的sql
在某些情景下我们可能需要拿到,新添加的记录的id,这时候我们可以使用@@identity
获取。
1.首先在sql之后添加@@identity
2.使用ExecuteScalar()方法,这个方法可以同时执行insert和select object result = cmd.ExecuteScalar();
ExecuteScalar方法的返回值是Object型
3.转换类型取得标识列的值Convert.ToInt32(result)
代码如下:
namespace ADO.NETDemo
{
class Program
{
static void Main(string[] args)
{
//定义连接字符串
string connString = "Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=admin";
//创建连接对象
SqlConnection conn = new SqlConnection(connString);
//打开连接
conn.Open();
if(ConnectionState.Open == conn.State)
{
Console.WriteLine("Connection is Opened");
}
//编写Sql
string sql = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)";
sql += "values('{0}','{1}','{2}',{3},{4},'{5}','{6}',{7});Select @@identity";
string sql1 = string.Format(sql, "呵呵00", "男", "1990-09-12", 120225099109101210, 25, "15029635631", "天津", 1);
//创建Command对象
SqlCommand cmd = new SqlCommand(sql1, conn);
//执行操作
object result = cmd.ExecuteScalar();
conn.Close();
if(ConnectionState.Closed == conn.State)
{
Console.WriteLine("Connection is Closed");
}
Console.WriteLine($"新添加的学员编号为{Convert.ToInt32(result)}");
Console.ReadKey();
}
}
}
结果