- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.SqlClient;
- namespace Demp13
- {
- class Program
- {
- static void Main(string[] args)
- {
- //1. 创建连接字符串
- string str = "server=.;database=MySchool;uid=sa;pwd = 密码";
- // string str = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=accp";
- //2. 创建 connection 对象
- SqlConnection conn = new SqlConnection(str);
- try
- {
- //3. 打开链接
- conn.Open();
- Console.WriteLine("打开链接成功");
- //4. 准备 sql 语句, 创建 command 对象, 并执行 sql 语句
- Console.WriteLine("请输入用户名");
- string name = Console.ReadLine();
- Console.WriteLine("请输入密码");
- string pwd = Console.ReadLine();
- string sql = "select COUNT(*) from admin where loginId='"+name+"'and loginPwd='"+pwd+"'";
- SqlCommand cmd = new SqlCommand(sql,conn);
- //5. 执行 sql 语句, 得到返回结果
- int r= Convert.ToInt32(cmd.ExecuteScalar());
- if (r> 0)
- {
- Console.WriteLine("登陆成功");
- }
- else {
- Console.WriteLine("登陆失败");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally {
- //4. 关闭链接
- conn.Close();
- Console.WriteLine("关闭链接成功");
- }
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2874162.html