数据库表中读取信息

来源:互联网 发布:仿快手源码 编辑:程序博客网 时间:2024/05/18 01:33

//存入

string con = "Server=.;Database=UsersInfo;integrated security=SSPI";

SqlConnection mycon = new SqlConnection(con);
            mycon.Open();
            using (SqlCommand cmd = mycon.CreateCommand())
            {
                cmd.CommandText = "select * from loginInfo where Account='" + account + "'";
                SqlDataReader reader = cmd.ExecuteReader();
                if (!reader.Read())
                {
                    reader.Close();
                    cmd.CommandText = "insert into loginInfo(Account,Password) values('" + account + "','" + passwordonce + "')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("注册成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("帐号已存在");
                }
            }
            con.Clone();


//读取

string con;
            con = "Server=.;Database=UsersInfo;integrated security=SSPI";
            SqlConnection mycon = new SqlConnection(con);
            mycon.Open();
            using (SqlCommand cmd = mycon.CreateCommand())
            {
                cmd.CommandText = "select * from loginInfo where Account='" + account + "'";
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        if (reader.Read())
                        {
                            string Password = reader.GetString(reader.GetOrdinal("Password"));


                            if (password == Password)
                            {
                                MessageBox.Show("登陆成功");
                                mycon.Close();
                                writeInPas();
                                //储存当前帐号
                                StreamWriter sw = new StreamWriter(Application.StartupPath + "\\tmpAccount.txt", false);
                                sw.WriteLine(txtAccountLgn.Text);
                                sw.Close();
                                this.DialogResult = DialogResult.OK;
                            }
                            else
                            {
                                MessageBox.Show("用户名或密码错误");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误");
                    }
                }
            }



0 0