Ado. NET Delete and modify

Source: Internet
Author: User

Class

SqlConnection Conn;
SqlCommand cmd;

Public Usersdata ()
{
conn = new SqlConnection ("server=.; database=data0504;user=sa;pwd=123; ");
CMD = conn. CreateCommand ();
}

Public list<users> SelectAll ()
{
list<users> ulist = new list<users> ();
Cmd.commandtext = "Select *from Users";
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
while (Dr. Read ())
{
Users u = new users ();
U.ids = dr["Ids"]. ToString ();
U.ucode = dr["Ucode"]. ToString ();
U.username = dr["UserName"]. ToString ();
U.password = dr["PassWord"]. ToString ();
U.nickname = dr["nickname"]. ToString ();
U.sex = Convert.toboolean (dr["Sex"]);
U.birthday = Convert.todatetime (dr["Birthday"]);
U.nation = dr["Nation"]. ToString ();

Ulist. ADD (U);
}
Conn. Close ();
return ulist;
}

public bool SelectAll (string UC)
{
BOOL has = false;

Cmd.commandtext = "Select *from Users where Ucode = @a";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@a", UC);
Conn. Open ();
SqlDataReader dr = cmd. ExecuteReader ();
if (Dr. HasRows)
{
has = true;
}
Conn. Close ();
return has;
}

public bool UpdateUser (Users u)
{
bool OK = false;

Cmd. CommandText = "Update Users set PassWord = @a, [email protected],[email protected], [email protected] where Ucode = @d";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@a", U.password);
Cmd. Parameters.addwithvalue ("@b", u.nickname);
Cmd. Parameters.addwithvalue ("@c", u.sex);
Cmd. Parameters.addwithvalue ("@d", U.ucode);
Cmd. Parameters.addwithvalue ("@e", u.birthday);
Conn. Open ();

int count = cmd. ExecuteNonQuery ();
if (Count > 0)
{
OK = true;
}

Conn. Close ();
return OK;
}

Public list<users> Insert1 () {
list<users> list = new list<users> ();
Users a= new users ();
Conn. Open ();
Cmd.commandtext = "INSERT into Users values (@a,@b,@c,@d,@e,@f,@g)";
Cmd. Parameters.clear ();
Cmd. Parameters.addwithvalue ("@a", A.ucode);
Cmd. Parameters.addwithvalue ("@b", a.username);
Cmd. Parameters.addwithvalue ("@c", A.password);
Cmd. Parameters.addwithvalue ("@d", a.nickname);
Cmd. Parameters.addwithvalue ("@e", a.sex);
Cmd. Parameters.addwithvalue ("@f", a.birthday);
Cmd. Parameters.addwithvalue ("@g", a.nation);
List. Add (a);

int count = cmd. ExecuteNonQuery ();
Conn. Close ();

if (Count > 0)
{
Console.WriteLine ("Add Success");

}
Else
{
Console.WriteLine ("Failure! ");
}


return list;

}

Console.backgroundcolor = Consolecolor.white;
Console.foregroundcolor = Consolecolor.black;
while (true)
{
Console.clear ();
New Test1 (). Datalook ();

while (true)
{
Console.WriteLine ();
Console.WriteLine ();
Console.Write ("Please enter the action you want to perform (add: 1, modify: 2, Delete: 3):");
string u = Console.ReadLine ();

if (U = = "1")
{
Users UUU = new users ();
UuU. Ucode = new Usersdata (). Newucode ();
Console.WriteLine ("Numbered:" + UUU. Ucode);
while (true)
{
Console.Write ("Please enter user name:");
UuU. UserName = Console.ReadLine ();
if (UUU. Username.trim (). Length > 0) {break;}
else {Console.WriteLine ("User name does not meet the rules!") "); }
}

while (true)
{
Console.Write ("Please enter password:");
UuU. PassWord = Console.ReadLine ();
Console.Write ("Please enter the new password again:");
String PassWord2 = Console.ReadLine ();

if (UUU. Password.trim (). Length > 4 && UUU. PassWord = = PassWord2)
{

Break
}
Else
{Console.WriteLine ("Password does not meet the rules!") ");
}
}

while (true)
{
Console.Write ("Please enter a nickname:");
UuU. Nickname = Console.ReadLine ();
if (UUU. Nickname.trim (). Length > 0) {break;}
else {Console.WriteLine ("Nickname does not meet the rules!") "); }
}

while (true)
{
Console.Write ("Please enter gender (male/female):");
String ssex = Console.ReadLine ();
if (ssex! = "Male" && ssex! = "female")
{
Console.WriteLine ("Gender input is wrong!") ");
}
Else
{
UuU. Sex = Ssex = = "Male"? True:false;
Break
}
}

while (true)
{
Console.Write ("Please enter your Birthday (2000-1-1):");
string bir = Console.ReadLine ();
Try
{
UuU. Birthday = Convert.todatetime (BIR);
Break
}
Catch
{
Console.WriteLine ("Date input Error! ");
}
}

while (true)
{
Console.Write ("Please enter your nationality (e.g. Han/Tibetan):");
string nat = Console.ReadLine ();
String snn = new Usernationdata (). Selectnationcode (NAT);
if (SNN = = "NULL")
{
Console.WriteLine ("Look no this nation!") ");
}
Else
{
UuU. Nation = SNN;
Break
}
}

Usersdata Udd = new Usersdata ();
bool OK = Udd. Insertuser (UUU);
if (OK)
{
Console.WriteLine ("added successfully! ");
}
Else
{
Console.WriteLine ("Add failed! ");
}

Console.readkey ();
Break
}

Modify

else if (U = = "2")
{
Console.Write ("Please enter the user number to be modified:");
String III = Console.ReadLine ();
BOOL h = new Usersdata (). SelectAll (iii);

if (h)
{
Users up = new users ();
Up. Ucode = III;

#region Change Password
Console.WriteLine ("This user information has been queried, can be modified");
while (true)
{
Console.Write ("Please enter a new password:");
String pwd1 = Console.ReadLine ();
Console.Write ("Please enter the new password again:");
String pwd2 = Console.ReadLine ();
if (pwd1 = = pwd2)
{
Up. PassWord = PWD1;
Break
}
Else
{
Console.WriteLine ("Two times password inconsistent!") Please re-enter! ");
}
}
#endregion

#region Modifying nicknames
while (true)
{
Console.Write ("Please enter a new nickname:");
String nn = Console.ReadLine ();
if (NN. Trim (). Length <= 0)
{
Console.WriteLine ("Input nickname does not meet the rules!") Please re-enter! ");
}
Else
{
Up. nickname = nn;
Break
}
}
#endregion


while (true)
{
Console.Write ("Please enter a new gender (male/female):");
string ss = Console.ReadLine ();
if (ss! = "Male" && ss! = "female")
{
Console.WriteLine ("Input Error! ");
}
Else
{
Up. Sex = SS = = "Male"? True:false;
Break
}
}
while (true) {
Try
{
Console.Write ("Please enter a new birthday:");
datetime birthday = new DateTime ();
Birthday = Convert.todatetime (Console.ReadLine ());
Up. Birthday = Birthday;
Break
}
catch {

Console.Write ("Please enter the correct date format:");
}

}
while (true)
{
Console.Write ("Please enter your nationality (e.g. Han/Tibetan):");
string nat = Console.ReadLine ();
String snn = new Usernationdata (). Selectnationcode (NAT);
if (SNN = = "NULL")
{
Console.WriteLine ("Look no this nation!") ");
}
Else
{
Up. Nation = SNN;
Break
}
}

BOOL B = new Usersdata (). UpdateUser (UP);
if (b)
{
Console.WriteLine ("Modified successfully! ");
}
Else
{
Console.WriteLine ("Modification failed!! ");
}

}
Else
{
Console.WriteLine ("Without this data");
}

Console.readkey ();
Break
}

Ado. NET Delete and modify

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.