增删改 语句:
SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123"); conn.Open(); //SqlCommand cmd = new SqlCommand(); //构造命令对象 //cmd.Connection = conn; SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into info values('p005','田泽','0','n004','1990-6-1')"; cmd.ExecuteNonQuery(); conn.Close(); Console.WriteLine("OK");
查询 语句
SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");//连接类 conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from info"; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Console.WriteLine(dr[0].ToString()+dr[1].ToString()+dr[2].ToString()+dr[3].ToString()+dr[4].ToString()); } conn.Close();
用户密码登录
Console.Write("账号:"); string uid = Console.ReadLine(); Console.Write("密码:"); string pwd = Console.ReadLine(); SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from login where username='"+uid+"'and password='"+pwd+"'"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows==false) { Console.WriteLine("输入的用户密码不正确 请重新输入"); } else { Console.WriteLine ("登录成功"); } conn.Close();