using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { //添加数据 //1、获取用户输入信息 Console.Write("请输入学生编号:"); string scode = Console.ReadLine(); Console.Write("请输入学生姓名:"); string sname = Console.ReadLine(); Console.Write("请输入学生性别:"); bool ssex =Convert.ToBoolean( Console.ReadLine()); Console.Write("请输入学生生日:"); DateTime sbirthday =Convert.ToDateTime( Console.ReadLine()); Console.Write("请输入学生分数:"); decimal sscore = Convert.ToDecimal( Console.ReadLine()); //2、开启数据库连接,添加用户输入信息 SqlConnection conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into student values(‘" + scode + "‘,‘" + sname + "‘,‘" + ssex + "‘,‘" + sbirthday + "‘," + sscore + ")"; //cmd.CommandText = "insert into Student values(‘" + scode + "‘,‘" + sname + "‘,‘" + ssex + "‘,‘" + sbirthday + "‘," + sscore + ")"; //Console.WriteLine(cmd.CommandText); try { conn.Open(); cmd.ExecuteNonQuery(); Console.WriteLine("添加成功!"); } catch { Console.WriteLine("连接数据库失败!请重新连接输入!"); } conn.Close(); Console.ReadKey(); } } }
时间: 2024-10-06 12:43:28