结合ASP.NET C# SQL做一个登录的网页,文本框两个,按钮一个。

用户名秘码是由SQL提供的比如用户名admin密码123。查找到SQL有存在就成功登录,没有就失败。我是C#初学者,我用VB和ACCESS做成功了,可是用换成C#和SQL做不来。
Dim objcomm As New OleDb.OleDbCommand
str = "select xh,mm from xs where xh='" & TextBox1.Text & "'"
objcomm.CommandText = str
objcomm.Connection = objconn
objda.SelectCommand = objcomm
objconn.Open()
objds.Clear()
objda.Fill(objds)
objconn.Close()

If objds.Tables(0).Rows.Count > 0 Then '若用户存在
If objds.Tables(0).Rows(0).Item(1) = TextBox2.Text Then '若密码正确

MsgBox("欢迎登陆系统!", MsgBoxStyle.OkOnly, "")
manages = 0
users = TextBox1.Text.Trim
Form2.Show()
Me.Hide()
Else
MsgBox("密码错误!", MsgBoxStyle.OkOnly, "")
End If
Else
MsgBox("该用户不存在!", MsgBoxStyle.OkOnly, "")
End If

结合ASP.NET 与C# SQL怎么做?谢谢各位
最新回答
霓裳梦颜

2024-10-12 13:56:57

完整代码:
a1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a1.aspx.cs" Inherits="a1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">

<html xmlns="
http://www.w3.org/1999/xhtml
" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Height="24px" Text="用户名:" Width="110px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Height="21px" Text="密码:" Width="108px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="151px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Height="27px" OnClick="Button1_Click" Text="Button"
Width="96px" /></div>
</form>
</body>
</html>

a1.aspx.cs:
using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;

public partial class a1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "select xh,mm from xs where xh='" + TextBox1.Text + "'";
SqlConnection con = new SqlConnection(@"Server=;Integrated Security=True;" + "Database=");
//SERVER添加服务器名,DataBase添加数据库名,
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();
cmd.CommandText = str;
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr.Read())
{
if (dr["mm"].ToString() == TextBox2.Text)
Response.Write(@"<script>alert('欢迎登录', ' + TextBox1.Text + ')</script>");
else
Response.Write(@"<script>alert('密码错误')</script>");
}

else
{
Response.Write(@"<script>alert('没有记录')</script>");
}
}
}
读取,连接数据不难,不过要注意命名空间的引用。。
鱼沉秋水

2024-10-12 08:11:40

最简单的。

SqlConnection con = new SqlConnection("连接数据库字符串");
SqlDataAdapter data = new SqlDataAdapter("select xh,mm from xs where xh='" + TextBox1.Text + "'", con);
con.Open();
DataSet ds = new DataSet();
data.Fill(ds);
很酷的小当家

2024-10-12 12:08:31

C#+SQL very easy~,因为已经封装好了。

string str = "select xh,mm from xs where xh='" & TextBox1.Text & "'";

sqlconnection conn=new sqlconnection("你的连接字符串");//连接~
sqlcommand cmd=new sqlcommand();//将要执行命令啦
cmd.connection=conn;//告诉他是什么连接
cmd.CommandType=CommandType.Text;//我执行的只是sql语句。
cmd.CommandText=str;//要执行的SQL语句
sqldataReader rder=cmd.ExecuteReader();//执行呗
if(rder.Read())//这里证明有数据,此行执行完毕后指针指在数据行
{
if (rd["mm"].Tostring()==TextBox2.Text)//密码正确
response.write(@"<script>alert('欢迎登录系统,"+textbox1.Text+"')
</script>");
else
response.write(@"<script>alert('密码错误')
</script>");

}esle{
response.write(@"<script>alert('记录数为0,没这个人咯')</script>");
}
微凉°

2024-10-12 09:22:23

邮件给你发代码