C#操作数据库
时间:2021-07-01 10:21:17
帮助过:2人阅读
using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Data.SqlClient;
7
8 namespace 数据库
9 {
10 class Program
11 {
12 static void Main(
string[] args)
13 {
14 int n = -
1;
15 //准备连接字符串
16 string str =
"Data Source=JACK-PC;Initial Catalog=test;Integrated Security=True";
17 //连接数据库
18 using (SqlConnection con =
new SqlConnection(str))
19 {
20 //打开数据库
21 con.Open();
22 //sql语句
23 //string sql = "select * from student";
24 //string sql = "insert into student values(4,‘陈蕊‘,‘chenrui@163.com‘,‘锦州‘)";
25 string sql =
"insert into student values(5,‘陈小蕊‘,‘chenxiaorui@163.com‘,‘锦州‘)";
26 //执行sql语句
27 using (SqlCommand cmd =
new SqlCommand(sql,con))
28 {
29 n =
cmd.ExecuteNonQuery();
30 }
31 if (n>
0)
32 {
33 Console.WriteLine(
"添加成功!");
34 Console.WriteLine(
"行数:"+
n);
35 }
36 else
37 {
38 Console.WriteLine(
"添加失败!");
39 }
40 Console.ReadKey();
41 }
//连接结束
42 }
43 }
44 }
C#操作数据库
标签: