小编Stu*_*ler的帖子

将参数添加到IDbCommand

我正在创建一个小辅助函数来返回一个DataTable.我想跨所有供应商合作ADO.Net支持,所以我想到了使一切使用IDbCommandDbCommand在可能的情况.

我用以下代码达到了绊脚石:

    private static DataTable QueryImpl(ref IDbConnection conn, String SqlToExecute, CommandType CommandType, Array Parameters)
    {
        SetupConnection(ref conn);
        // set the capacity to 20 so the first 20 allocations are quicker...
        DataTable dt = new DataTable();
        using (IDbCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = SqlToExecute;
            cmd.CommandType = CommandType;
            if (Parameters != null && Parameters.Length > 0)
            {
                for (Int32 i = 0; i < Parameters.Length; i++)
                {
                    cmd.Parameters.Add(Parameters.GetValue(i));
                }
            }
            dt.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges);
        } …
Run Code Online (Sandbox Code Playgroud)

c# datatable

46
推荐指数
2
解决办法
4万
查看次数

C++抽象基类构造函数/析构函数 - 一般正确性

最近我作为一名开发人员愚蠢,所以我冒险尝试了一本C++书籍并学习如何正确地做事.在我脑海里,我知道自己想做什么.我有效地想要一个Interface继承的,必须被覆盖(如果这是可能的?).到目前为止,我有以下内容:

class ICommand{

public:
    //  Virtual constructor. Needs to take a name as parameter
    //virtual ICommand(char*) =0;
    //  Virtual destructor, prevents memory leaks by forcing clean up on derived classes?
    //virtual ~ICommand() =0; 
    virtual void CallMe() =0;
    virtual void CallMe2() =0;
};

class MyCommand : public ICommand
{
public:
    // Is this correct?
    MyCommand(char* Name) { /* do stuff */ }
    virtual void CallMe() {}
    virtual void CallMe2() {}
};
Run Code Online (Sandbox Code Playgroud)

我故意离开了我认为应该实现构造函数/析构函数的方式ICommand.我知道如果删除注释,它将无法编译.请有人可以:

  1. 演示如何声明构造函数/析构函数ICommand以及如何使用它们MyCommand
  2. 我是否正确设置, …

c++ inheritance abstract-class virtual-inheritance

16
推荐指数
1
解决办法
4万
查看次数

最大自定义窗口会丢失阴影效果

我有一个自定义WPF窗口定义为:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" MinHeight="300" Height="350" MinWidth="600" Width="700"      ResizeMode="CanResizeWithGrip" AllowsTransparency="True" WindowStyle="None">
Run Code Online (Sandbox Code Playgroud)

我在网上发现了一个创建阴影的课程,如下所示.即使使用调整大小的抓地力,这也很有效,直到我最大化窗口.一旦我最大化窗口或更改另一个窗口的窗口状态(例如Visual Studio),我就会松开阴影,我无法将其恢复.有任何想法吗?


阴影类:

Public Class DropShadow

Private Shared _handler As EventHandler = New EventHandler(AddressOf window_SourceInitialized)

<DllImport("dwmapi.dll", PreserveSig:=True)> _
Private Shared Function DwmSetWindowAttribute(hwnd As IntPtr, attr As Integer, ByRef attrValue As Integer, attrSize As Integer) As Integer

End Function

<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(hWnd As IntPtr, ByRef pMarInset As Margins) As Integer
End Function

Public Shared Sub DropShadowToWindow(window As Window)
    If Not DropShadow(window) Then
        AddHandler window.SourceInitialized, _handler …
Run Code Online (Sandbox Code Playgroud)

vb.net wpf xaml window

14
推荐指数
3
解决办法
1万
查看次数

需要多个锁定对象?

鉴于以下课程:

class x
{
    Object lockOne = new Object();
    Object lockTwo = new Object();

    List<Something> listOne = new List<Something>();
    List<Something> listTwo = new List<Something>();

    void MethodOne()
    {
        lock(lockOne)
        {
            // some operation on listOne
        }
    }

    void MethodTwo()
    {
        lock(lockTwo)
        {
            // some operation on listTwo
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用两个锁定对象是正确的,假设MethodOne()并且MethodTwo()可以同时从不同的线程调用并注意到listOne并且listTwo无论如何都不相关.锁中涉及的唯一操作是上面注释中指定的操作.

c# concurrency multithreading locking

13
推荐指数
2
解决办法
3999
查看次数

'Y'中的实体参与'FK_Y_X'关系.找到0个相关的'X'.预计1'X'

我和1..*之间有关系,父母在哪里.当我尝试删除记录时,我收到以下异常消息:XYXY

'Y'中的实体参与'FK_Y_X'关系.找到0个相关的'X'.预计1'X'.

我目前正尝试以通用,断开连接的方式删除记录,方法如下:

public bool Delete(TEntity entity)
{
    if (entity == null)
    {
        return false;
    }
    try
    {
        var entry = _context.Entry(entity);
        entry.State = EntityState.Deleted;
        _context.SaveChanges();
        return true;
    }
    catch
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

传入的实体AsNoTracking()在相同的上下文中加载.

有任何想法吗?

c# generics entity-framework-6

12
推荐指数
3
解决办法
9217
查看次数

ResolveBundleUrl没有解析所有文件?

我是MVC的新手,所以我想我会开始一个新项目并尝试MVC4中的一些新功能.我的Content目录中有两个css文件,normalise_minisite.css.当我使用以下代码时:

<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

它只需要我的site.css文件而不是我的规范化文件.我的应用开始时有以下内容:

protected void Application_Start()
{

    // Remove all other view engines except razor:
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    BundleTable.Bundles.EnableDefaultBundles();
}
Run Code Online (Sandbox Code Playgroud)

我是否需要为每个css文件创建一个包(就像这个人一样)?或者它应该只是自动找到所有的css文件(我希望这是默认行为).值得注意的是,我开始将这个项目作为基于Razor视图引擎的空网站(实际上根本不是空的:/)

提前致谢


更新

根据发布的链接,我需要注释掉注册模板包的行.例如:

protected void Application_Start()
{

    // Remove all other view engines except razor:
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    //BundleTable.Bundles.RegisterTemplateBundles();  // This is not needed
    BundleTable.Bundles.EnableDefaultBundles();
}
Run Code Online (Sandbox Code Playgroud)

这现在有效.我的想法是两条线应该没有任何问题共存.我想这是设计的,但是设计不好吗?

c# optimization asp.net-mvc-4 asp.net-optimization

11
推荐指数
1
解决办法
7009
查看次数

如何检查存储过程的SQL Server性能调整

我正在为将在生成一些业务逻辑的应用程序创建存储过程.如何检查存储过程的性能,如速度,准确度等.任何想法或方向将非常感激

sql-server sql-server-2008

7
推荐指数
1
解决办法
2万
查看次数

在Datagrid-WPF中垂直显示水平表

我有一个从数据库显示的表.我想显示水平标题栏 - >垂直

我的表结构是

  Server|Role|Status|Date
Run Code Online (Sandbox Code Playgroud)

但我想显示为

  Server
  Role
  Status
  Date
Run Code Online (Sandbox Code Playgroud)

我试图翻转数据集,并尝试构建它.最初构建成功但我无法在我的数据网格上查看任何数据.亲切的帮助,有没有其他方法来解决这个问题?

这是我的代码片段

SqlConnection con;
SqlDataAdapter da = null;
DataSet ds = null;

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    try
    {
       da = new SqlDataAdapter("Select * from [ServerDB_Test].[dbo].[ServerStatus] ", con);
       ds = new DataSet();
       foreach (DataTable dt in my_DataSet.Tables)
       {
            DataTable table = new DataTable();

            for (int i = 0; i <= dt.Rows.Count; i++)
            {
                table.Columns.Add(Convert.ToString(i));  }
                DataRow r;
                for (int k = 0; k < dt.Columns.Count; k++)
                { 
                    r …
Run Code Online (Sandbox Code Playgroud)

c# sql-server wpf wpfdatagrid

7
推荐指数
1
解决办法
6928
查看次数

多线程使用正则表达式

鉴于以下来自MSDN:

可以在任何线程上创建正则表达式对象并在线程之间共享.

我发现,对于性能,最好不要Regex在使用ThreadLocal类时在线程之间共享实例.

请问有人可以解释为什么线程本地实例的运行速度大约快5倍?

以下是结果(在8核机器上):

   Using Regex singleton' returns 3000000 and takes 00:00:01.1005695
   Using thread local Regex' returns 3000000 and takes 00:00:00.2243880
Run Code Online (Sandbox Code Playgroud)

源代码:

using System;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static readonly string str = new string('a', 400);
        static readonly Regex re = new Regex("(a{200})(a{200})", RegexOptions.Compiled);

        static void Test(Func<Regex> regexGettingMethod, string methodDesciption)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var sum = Enumerable.Repeat(str, 1000000).AsParallel().Select(s => regexGettingMethod().Match(s).Groups.Count).Sum(); …
Run Code Online (Sandbox Code Playgroud)

.net regex performance multithreading thread-local

6
推荐指数
1
解决办法
1431
查看次数

从组播数据包中获取发送方IP

你如何获得多播UDP数据包的发送者的IP?当前代码以同步/阻塞方式设置(请参阅下面的注释).这是代码:

    private void receive()
    {
        string mcastGroup = SetMcastGroup();
        s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        s.EnableBroadcast = true;
        IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5000);
        s.Bind(ipep);
        IPAddress ip = IPAddress.Parse(mcastGroup);
        s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));

        while (true)
        {
            try
            {
                byte[] b = new byte[4096];
                s.Receive(b);
                string str = Encoding.ASCII.GetString(b, 0, b.Length);
                //this.SetText(ipep.Address + ": " + str.Trim());
                this.SetText(senderIP() + ": " + str.Trim());
            }
            catch{}
        }
    }
Run Code Online (Sandbox Code Playgroud)

注意:这个问题来自聊天,因为这不是我的代码.我只是问,因为我理解这个问题.

c# network-programming multicast

6
推荐指数
1
解决办法
1509
查看次数