using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program s = new Program();
string userName = "Admin";
string pwd = "0";
string strMsg = "有此信息";
bool b = s.CheckUserInfo(userName, pwd, ref strMsg);
if (b)
{
Console.WriteLine(strMsg);
}
else
{
Console.WriteLine(strMsg);
}
Console.ReadLine();
}
private const string strConn = @"Data Source=.;Initial Catalog=MySchool;User ID=bdqn;Password=11";
public bool CheckUserInfo(string userName,string pwd,ref string strMsg) {
SqlConnection conn = new SqlConnection(strConn);
try
{
string strSql = "SELECT COUNT(*)FROM [MySchool].[dbo].[Admin] WHERE LoginId='"+userName+"' and LoginPwd='"+pwd+"'";
conn.Open();
Console.WriteLine(strSql);
SqlCommand comm = new SqlCommand(strSql, conn);
int i = (int)comm.ExecuteScalar();
if (i != 1)
{
strMsg = "输入无效";
return false;
}
else {
return true;
}
}
catch (Exception ex)
{
strMsg = "发生异常";
Console.WriteLine(ex.Message);
return false;
}
finally {
conn.Close();
Console.WriteLine("关闭数据库");
}
}
}
}
14章 示题3 代码
最新推荐文章于 2017-10-30 21:31:40 发布