-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.cs
86 lines (79 loc) · 2.58 KB
/
Database.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Database
{
class Program
{
SqlConnection conn = null;
public void OpenDB()
{
string costring = null;
conn = new SqlConnection(costring);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
Console.WriteLine("数据库已连接");
}
public void Insert(string sql)
{
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery(); //执行sql语句
if (result > 0)
Console.WriteLine("添加成功");
else
Console.WriteLine("添加失败");
}
public void Change(string sql)
{
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery(); //执行sql语句
if (result > 0)
Console.WriteLine("修改成功");
else
Console.WriteLine("修改失败");
}
public void Delete(string sql)
{
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
Console.WriteLine("添加成功");
else
Console.WriteLine("添加失败");
}
public void Search(string sql)
{
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader(); //查询
while (read.Read())
{
//示例:int number = Convert.ToInt32(read["列名1"]); //查询列名1的数据,方法为: read(变量名)["列名"]; 该方法返回的是object类型
//string name = read["列名2"].ToString();
//Console.WriteLine("{0}\t{1}\t{2}\t\t{3}\t\t{4}", number, name, revise, Email, day);
}
}
public void CloseDB()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
Console.WriteLine("数据库已断开");
}
static void Main(string[] args)
{
string sql = "insert into student(id,name,age,teacherID) values(5,'小王',25,105)";
Program p = new Program();
p.OpenDB();
p.Insert(sql);
p.CloseDB();
Console.ReadKey();
}
}
}