liang520521通过本文主要向大家介绍了c#,visual studio,数据库等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
数据库的修改
该程序是在建立好数据哭的基础上进行的,详情请参考笔记————C#数据库的读取
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 数据库的修改 { class Program { static void Main(string[] args) { string strcon = @"Data Source =(localdb)\MSSQLLocalDB;Initial Catalog = new;Integrated Security = True"; using (SqlConnection sqlcon = new SqlConnection(strcon))//创建数据库的连接 { sqlcon.Open(); Console.WriteLine("数据库连接状态:" + sqlcon.State.ToString()); string sql = @"SELECT* FROM XSB"; //检索 SqlCommand command = new SqlCommand(sql, sqlcon);//创建一个sqlcommand 对象 string a = Console.ReadLine();//输入修改学生的学号 #region//数据库的修改 //sql: //update tablename(数据库表格的名字) set age=4 where age=2。 //解释 :上面表的意思是更新tablename表中age字段值2为4。update语句的作用主要就是通过对某些特定表进行更新,如果没有where条件语句的话,就是更加整张表的age字段值为4。 string MyUpdate = "Update XSB set XH='" + Console.ReadLine().ToString() + "',XM='" + Console.ReadLine().ToString() + "',CSRQ = '" + Console.ReadLine().ToString() + "' where XH=" + a; //修改 SqlCommand MyCommand = new SqlCommand(MyUpdate, sqlcon);//将修改的内容打包到Command中 try { if(MyCommand.ExecuteNonQuery().ToString()){//查看数据表中受影响的行数} } catch (Exception ex) { Console.WriteLine("{0} Exception caught.", ex); } Console.Read(); #endregion } } } }