我在数据库里建了用户表,有username,password,powers(权限)
然后a1是代表有查询的查看表的功能,就是登入的代码处了问题,我是一只菜的不能菜的菜鸟,是在写不下去了,求求各位大神指点下,目前还不能将连接字符串写进类里,数据层烂了点.
            DataTable dt=new DataTable ();
            string str = "Data Source=LAUGHING;Initial Catalog=Andeng;Integrated Security=True";
            SqlConnection conn=new SqlConnection (str);
            string str1= "select * from yonghu where username='" + textBox1.Text.Replace("'", "''") + "'and password='" + textBox2.Text.Replace("'", "''") + "'";
            SqlCommand cmd=new SqlCommand (str1,conn);
            conn.Open();
            Main main = new Main();
             for(int i = 0; i < dt.Rows.Count; i++)
             {
                  if (dt.Rows[i][3].ToString() == "a1")
                  {
                     Main.Buttion1.Enable=true;
                   }
或者有更好代码帮我下,我不会讲代码放在类里然后调用,我只会写作文一样的写,求各位大神,大仙,大师傅教教我

解决方案 »

  1.   

    public class DBHelper
        {
            //属性:数据库链接对象
            private static SqlConnection conn;
            public static SqlConnection Conn
            {            get
                {
                    try
                    {                    string connstr = "server=.;database=Driver;User ID=sa;Password=123456";
                        if (conn == null)
                            conn = new SqlConnection(connstr);
                        if (conn.State == ConnectionState.Closed)
                            conn.Open();
                        if (conn.State == ConnectionState.Broken)
                        {
                            conn.Close();
                            conn.Open();
                        }
                        return conn;                }
                    catch (Exception ex)
                    {                    throw;
                    }
                }
            }
            //方法:查询,DataReader
            public static SqlDataReader GetReader(string SqlStr)
            {
                SqlCommand cmd = new SqlCommand(SqlStr, Conn);
                return cmd.ExecuteReader();
            }        public static SqlDataReader GetReader(string SqlStr, SqlParameter[] paras)
            {
                SqlCommand cmd = new SqlCommand(SqlStr, Conn);
                cmd.Parameters.AddRange(paras);
                return cmd.ExecuteReader();
            }
            //查询:DataTable
            public static DataTable GetTable(string SqlStr)
            {
                try
                {      
                SqlDataAdapter dap = new SqlDataAdapter(SqlStr, Conn);
                DataSet ds = new DataSet();
                dap.Fill(ds);
                conn.Close();
                return ds.Tables[0];
                }
                catch (Exception ex)
                {                throw;
                }
            }        //增删改
            public static bool Execute(string SqlStr)
            {
                SqlCommand cmd = new SqlCommand(SqlStr, Conn);
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                return result > 0;
            }        //返回首行首列
            public static object GetScalar(string SqlStr)
            {
                SqlCommand cmd = new SqlCommand(SqlStr, Conn);
                object obj = cmd.ExecuteScalar();
                conn.Close();
                return obj;
            }string str1= "select * from yonghu where username='" + textBox1.Text.Replace("'", "''") + "'and password='" + textBox2.Text.Replace("'", "''") + "'";
      Main main = new Main();
      string al=DBHelper.GetScalar(str1).ToString();//注意自己判断下为空的情况
      if (al== "a1")
      {
      Main.Buttion1.Enable=true;
      }
      

  2.   

    上面sql忘记改了string str1= "select powers from yonghu where username='" + textBox1.Text.Replace("'", "''") + "'and password='" + textBox2.Text.Replace("'", "''") + "'";
      

  3.   


    Main窗口的Button属性改成Public
      

  4.   

    我觉得比较好的方法是Main窗体里定义一个属性public string powers { get; set; }然后在Main窗体的Load事件里写上。private void Main_Load(object sender, EventArgs e)
    {
       if(powers=xxx)
       {
           Buttion1.Enable=false;
       }
    }登录的时候:
      Main main = new Main();
      for(int i = 0; i < dt.Rows.Count; i++)
      {
         main.power=dt.Rows[i][3].ToString();
      }
      
      main .Show();