定义字符串然后传到数据库中(数据库字符串拼接)
作者
1、EF模板生成继承DbContext定义字符串然后传到数据库中的类文件重新建一个部分类文件避免每次生成实体都会被覆盖掉。2、重新构造调用数据库连接字符串定义字符串然后传到数据库中的函数//VS模板生成示例:public Test_DBEntities(): base("name=Test_DBEntities"){} //重写函数public Test_DBEntities(string connectionString): base(connectionString){} 3、自定义EF数据库字符串函数(SQLServer) public static string GetConnection(){string DataSource = ".";string DataBase = "DB;string UserId = "sa";string PassWord = "sa";SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();sqlBuilder.DataSource = DataSource;sqlBuilder.InitialCatalog = DataBase;sqlBuilder.PersistSecurityInfo = true;sqlBuilder.IntegratedSecurity = true;sqlBuilder.MultipleActiveResultSets = true;sqlBuilder.UserID = UserId;sqlBuilder.Password = PassWord;EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();entityBuilder.ProviderConnectionString = sqlBuilder.ToString();entityBuilder.Metadata = "res://*/";entityBuilder.Provider = "System.Data.SqlClient";return entityBuilder.ToString();} 4、调用示例 string definestr = "";//自定义数据库查询结果string str = "";using (var db = new Test_DBEntities()){var list = db.T_User.Select(m=>m.name).ToList();str= string.Join(",", list);}using (var db = new Test_DBEntities(ConnectionHelper.GetConnection())){var list = db.T_User.Select(m => m.name).ToList();definestr = string.Join(",", list);}
目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~