ADO.NET包括两部分1.程序端数据集 包括DataSet/DataTable/DataRow
2..NET Framwork数据提供程序 包括Connection/DataAdapter/DataReader/Commander
///简单的例子:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; //包含数据集(DataSet)/数据表(DataTable)/数据行(DataRow)。。。类using System.Data.SqlClient; //包含 连接(Connection)/命令(Commander)/适配器(DataAdapter)/读取器(DataReader)。。。类
namespace ADO_NetConnection{class Program{static void Main(string[] args){Program sq = new Program();sq.dataPractice();}
public void dataPractice(){int res = -1;//1.连接通道的连接字符串string str = "server=.;database =test;uid=sa;pwd=chy19900909";//2.连接通道对象SqlConnection conn = new SqlConnection(str);string sql = "insert into [User] values (2,'chengyuan',23,null)"; //易错点:如果数据库表名是User这样的关键词要注意加[]string sql2 = "insert into Name values(1,'chy')";SqlCommand cmd = new SqlCommand(sql2, conn);
//3.打开通道conn.Open();res = cmd.ExecuteNonQuery();//关闭通道conn.Close();if (res > 0){Console.WriteLine("数据库更新成功");}else{Console.WriteLine("数据库更新失败");}Console.ReadKey();
}}}
ADO_NET数据库连接示例
最新推荐文章于 2022-09-16 11:42:37 发布