当前位置:Gxlcms > 数据库问题 > ADO.NET— 数据库增删改查方法的编写

ADO.NET— 数据库增删改查方法的编写

时间:2021-07-01 10:21:17 帮助过:39人阅读

using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using System.Data; 8 using System.Data.SqlClient; 9 10 namespace ADO.NETDemo 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 //定义连接字符串 17 string connstring = "Server=F-PC\\SQLEXPRESS;DataBase=StudentManageDB;Uid=sa;Pwd=123456"; 18 //创建连接对象 19 SqlConnection conn = new SqlConnection(connstring); 20 21 //编写SQL语句 22 string sql = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)"; 23 sql +="values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7})"; 24 string sql1 = string.Format(sql, "王二", "", "1992-12-16", 330809199212161235, 25, "13709872345","北京",1); 25 string sql2 = string.Format(sql, "李四", "", "1992-08-16", 330809199208161236, 25, "13709872346","上海",1); 26 string sql3 = string.Format(sql, "宋玲", "", "1993-08-24", 330809199308241236, 24, "13977777777","深圳",1); 27 28 string manysql = sql1 + ";" + sql2 + ";" + sql3; 29 //创建command对象 30 SqlCommand cmd = new SqlCommand(manysql, conn); 31 32 //打开连接 33 conn.Open(); 34 35 //执行操作 36 int result = cmd.ExecuteNonQuery(); 37 //关闭连接 38 conn.Close(); 39 40 if(result==3) 41 Console.WriteLine("添加成功"); 42 else 43 Console.WriteLine("添加失败"); 44 Console.ReadLine(); 45 46 } 47 } 48 }

在insert语句后面添加select @@identity 查询。

执行ExecuteScalar()方法:同时执行insert和select

即插入新纪录后返回该记录的标准列。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 
10 namespace ADO.NETDemo
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             //定义连接字符串
17             string connstring = "Server=F-PC\\SQLEXPRESS;DataBase=StudentManageDB;Uid=sa;Pwd=123456";
18             //创建连接对象
19             SqlConnection conn = new SqlConnection(connstring);
20 
21             //编写SQL语句
22             string sql = "insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,StudentAddress,ClassId)";
23             sql += "values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7});select @@identity";
24             sql = string.Format(sql, "周佳", "", "1994-12-16", 330809199412161236, 23, "13709552395", "云南", 1);
25 
26             //创建command对象
27             SqlCommand cmd = new SqlCommand(sql, conn);
28 
29             //打开连接
30             conn.Open();
31 
32             //执行操作
33             int result=Convert.ToInt32(cmd.ExecuteScalar());
34             //关闭连接
35             conn.Close();
36 
37             if(result>0)
38                 Console.WriteLine(result);
39             else
40                 Console.WriteLine("添加失败");
41             Console.ReadLine();
42 
43         }
44     }
45 }

 

  

ADO.NET— 数据库增删改查方法的编写

标签:scala   tde   birt   span   lex   数据   创建   nec   错误   

人气教程排行