1 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 sql插入数据 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //数据库字符串 15 string str = "Data Source=.;Initial Catalog=mysql;Integrated Security=True"; 16 //sql语句 17 string sql = "insert into student values(5,'aaa','sss','ddd')"; 18 //连接数据库 19 SqlConnection con = new SqlConnection(str); 20 con.Open(); 21 //执行sql命令 22 SqlCommand cmd = new SqlCommand(sql,con); 23 //判断是否执行了sql 24 int n = -1; 25 n = cmd.ExecuteNonQuery(); 26 if (n>0) 27 { 28 Console.WriteLine("执行完毕!受影响行数为:"+n); 29 } 30 else 31 { 32 Console.WriteLine("执行失败!"); 33 } 34 Console.ReadKey(); 35 //关闭数据库 36 con.Dispose(); 37 cmd.Dispose(); 38 } 39 } 40 }