using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace 登陆
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string passworld = Console.ReadLine();
//连接字符串
string sqlstr = @"Data Source=BE541554DB7E431;Integrated Security=True";
{
//创建连接对象
using (SqlConnection con = new SqlConnection(sqlstr))
{
//打开数据库
con.Open();
//创建Command命令
using (SqlCommand cmd = new SqlCommand())
{
//先查出表中用户名对应信息
cmd.CommandText = "select *from Ado登陆 where UserName=" + "username";
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())//如果有用户输入的数据就表示存在
{
string dbpassworld = reader.GetString(reader.GetOrdinal("passworld"));
if (passworld == dbpassworld)
{
Console.WriteLine("登陆成功");
}
else
{
Console.WriteLine("登陆失败");
}
}
else
{
Console.WriteLine("用户名或密码错误!");
}
}
}
}
}
}
}
}有问题?没反应,数据是连上了的
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace 登陆
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string passworld = Console.ReadLine();
//连接字符串
string sqlstr = @"Data Source=BE541554DB7E431;Integrated Security=True";
{
//创建连接对象
using (SqlConnection con = new SqlConnection(sqlstr))
{
//打开数据库
con.Open();
//创建Command命令
using (SqlCommand cmd = new SqlCommand())
{
//先查出表中用户名对应信息
cmd.CommandText = "select *from Ado登陆 where UserName=" + "username";
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())//如果有用户输入的数据就表示存在
{
string dbpassworld = reader.GetString(reader.GetOrdinal("passworld"));
if (passworld == dbpassworld)
{
Console.WriteLine("登陆成功");
}
else
{
Console.WriteLine("登陆失败");
}
}
else
{
Console.WriteLine("用户名或密码错误!");
}
}
}
}
}
}
}
}有问题?没反应,数据是连上了的
=>
cmd.CommandText = "select * from Ado登陆 where UserName='" + username + "'";
=>
cmd.CommandText = "select * from Ado登陆 where UserName='" + username + "'";
这个是错误的,你看一下
cmd.CommandText="select * from ado-登录 where UserName='"+ username+ "'";