C#做一登陆界面,分管理员和普通用户。普通用户只能有一部分功能,我需要从登陆判断,然后确实隐藏一部分功能。但在窗口传递值问题上,有点为难。。
public Form1()
{
InitializeComponent();
}
public int x; ************
public int X { get {return x;} 此为我为form1,增加的公共属性。
set { x = value; }
}********* private void error(string name) { SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656"); conn.Open();
string sql = "update T_user set errortimes=errortimes+1 where [User]='" + name + "' ";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader dataReader = command.ExecuteReader();
} private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656"); conn.Open(); if (radioButton1.Checked == true) { string na = textBox1.Text;
string pa = textBox2.Text; string ab = "select * from T_student where [name]='" + na + "'";
SqlCommand ss = new SqlCommand(ab, conn);
SqlDataReader cc = ss.ExecuteReader(); if (cc.Read())
{ string password = cc["pass"].ToString(); if (password == pa)
{
MessageBox.Show("学生用户登录成功");
this.X = 1; *********************此为给公共属性赋值,然后传
Form2 tt = new Form2(); 递到form2中判断隐藏
tt.ShowDialog();
}
else { MessageBox.Show("密码错误"); } }
else { MessageBox.Show("用户不存在");
}
}
//管理员验证 else if (radioButton2.Checked == true)
{ string name = textBox1.Text;
string aa = textBox2.Text; string sql = "select * from T_user where [User]='" + name + "'";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.Read())
{ string password = dataReader["pass"].ToString();
int bb = int.Parse(dataReader["errortimes"].ToString());
if (bb <= 3)
{
if (password == aa)
{
MessageBox.Show("登录成功");
Form2 m = new Form2();
m.ShowDialog();
this.X = 2;
} else
{ MessageBox.Show("密码错误");
error(name);
}
}
else { MessageBox.Show("登录次数过多"); }
}
else
{
MessageBox.Show("用户不存在");
error(name); }
}
}
private void button2_Click(object sender, EventArgs e)
{
Form3 a = new Form3();
a.ShowDialog();
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、form2
private void Form2_Load(object sender, EventArgs e)
{
Form1 tt = new Form1();
if (tt.X == 2)
{ this.用户管理ToolStripMenuItem.HideDropDown();
this.用户管理ToolStripMenuItem.Enabled = false;
*****部分为主要问题所在。求指导
public Form1()
{
InitializeComponent();
}
public int x; ************
public int X { get {return x;} 此为我为form1,增加的公共属性。
set { x = value; }
}********* private void error(string name) { SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656"); conn.Open();
string sql = "update T_user set errortimes=errortimes+1 where [User]='" + name + "' ";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader dataReader = command.ExecuteReader();
} private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656"); conn.Open(); if (radioButton1.Checked == true) { string na = textBox1.Text;
string pa = textBox2.Text; string ab = "select * from T_student where [name]='" + na + "'";
SqlCommand ss = new SqlCommand(ab, conn);
SqlDataReader cc = ss.ExecuteReader(); if (cc.Read())
{ string password = cc["pass"].ToString(); if (password == pa)
{
MessageBox.Show("学生用户登录成功");
this.X = 1; *********************此为给公共属性赋值,然后传
Form2 tt = new Form2(); 递到form2中判断隐藏
tt.ShowDialog();
}
else { MessageBox.Show("密码错误"); } }
else { MessageBox.Show("用户不存在");
}
}
//管理员验证 else if (radioButton2.Checked == true)
{ string name = textBox1.Text;
string aa = textBox2.Text; string sql = "select * from T_user where [User]='" + name + "'";
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.Read())
{ string password = dataReader["pass"].ToString();
int bb = int.Parse(dataReader["errortimes"].ToString());
if (bb <= 3)
{
if (password == aa)
{
MessageBox.Show("登录成功");
Form2 m = new Form2();
m.ShowDialog();
this.X = 2;
} else
{ MessageBox.Show("密码错误");
error(name);
}
}
else { MessageBox.Show("登录次数过多"); }
}
else
{
MessageBox.Show("用户不存在");
error(name); }
}
}
private void button2_Click(object sender, EventArgs e)
{
Form3 a = new Form3();
a.ShowDialog();
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、form2
private void Form2_Load(object sender, EventArgs e)
{
Form1 tt = new Form1();
if (tt.X == 2)
{ this.用户管理ToolStripMenuItem.HideDropDown();
this.用户管理ToolStripMenuItem.Enabled = false;
*****部分为主要问题所在。求指导
解决方案 »
- 现有个层次分析法(AHP)的程序,需要用C# winform进行设计,有兴趣者欢迎探讨。。。
- 关于3D命名空间
- QQ的这种功能怎么做啊?
- 象浏览器上"刷新,后退"等带图片按纽,在c#中怎么实现
- C# 网页浏览器
- 如何在VS2008里用ADO.NET连接SQL2005并编辑表?
- 大家帮我看看,我的IE是怎么了??
- 求助:IndexOf方法的用法
- 明明有了runat="server",却提示必须包含在runat="server"中。
- 如何定时弹出一个窗口
- 类A继承了类B,调用类A的时候我希望类A能执行一下它本身的构造函数(2个类里面的方法都是静态的)
- WebBrowser中html使用window.open方法open相同的窗口名总是新建tab页
去设置用户的使用和查看权限的。。建立公共类库吧
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;namespace database
{
public class DataBase
{ /// <summary>
/// 保护变量,数据库连接。
/// </summary>
protected SqlConnection Connection; /// <summary>
/// 保护变量,数据库连接串。
/// </summary>
protected string ConnectionString; /// <summary>
/// 构造函数。
/// </summary>
/// <param name="DataBaseConnectionString">数据库连接串</param>
public DataBase()
{
//这里的连接字符串是从APP。config中获取,可以再APP。config的<configuration>
下加入 <appSettings><add key="ConnectString" value="server=服务器IP或者名称,端口号;user id=数据库用户名;pwd=密码;database=数据库名"/></appSettings>这里填写你的数据库相关信息,当然这只是混合登陆模式,系统登陆模式的字符串自己可以去网上查查这里就不细说了
ConnectionString = ConfigurationManager.AppSettings["ConnectString"];
} /// <summary>
/// 析构函数,关闭数据库
/// </summary>
~DataBase()
{
try
{
if (Connection != null)
Connection.Close();
}
catch { }
} /// <summary>
/// 保护方法,打开数据库连接。
/// </summary>
protected void Open()
{
if (Connection == null)
{
Connection = new SqlConnection(ConnectionString);
}
if (Connection.State.Equals(ConnectionState.Closed))
{
Connection.Open();
}
} /// <summary>
/// 公有方法,关闭数据库连接。
/// </summary>
public void Close()
{
if (Connection != null)
Connection.Close();
} /// <summary>
/// 公有方法,获取数据,返回一个DataSet。
/// </summary>
/// <param name="SqlString">Sql语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SqlString)
{
Open();
SqlDataAdapter adapter = new SqlDataAdapter(SqlString, Connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
Close();
return dataset;
}
}
}值的传递共有方法 建立一个类文件
public class Oper
{
public static string userid; public static string getUserid
{
get { return userid; }
} public static string setUserid
{
set { userid = value; }
}
}
类似这样就可以了 调用很简单 例如 Oper.setUserid=textbox.text.toString();别的地方用也是一样
ds = db.getDataset("select * from UserInfo where UserID = '"+Oper.getUserid+"'");db是共有类Database创建的对象,还有一些更复杂的使用 例如结合XML文件或者结合APP.CONFIG都可以
例如
public class AppConfig
{
private XmlDocument Doc = new XmlDocument();
private AppDomain Ad = AppDomain.CurrentDomain;
public AppConfig()
{
//可以指定加载的XML文件
Doc.Load(Ad.SetupInformation.ConfigurationFile);
}
public void SetAppValue(string key, string newValue)
{
//然后找XML文件中的configuration下的appSettings标签下的值
Doc.SelectSingleNode("/configuration/appSettings/add[@key='" + key + "']").Attributes["value"].Value = newValue;
}
}
等等 仅作参考
Form2 tt = new Form2(); 递到form2中判断隐藏
tt.X=2;然后在form2里
if (this.X == 2)
{ this.用户管理ToolStripMenuItem.HideDropDown();
this.用户管理ToolStripMenuItem.Enabled = false;
......
一般这样做在子窗口可以传给父窗口
不如你定义一个静态变量
public static int X;
然后把值传给他。。调用的时候 类名.X就好了
窗体传值汇总
比如:
public class Common
{
//创建一个静态的字段用于存放管理员的姓名
public static string Uname;
}这个代码就是用Uname作为判断权限的字段,你根据这个字段去数据库查询他的权限就可以了
C# 窗体间传值方法大汇总(转)
http://topic.csdn.net/u/20120710/14/C88A8F3C-C096-4F93-A0EE-B82B810EBD9D.html