首页 > 代码库 > ado.net
ado.net
使用前先添加
using System.Data.SqlClient;
delete删除:
namespace adonet_接受_删除_添加 { class Program { static void Main(string[] args) { Console.WriteLine("请输入要删除的用户名:"); string uname = Console.ReadLine(); //1、创建数据库链接类 string sql = "server=.;database=data0928;user=sa;pwd=123;"; SqlConnection conn = new SqlConnection(sql); //2、创建数据库操作类 SqlCommand cmd = conn.CreateCommand(); //3、编写操作语句 cmd.CommandText = "delete from users where username=‘" + uname + "‘"; //4、打开数据库连接 conn.Open(); //5、执行操作,返回受影响行数 int a= cmd.ExecuteNonQuery(); //6、关闭数据库连接 conn.Close(); //7、提示操作是否成功 if (a > 0) { Console.WriteLine("删除成功"); } else { Console.WriteLine("删除失败"); } Console.ReadLine(); } } }
insert添加:
namespace insert { class Program { static void Main(string[] args) { Console.WriteLine("请输入要添加的用户名:"); string uname = Console.ReadLine(); Console.WriteLine("请输入要添加的密码:"); string pwd = Console.ReadLine(); Console.WriteLine("请输入要添加的昵称:"); string nick = Console.ReadLine(); Console.WriteLine("请输入要添加的性别:"); string sex = Console.ReadLine(); Console.WriteLine("请输入要添加的生日:"); string bir = Console.ReadLine(); Console.WriteLine("请输入要添加的民族:"); string nation = Console.ReadLine(); Console.WriteLine("请输入要添加的班级:"); string cla = Console.ReadLine(); //创建数据库连接类 SqlConnection con = new SqlConnection("server=.;database=data0928;user=sa;pwd=123"); //创建数据库操作类 SqlCommand cm = con.CreateCommand(); //编写操作语句 cm.CommandText = "insert into users values(‘" + uname + "‘,‘" + pwd + "‘,‘" + nick + "‘,‘" + sex + "‘,‘" + bir + "‘,‘" + nation + "‘,‘" + cla + "‘)"; //打开数据库连接 con.Open(); //执行操作 cm.ExecuteNonQuery(); //关闭数据库 con.Close(); Console.ReadLine(); } } }
select选择:
namespace select { class Program { static void Main(string[] args) { //创建连接 SqlConnection conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123"); //创建数据库操作 SqlCommand cmd = conn.CreateCommand(); //编写操作语句 cmd.CommandText = "select * from users"; conn.Open(); SqlDataReader dr= cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Console.WriteLine(dr["ids"] + "\t" + dr["username"] + "\t" + dr["password"] + "\t" + dr["nickname"] + "\t" + dr["sex"] + "\t" + dr["birthday"] + "\t" + dr["nation"] + "\t" + dr["class"]); } } conn.Close(); Console.ReadLine(); } } }
update更改:
namespace Update { class Program { static void Main(string[] args) { Console.Write("请输入要修改的用户名:"); string uname = Console.ReadLine(); Console.Write("请输入密码:"); string pwd = Console.ReadLine(); Console.Write("请输入昵称:"); string nick = Console.ReadLine(); Console.Write("请输入性别:"); string sex = Console.ReadLine(); Console.Write("请输入生日:"); string bir = Console.ReadLine(); Console.Write("请输入民族:"); string nation = Console.ReadLine(); Console.Write("请输入班级:"); string cla = Console.ReadLine(); SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "update Users set PassWord = ‘" + pwd + "‘,NickName = ‘" + nick + "‘,Sex=‘" + sex + "‘,Birthday=‘" + bir + "‘,Nation=‘" + nation + "‘,Class=‘" + cla + "‘ where UserName = ‘" + uname + "‘"; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } }
ado.net
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。