配置连接数据库
<connectionStrings> <add name="strCon" connectionString="server=*.168.13.*\SERVICE;user id=sa; password=123456; database=TimeRecord;"/> </connectionStrings>
准备一个UserInfo表
CREATE TABLE [dbo].[UserInfo]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [MyPic] [nvarchar](255) NULL, [Email] [nvarchar](50) NULL, [Password] [nvarchar](50) NULL, [Gender] [nvarchar](50) NULL, [Birthday] [datetime] NULL, [Textarea] [nvarchar](255) NULL, [CreateTime] [datetime] NULL, CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
用vs建立一个控制台应用程序
新建一个UserInfo类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class UserInfo { private int iD; private string name; private string myPic; private string email; private string password; private string gender; private DateTime birthday; private string textarea; public string Name { get { return name; } set { name = value; } } public string MyPic { get { return myPic; } set { myPic = value; } } public string Email { get { return email; } set { email = value; } } public string Gender { get { return gender; } set { gender = value; } } public DateTime Birthday { get { return birthday; } set { birthday = value; } } public string Textarea { get { return textarea; } set { textarea = value; } } public string Password { get { return password; } set { password = value; } } public int ID { get { return iD; } set { iD = value; } } } }
项目添加Dapper引用,通过工具→NUg包管理器→解决方案的nug管理包,进行查询dapper进行安装。
主程序
using Dapper; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //读取config中的字符串 string connstr = ConfigurationManager.ConnectionStrings["strCon"].ToString(); //IDbConnection IDbConnection connection = new SqlConnection(connstr); //插入操作 // var result = connection.Execute("insert into UserInfo (name,mypic,email,[password],gender,birthday) values (@Name,@MyPic,@Email,@Password,@Gender,@Birthday)", new { Name = "kfz", MyPic = "74.jpg",Email="[email protected]", Password = "123456" ,Gender="Male", Birthday="1994-11-09"}); //批量插入操作 // var UserList = Enumerable.Range(0, 10).Select(i => new UserInfo() { Name = "kfz" + i,Textarea=i.ToString(),Password="456"+i }); // var result2 = connection.Execute("insert into UserInfo (Name,Textarea,Password) values(@Name,@Textarea,@Password)", UserList); //查询操作 // var query = connection.Query<UserInfo>("select * from UserInfo where Name =@Name", new UserInfo() { Name = "VinKong" }); // foreach (var item in query) // { // Console.WriteLine(item.Name+"==="+item.Email); // } //更新操作 //var result = connection.Execute("update UserInfo set Name ='MyGirl' where ID=@ID",new UserInfo (){ID = 10008 }); //删除操作 var result = connection.Execute("delete from UserInfo where ID = @ID",new UserInfo() { ID=10010}); Console.ReadKey(); } } }