ADO.NET添加数据
2021-06-30 12:06
标签:write pen 添加 ade lin 命名空间 nic close command //这里记得添加引用命名空间 ADO.NET添加数据 标签:write pen 添加 ade lin 命名空间 nic close command 原文地址:http://www.cnblogs.com/wcl2017/p/7136398.htmlstring ucode = "";
//开始 生成编号
SqlConnection conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=lyb0629");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Users order by Ucode desc";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
ucode = dr["Ucode"].ToString();
int a = Convert.ToInt32(ucode.Substring(1));
a++;
ucode = "U" + a.ToString("000");
conn.Close();
//结束 生成编号
Console.WriteLine("您的编号是:"+ucode);
Console.Write("请输入用户名:");
string username = Console.ReadLine();
Console.Write("请输入密码:");
string password = Console.ReadLine();
Console.Write("请输入昵称:");
string nickname = Console.ReadLine();
Console.Write("请输入性别:");
string sex = Console.ReadLine();
Console.Write("请输入生日:");
string birthday = Console.ReadLine();
Console.Write("请输入民族:");
string nation = Console.ReadLine();
cmd.CommandText = "insert into Users value(‘@b‘,‘@c‘,‘@d‘,‘@e‘,‘@f‘,‘@g‘)";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue(@b, username);
cmd.Parameters.AddWithValue(@c, password);
cmd.Parameters.AddWithValue(@d, nickname);
cmd.Parameters.AddWithValue(@e, sex);
cmd.Parameters.AddWithValue(@f, birthday);
cmd.Parameters.AddWithValue(@g, nation);
conn.Open();
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count >= 1)
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
Console.ReadKey();