我正在使用实体框架来连接数据库.我有一个小问题:
我有一个表有一个varbinary(MAX)列(带有文件流).
我正在使用SQL请求来管理"数据"部分,而其余部分则使用EF(文件的元数据).
我有一个代码必须获取文件的所有文件id,文件名,guid,修改日期等.这根本不需要"数据"字段.
有没有办法检索List但没有填充此列?
就像是
context.Files.Where(f=>f.xyz).Exclude(f=>f.Data).ToList();
Run Code Online (Sandbox Code Playgroud)
??
我知道我可以创建匿名对象,但我需要将结果传递给方法,所以没有匿名方法.我不想把它放在匿名类型列表中,然后创建一个非匿名类型(File)列表.
目标是避免这种情况:
using(RsSolutionsEntities context = new RsSolutionsEntities())
{
var file = context.Files
.Where(f => f.Id == idFile)
.Select(f => new {
f.Id, f.MimeType, f.Size, f.FileName, f.DataType,
f.DateModification, f.FileId
}).FirstOrDefault();
return new File() {
DataType = file.DataType, DateModification = file.DateModification,
FileId = file.FileId, FileName = file.FileName, Id = file.Id,
MimeType = file.MimeType, Size = file.Size
};
}
Run Code Online (Sandbox Code Playgroud)
(我在这里使用的是匿名类型,否则你会得到一个NotSupportedException:实体或复杂类型'ProjectName.File'不能在LINQ to Entities查询中构造.)
(例如,此代码抛出先前的异常:
File file2 = context.Files.Where(f => f.Id == idFile)
.Select(f => new File() …Run Code Online (Sandbox Code Playgroud) 好吧,我想重新创建一个我使用EF 4.1到EF 5.0创建的项目,这个项目很简单,或者至少我认为.我的旧项目中的一件事是我能够在运行时在EF 4.1中更改数据库连接字符串:
using (var myContext = new MyEntities(ConnectionString))
{
}
Run Code Online (Sandbox Code Playgroud)
轻松但在EF 5.0中你必须以不同的方式做到这一点:
string connectionString = "data source=LocalHost;initial catalog=MyDatabase;user id=MyUserName;password=MyPassword;multipleactiveresultsets=True;App=EntityFramework";
using (var myContext = new MyEntities())
{
myContext.Database.Connection.ConnectionString = connectionString;
}
Run Code Online (Sandbox Code Playgroud)
现在,这花了我两个小时的时间来弄清楚,所以我想我的问题是这是在运行时更改连接字符串的正确方法吗?如果这是为什么他们做出这个改变?
我找到了这个链接,但它没有用.我收到了Ladislav Mrnka在第一个回答的第一条评论中详述的错误.我后来发现这个链接似乎工作正常.
我重新阅读了我发布的第一个链接,我发现了另一个解决方案,我只是创建了一个部分类:
public partial class MyEntities : DbContext
{
public MyEntities(string connectionString) : base(connectionString)
{
Database.Connection.ConnectionString = connectionString;
}
}
Run Code Online (Sandbox Code Playgroud) entity-framework connection-string entity-framework-4.1 entity-framework-5
我在SQL Server 2008 R2实例中有这个表,我有一个计划的进程,每晚运行它.该表在任何时候都可以有多达500K的记录.处理完这个表之后,我需要从中删除所有行,所以我想知道以下哪种方法会产生最少的开销(即过多的事务日志条目):
删除表的内容是由于时间和它所做的额外事务日志条目.
共识似乎是截断,谢谢大家!
我有一个WinForms应用程序,我想知道是否有更优雅的方法来禁用Combobox项目而不更改所有禁用值的SelectedIndex属性-1.
我一直在谷歌搜索,很多解决方案涉及ASP.Net DropDownLists但这个LINK看起来很有希望.我想我可能要建立自己的ComboBox控件,但在我重新发明轮子之前,我想我会问这里是否有可能.
这是最终解决方案,感谢Arif Eqbal:
//Add a Combobox to a form and name it comboBox1
//
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.Items.Add("Test1");
this.comboBox1.Items.Add("Test2");
this.comboBox1.Items.Add("Test3");
this.comboBox1.Items.Add("Test4");
this.comboBox1.Items.Add("Test5");
this.comboBox1.Items.Add("Test6");
this.comboBox1.Items.Add("Test7");
}
Font myFont = new Font("Aerial", 10, FontStyle.Underline|FontStyle.Regular);
Font myFont2 = new Font("Aerial", 10, FontStyle.Italic|FontStyle.Strikeout); …Run Code Online (Sandbox Code Playgroud) 我一直在搜索超过一个小时,我不能为我的生活弄清楚如何从右边开始搜索字符串变量.我想要做的是获取路径的最后一个文件夹(在文件名之前),在VB6中我会做这样的事情:
Dim s As String
s = "C:\Windows\System32\Foo\Bar\"
Debug.Print Mid(s, InStrRev(Left(s, Len(s) - 1), "\") + 1)
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止尝试的内容:
string s = "C:\\Windows\System32\\Foo\\Bar\\";
s = agencyName.Substring(s.LastIndexOf("\\") + 1)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些文本添加到工具条进度条,但到目前为止我还没有成功,这里是我在这里找到的一些代码:
private void pbPrecentage(ToolStripProgressBar pb)
{
ProgressBar p = new ProgressBar();
int percent = (int)(((double)(pb.Value - pb.Minimum) /
(double)(pb.Maximum - pb.Minimum)) * 100);
using (Graphics gr = pb.CreateGraphics())
{
gr.DrawString(percent.ToString() + "%",
SystemFonts.DefaultFont,
Brushes.Black,
new PointF(pb.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
SystemFonts.DefaultFont).Width / 2.0F),
pb.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
SystemFonts.DefaultFont).Height / 2.0F)));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是Tool Strip Progress Bar没有CreateGraphics方法.所以我想知道是否有人能够成功地将文本添加到工具条进度条.
好吧,似乎ToolStripProgressBar有一个进度条属性,后者又有CreateGraphics方法,但现在的问题是文本值闪烁并闪烁,我该如何修复它?这是修改后的代码:
private void pbPrecentage(ToolStripProgressBar pb)
{
int percent = (int)(((double)(pb.Value - pb.Minimum) /
(double)(pb.Maximum …Run Code Online (Sandbox Code Playgroud) 我的类库中有一个名为Lookup的静态类,我使用这个类来查找不同的值(在本例中为Locations).
这些值可以达到数百个.由于95%的客户在没有Internet访问权限的计算机上安装我的应用程序,因此我必须假设我的应用程序无法访问Internet或访问数据库.
所以我想知道这是否是一种有效的处理方法,如果我在方法完成后正确处理对象:
代码:
using System;
using System.Collections.Generic;
namespace FunctionLibrary
{
public static class Lookups
{
private static List<Vers> Versions;
public static string GetVersion(string s)
{
string retValue = string.Empty;
Versions = new List<Vers>();
try
{
if (s.Trim().Length > 0)
{
GetVersions();
retValue = Versions.Find(ver => ver.VersionNumber == s).VersionLiteral;
if (string.IsNullOrEmpty(retValue))
{
retValue = string.Format("{0} is an Unknown Version Number", s);
}
}
else
{
retValue = "No version number supplied";
}
}
catch
{
retValue = string.Format("{0} is an …Run Code Online (Sandbox Code Playgroud) 我的代码中有一个部分,我在网络上查询所有SQL Server数据库.我首先尝试使用SQL登录来访问SQL Server实例,但如果失败,那么我想尝试使用我的Windows凭据进行连接.之后,如果我仍然无法连接,那么我希望代码失败,然后通知用户.
所以我想我要问的是如何从Try-Catch块内部循环回到Try-Catch块上方的行:
String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;";
bool secondTime = false;
using (SqlConnection sqlConx = new SqlConnection(conxString))
{
Try{
sqlConx.Open();
DataTable tblDatabases = sqlConx.GetSchema("Databases");
sqlConx.Close();
secondTime = false;
Console.WriteLine("SQL Server found!");
}
Catch(System.Data.SqlClient.SqlException e){
if (!secondTime){
secondTime = true;
conxString = @"Data Source=SQLInstance1; Integrated Security=True;";
//Loop back to the using statement to try again with Windows Creds
{
else{
Console.WriteLine("SQL Server not found or credentials refused");
}
//Report Failure to connect to user
}
finally{
//Reset Variable …Run Code Online (Sandbox Code Playgroud) 我希望能够使用我在这里找到的代码在我的C#WinForm应用程序中创建一个数据库.
但我需要找到一种方法来获取该特定SQL Server实例的默认数据目录.我想知道是否有一种简单的方法来实现这一点,可以在各种版本的SQL Server上使用.
提前致谢.
我发现以下Select将返回远程服务器上的默认数据目录:
SELECT
SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1
AND file_id = 1
Run Code Online (Sandbox Code Playgroud)
此解决方案仅适用于SQL Server 2005+.
**
我table使用跟随创建了一个,fields并且使用了interval字段来存储课程的持续时间。我想插入间隔时间,DD:HH但找不到它的语法。
任何人都可以通过上述格式使用此插入语法来帮助我DD:HH。提前致谢。
CREATE TABLE practical.course
(
course_id character(3) primary key,
course_name character varying(30),
duration interval,
dept_id integer references practical.department(dept_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE practical.course
OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×2
sql-server ×2
combobox ×1
create-table ×1
database ×1
dictionary ×1
list ×1
lookup ×1
postgresql ×1
sql ×1
string ×1
t-sql ×1
truncate ×1
try-catch ×1
winforms ×1