您的位置:首页 > 博客中心 > 数据库 >

Ado.Net 连接数据库

时间:2022-03-10 18:08

     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) { }

  

Ado.Net 连接数据库,布布扣,bubuko.com

热门排行

今日推荐

热门手游