首页 > 代码库 > ADO.NET增添
ADO.NET增添
using System.Data.SqlClient;
//让用户输入添加的信息
Console.Write("请输入编号:");
string ucode = Console.ReadLine();
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();
//将添加的信息存入数据库并提示
string sql = "server=.;database=Data0504;user=sa;pwd=123;";
//创建数据库连接对象
SqlConnection conn = new SqlConnection(sql);
//创建数据库操作对象
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Users values(‘" + ucode + "‘,‘" + username + "‘,‘" + password + "‘,‘" + nickname + "‘,‘" + sex + "‘,‘" + birthday + "‘,‘" + nation + "‘);";
conn.Open();
int count = cmd.ExecuteNonQuery();
conn.Close();
if (count >= 1)
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
Console.ReadLine();
ADO.NET增添