Ado.Net 连接数据库

     try
            {
                //using 用于对资源进行回收,实现了IDisposible借口类才能用。
                using(SqlConnection conn=new SqlConnection("Data Source=.;Initial Catalog=HGPersonnel;User Id=sa;Password=123;"))
                {
                    conn.Open(); //打开连接
//通过连接创建一个向数据库发命令(Command)对象。 using (SqlCommand cmd = conn.CreateCommand()) { //CommandText 要执行的sql语句, cmd.CommandText = "Insert into tb_user(name,age)value('123','123')";
cmd.COmmandText="Insert into tb_user(name,age)output inserted.id value('123','123')";//插入数据并且输出刚插入数据的id
cmd.ExecuteNonQuery();//ExecuteNonQuery 执行
 cmd.ExecuteScalar()//用来执行只有一行一列的返回值的sql语句 } } } catch (Exception) { }

  

原文地址:https://www.cnblogs.com/fang645421992/p/3860179.html