最近开始学C#,从VB.NET转过来学VC#.NET有很多不熟悉的地方!
今天我就遇到一个很大的问题:我在做一个账目管理系统,有用户登录和管理员登陆2种模式,当要登陆时,须向数据库(使用的是MS SQL Server)的表查询输入的ID和密码是否正确,如ID正确密码不正确,MessageBox.Show("管理员帐号错误!","错误",MessageBoxButtons.OK, MessageBoxIcon.Error);,如ID不正确密码正确,MessageBox.Show("管理员密码错误!","错误",MessageBoxButtons.OK, MessageBoxIcon.Error);,也是说查询查的是表中的行和列,这个代码怎么写?
求强人赐教!
今天我就遇到一个很大的问题:我在做一个账目管理系统,有用户登录和管理员登陆2种模式,当要登陆时,须向数据库(使用的是MS SQL Server)的表查询输入的ID和密码是否正确,如ID正确密码不正确,MessageBox.Show("管理员帐号错误!","错误",MessageBoxButtons.OK, MessageBoxIcon.Error);,如ID不正确密码正确,MessageBox.Show("管理员密码错误!","错误",MessageBoxButtons.OK, MessageBoxIcon.Error);,也是说查询查的是表中的行和列,这个代码怎么写?
求强人赐教!
if(myDS.Tables[0].Rows.Count==0)
{
用户不存在
return;
}
else
{
if(txtPwd.Text != myDS.Tables[0].Rows["pwd"].ToString())
{
密码不正确
return
}
成功;
.......
}
{
Response.Write("<script>alert('ok');</script>");
}
else
{
Response.Write("<script>alert('wrong');</script>");
}
如果要输入msgbox这种可以尝试
Response.Write("<script language=vb>msgbox('ok');</script>");
是不是有问题?
写好了sql语句,下面的操作你可以用SqlDataReader和sqlcommand来做啊SqlConnection mySqlConnection = new SqlConnection("连接字符串");
SqlCommand mySqlCommand = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", mySqlConnection); mySqlConnection.Open();
myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection); if(myDataReader.Read())
{
查询有结果,也就是你要的登陆成功
}
else
{
登陆失败
}
private System.Data.SqlClient.SqlDataAdapter myDa; string strCon=" Server=czf; database=msscl; uid=sa; pwd=providence; ";//czf
SqlConnection chkConn=new SqlConnection(strCon);
chkConn.Open();
string strSelect ="select count(*) from au_info where au_id='"+txtUser.Text+"'";
SqlCommand chkCmd=new SqlCommand(strSelect,chkConn);
int chk=(int)chkCmd.ExecuteScalar();
chkConn.Close();
if (chk==0)
{
MessageBox.Show("ID错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtUser.Text="";
txtPass.Text="";
txtUser.Focus();
return;
}
else
{
this.myDs=new DataSet();
string strCon1=" Server=***; database=***; uid=sa; pwd=***; ";//czf
string strSelect1 ="select id,pswd from au_info where id='"+txtUser.Text+"' and pswd='"+txtPass.Text+"'";
SqlConnection chkConn1=new SqlConnection(strCon1);
chkConn1.Open();
this.myDa=new SqlDataAdapter(strSelect1,chkConn1);
this.myDa.Fill(this.myDs,"au_info");
DataTable myTable=this.myDs.Tables["au_info"]; if(this.myDs.Tables["au_info"].Rows[0][0]==txtUser.Text && this.myDs.Tables ["au_info"].Rows[0][1]==txtPass.Text)
{
登陆代码!
}
else
{
MessageBox.Show("密码错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}自己去整理优化啊