菜鸟求助!!!C#登录问题,请各位大大们帮忙解答,感激不尽,具体如下
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;//首先在资源管理器的"引用"里引用System.configuration文件
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sqlnn = "Server=localhost;Integrated Security = True; database=学生成绩管理系统";
SqlConnection conn = new SqlConnection();
string sqlstr = "select count(*) from 用户信息表 where(username='" + textBox1.Text + "'and password='" + textBox2.Text + "')";
conn.ConnectionString = sqlnn;
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
int flag = System.Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空");
}
else
{
if (flag > 0)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;//首先在资源管理器的"引用"里引用System.configuration文件
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sqlnn = "Server=localhost;Integrated Security = True; database=学生成绩管理系统";
SqlConnection conn = new SqlConnection();
string sqlstr = "select count(*) from 用户信息表 where(username='" + textBox1.Text + "'and password='" + textBox2.Text + "')";
conn.ConnectionString = sqlnn;
SqlCommand cmd = new SqlCommand(sqlstr, conn);
conn.Open();
int flag = System.Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空");
}
else
{
if (flag > 0)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
}
}
} 展开
展开全部
晕。。你没有把连接语句关联到sqlconnection连接类里面,怎么能连接的上呢
把SqlConnection conn = new SqlConnection();
改成SqlConnection conn = new SqlConnection(sqlnn);就好了。。
把SqlConnection conn = new SqlConnection();
改成SqlConnection conn = new SqlConnection(sqlnn);就好了。。
追问
改了之后还是那样啊,求解释
追答
string sqlnn = "Server=localhost;Integrated Security = True; database=学生成绩管理系统";
SqlConnection conn = new SqlConnection(sqlnn); //此处关联
conn.Open(); //连接的打开放在这里
string sqlstr = "select count(*) from 用户信息表 where(username='" + textBox1.Text + "'and password='" + textBox2.Text + "')";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
int flag = System.Convert.ToInt32(cmd.ExecuteScalar());
if (textBox1.Text == "")
{
MessageBox.Show("用户名不能为空");
}
else if (textBox2.Text == "")
{
MessageBox.Show("密码不能为空");
}
else
{
if (flag > 0)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
}
}
conn.Close();//连接的关闭是放在最后的,你再试试,如果还没成功,就是你sql sever的问题啦、、
还有一个问题,楼下说的,在and前面敲几个空格~~~
展开全部
好久没做,都看懵了,我这有个以前做的登录界面,你有时间自己看看~
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
{
txtUserName.Text = "";
txtPassword.Text = "";
}
protected void ibtnLogin_Click(object sender, ImageClickEventArgs e)
{
//判断数据库中是否存在与输入用户名一致的记录
//如果数据库中存在相应的用户名
//判断密码是否与输入密码一致
//如果一致,则进入管理页面
try
{
DataClassesDataContext db = new DataClassesDataContext();
tbFamily fa = db.tbFamily.Single(p => p.UserName == txtUserName.Text);
if (fa.UserPassword == txtPassword.Text)
{
//加载登录信息
Session["UserName"] = txtUserName.Text;
//保存登录日志
Response.Redirect("jtcygl.aspx");
}
else
{
Response.Write("<script>alert('您输入的用户名或密码不正确');</script>");
}
}
catch
{
Response.Write("<script>alert('您输入的用户名或密码不正确');</script>");
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
{
txtUserName.Text = "";
txtPassword.Text = "";
}
protected void ibtnLogin_Click(object sender, ImageClickEventArgs e)
{
//判断数据库中是否存在与输入用户名一致的记录
//如果数据库中存在相应的用户名
//判断密码是否与输入密码一致
//如果一致,则进入管理页面
try
{
DataClassesDataContext db = new DataClassesDataContext();
tbFamily fa = db.tbFamily.Single(p => p.UserName == txtUserName.Text);
if (fa.UserPassword == txtPassword.Text)
{
//加载登录信息
Session["UserName"] = txtUserName.Text;
//保存登录日志
Response.Redirect("jtcygl.aspx");
}
else
{
Response.Write("<script>alert('您输入的用户名或密码不正确');</script>");
}
}
catch
{
Response.Write("<script>alert('您输入的用户名或密码不正确');</script>");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string sqlstr = "select count(*) from 用户信息表 where(username='" + textBox1.Text + "'and password='" + textBox2.Text + "')";语句里面where、and关键字前后要有空格!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据库连接字符串有问题。。
最好用sa登录方式连接。
最好用sa登录方式连接。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询