using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//SQL SEVER .NET 数据程序提供程序命名空间
namespace ConsoleApplication1
{
class Program
{
private const string strconn = @"Data Source=.;Initial Catalog=MySchool;
Integrated Security=True";
static void Main(string[] args)
{
Program p = new Program();
p.delu();
//string name = "admin"; //为声明项目可以不用特意声明他
//string pwd = "0"; //为声明项目可以不用特意声明他
//Console.WriteLine("请输入账号:");
//string a = Console.ReadLine();
//Console.WriteLine("请输入密码:");
//string C = Console.ReadLine();
//string Msg = " ";
//bool b = p.chech(a, C, ref Msg);
//if (b)
//{
// p.show();
// Console.WriteLine(Msg);
//}
//else
//{
// Console.WriteLine("你登陆失败!!!");
//}
//
}
public bool chech(string name,string pwd,ref string Msg)
{
SqlConnection conn = new SqlConnection(strconn);//局部变量
try
{
// SqlConnection conn = new SqlConnection(strconn);成员变量
conn.Open();
string str = @"SELECT COUNT(*) FROM [MySchool].[dbo].[Admin]
where LoginId='" + name + "'and LoginPwd='" + pwd + "'";
Console.WriteLine(str);
SqlCommand comm = new SqlCommand(str, conn);
int o = (int)comm.ExecuteScalar(); //强制转换为 int 类型
if (o > 0)
{
Msg = "登陆成功";//引用传参 可以不写
return true;
}
Console.WriteLine(o);
}
catch (Exception ex)
{
Console.WriteLine("出现异常" + ex.Message);
}
finally
{
conn.Close();
}
return false;
}
public bool delu() 登陆账户和密码
{
Console.WriteLine("请输入账号:");
string a = Console.ReadLine();
Console.WriteLine("请输入密码:");
string C = Console.ReadLine();
string Msg = " ";
bool b = chech(a, C, ref Msg);
if (b)
{
show();
Console.WriteLine(Msg);
return true;
}
else
{
Console.WriteLine("你登陆失败!!!");
Console.WriteLine("请重新输入");
return false;
}
}
public void show() /创建一个菜单//
{
do
{
Console.WriteLine("=======请选择操作键======");
Console.WriteLine("1. 统计学生人数");
Console.WriteLine("2. 查看学生名单");
Console.WriteLine("3. 按学号查询学生姓名");
Console.WriteLine("4. 按姓名查询学生信息");
Console.WriteLine("5. 修改学生出生日期");
Console.WriteLine("6. 删除学生记录");
Console.WriteLine("0. 退出");
Console.WriteLine("=========================");
Console.WriteLine("请选择:");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 1:
show1();
continue;
case 2:
continue;
case 3:
continue; ;
case 4:
continue;
case 5:
continue;
case 6:
continue;
case 0:
break;
default:
Console.WriteLine("输入错误");
continue;
}
break;
} while (true);
}
public void show1() /查询学生人数
{
SqlConnection conn = new SqlConnection(strconn);
try
{
string str = "select count(*) from Student";
conn.Open();
SqlCommand comm = new SqlCommand(str, conn);
int i = (int)comm.ExecuteScalar();
Console.WriteLine("学生总人数为:" + i);
}
catch (Exception)
{
Console.WriteLine("出现异常");
}
finally
{
conn.Close();
}
}
}
}
T-SQL 第十四章上机连接数据库
最新推荐文章于 2016-12-06 04:48:48 发布