Пример #1
0
        private void denglubutton_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Convert.ToString(yonghuming_textbox.Text == null));
            string        source = "server=(local) \\SQLEXPRESS;database=users;integrated security=SSPI";
            SqlConnection con    = new SqlConnection(source);

            con.Open();//打开数据库
            if (con.State == ConnectionState.Open)
            {
                MessageBox.Show("数据库连接成功!");
            }
            string         select = "select * from yonghu";
            SqlDataAdapter sda    = new SqlDataAdapter(select, con);
            DataSet        ds     = new DataSet();

            sda.Fill(ds, "yonghu");
            int flag = 0;//设置一个标记,判断用户是否登录成功

            foreach (DataRow x in ds.Tables["yonghu"].Rows)
            {
                if (yonghuming_textbox.Text == x["id"].ToString().Trim() && yonghumina_textbox.Text == x["mima"].ToString().Trim())
                {
                    yonghuid   = yonghuming_textbox.Text;
                    yonghumima = yonghumina_textbox.Text;
                    yonghuname = x["name"].ToString().Trim();
                    flag       = 1;
                    //MessageBox.Show(x["name"].ToString());
                    //如果yonghu数据库中的name不为空,则进入用户界面
                    if (x["name"].ToString().Trim() != String.Empty)//此处用String.Empty而不用null是因为数据库中为赋值的行为“”而不是null
                    {
                        //if()
                        yuhujiemian f3 = new yuhujiemian();
                        f3.ShowDialog();
                    }
                    //否则进入绑定界面
                    else
                    {
                        yonghubangding yhbd = new yonghubangding();
                        yhbd.ShowDialog();
                    }
                }
            }
            if (flag == 0)//如果没有登录成功,则显示
            {
                MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            string select = "select cname from customers";

            con = new SqlConnection(source);
            con.Open();
            if (con.State == ConnectionState.Open)
            {
                MessageBox.Show("数据库连接成功!");
            }
            sda = new SqlDataAdapter(select, con);
            sda.Fill(ds, "name");
            //MessageBox.Show("lalala!");
            int flag = 0;//判断customers中是否有输入的姓名

            foreach (DataRow x in ds.Tables["name"].Rows)
            {
                if (textBox1.Text == x["cname"].ToString().Trim())
                {
                    flag = 1;//有的话赋值为1
                    MessageBox.Show("有这个姓名!");
                    con2 = new SqlConnection(source2);
                    string select2 = "select * from yonghu";
                    sda = new SqlDataAdapter(select2, con2);
                    sda.Fill(ds2, "yonghu");
                    foreach (DataRow y in ds2.Tables["yonghu"].Rows)
                    {
                        if (y["id"].ToString().Trim() == yonghuyanzhengjiemian.yonghuid)
                        {
                            y["name"] = textBox1.Text;
                            SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                            sda.Update(ds2, "yonghu");
                            yonghuyanzhengjiemian.yonghuname = textBox1.Name.Trim();//保存用户名字,方便以后的查询用户个人信息等操作
                            MessageBox.Show("会员绑定成功!");
                            //yonghuyanzhengjiemian.yonghuname=
                            yuhujiemian yh = new yuhujiemian();
                            yh.ShowDialog();
                        }
                    }
                }
            }
            if (flag == 0)
            {
                MessageBox.Show("你还不是会员!请联系管理员");
            }
        }