Tag: C#的

如果发生错误,使用语句会回滚数据库事务吗?

我在using语句中有一个IDbTransaction,但是我不确定在using语句中是否抛出exception,是否会回滚。 我知道一个使用语句将强制执行Dispose()的调用…但是有谁知道对于Rollback()是否也是如此? 更新:另外,我是否需要显式调用Commit(),如下所示,或者这也将由using语句来处理? 我的代码看起来像这样: using Microsoft.Practices.EnterpriseLibrary.Data; … using(IDbConnection connection = DatabaseInstance.CreateConnection()) { connection.Open(); using(IDbTransaction transaction = connection.BeginTransaction()) { //Attempt to do stuff in the database //potentially throw an exception transaction.Commit(); } }

在if语句的条件部分中定义一个variables?

我只是感到震惊,这是允许的: if( int* x = new int( 20 ) ) { std::cout << *x << "!\n"; // delete x; } else { std::cout << *x << "!!!\n"; // delete x; } // std:cout << *x; // error – x is not defined in this scope 那么,这是标准所允许的,还是只是一个编译器扩展? PS由于有几个意见,请忽略这个例子是“坏”或危险的。 我知道什么 作为一个例子,这只是我想到的第一件事情。

stdcall和cdecl

有(除其他外)两种调用约定 – stdcall和cdecl 。 我对他们有几个问题: 当一个cdecl函数被调用时,一个调用者怎么知道它是否应该释放栈? 在呼叫站点,呼叫者是否知道被调用的函数是一个cdecl函数还是一个stdcall函数? 它是如何工作的 ? 来电者如何知道是否应该释放堆栈? 还是连接器的责任? 如果一个被声明为stdcall的函数调用一个函数(调用约定为cdecl),或者相反,这会不合适吗? 一般来说,我们可以说哪个调用会更快 – cdecl或stdcall?

如何find在C ++中抛出exception?

我有一个程序抛出一个未捕获的exception的地方。 我所得到的只是一个抛出exception的报告,并没有关于抛出的地方的信息。 编译为包含debugging符号的程序似乎不合逻辑,不会通知我在代码中生成exception的地方。 有没有什么办法可以告诉我哪些exception是由于在gdb中设置'catch throw'而引起的,并且为每一个抛出的exception调用一个回溯?

堆栈是向上还是向下?

我有这段代码在c: int q = 10; int s = 5; int a[3]; printf("Address of a: %d\n", (int)a); printf("Address of a[1]: %d\n", (int)&a[1]); printf("Address of a[2]: %d\n", (int)&a[2]); printf("Address of q: %d\n", (int)&q); printf("Address of s: %d\n", (int)&s); 输出是: Address of a: 2293584 Address of a[1]: 2293588 Address of a[2]: 2293592 Address of q: 2293612 Address of […]

WPF MVVM为什么使用ContentControl + DataTemplate Views而不是直观的XAML Window Views?

我有一个关于WPF中的MVVM的问题,正在驱动我batty。 为什么要这样做:? MainWindow.xaml: <Window x:Class="MVVMProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <ContentControl Content="{Binding}"/> </Grid> </Window> 把你的ExampleView.xaml设置为: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vms="clr-namespace:MVVMProject.ViewModels"> <DataTemplate DataType="{x:Type vms:ExampleVM}" > <Grid> <ActualContent/> </Grid> </DataTemplate> </ResourceDictionary> 并像这样创build窗口: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MainWindow app = new MainWindow(); ExampleVM context = new ExampleVM(); app.DataContext = context; app.Show(); […]

HttpClient请求抛出IOException

下面的代码抛出一个IOException消息:“指定的registry项不存在”。 HttpClient client = new HttpClient(); Uri uri = new Uri("http://www.google.com"); client.GetAsync(uri); 这只是在Main中的一个控制台应用程序。 它看起来像错误正在由mscorlib.dll!Microsoft.Win32.RegistryKey.Win32Error(int errorCode,stringstr)引发。 我不知道为什么这个错误被抛出或如何开始debugging。 编辑堆栈跟踪: 在Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode,String str) 这只是一条线,没有内在的含义。 调用堆栈是: mscorlib.dll!Microsoft.Win32.RegistryKey.Win32Error(int errorCode, string str) + 0x189 bytes mscorlib.dll!Microsoft.Win32.RegistryKey.GetValueKind(string name) + 0x7f bytes System.dll!System.Net.HybridWebProxyFinder.InitializeFallbackSettings() + 0x9e bytes [Native to Managed Transition] [Managed to Native Transition] System.dll!System.Net.AutoWebProxyScriptEngine.AutoWebProxyScriptEngine(System.Net.WebProxy proxy, bool useRegistry) + 0xd0 bytes System.dll!System.Net.WebProxy.UnsafeUpdateFromRegistry() + […]

在Windowsapp store应用中获取CoreDispatcher的正确方法

我正在构build一个Windowsapp store应用程序,我有一些代码需要发布到UI线程。 为此,我想检索CoreDispatcher并使用它来发布代码。 看来有几种方法可以这样做: // First way Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher; // Second way Window.Current.Dispatcher; 我想知道哪一个是正确的? 或者两者是否相同?

如何获得使用system.net.webrequest在C#中的JSON响应?

我需要从外部域获取json数据。 我使用webrequest从网站获得响应。 代码如下: var request = WebRequest.Create(url); string text; var response = (HttpWebResponse) request.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } 任何人都知道为什么我不能得到JSON数据?

int main(){return(0);}上的浮点exception(SIGFPE) }”

我正在尝试为两个不同的Linux环境构build一个简单的C程序。 在一个设备上,程序运行良好,在另一个设备上程序产生一个浮点exception。 该程序什么都不做,但从主返回0,这使我相信有一些不符合启动代码或ABI? 该程序是与海湾合作委员会与以下生成规范编译: 使用内置的规格。 目标:i386-redhat-linuxconfiguration:../configure –prefix = / usr –mandir = / usr / share / man –infodir = / usr / share / info –enable-shared –enable-threads = posix –enable-checking = release –with-system-zlib –enable -__ cxa_atexit –disable-libunwind-exceptions –enable-libgcj-multifile –enable-languages = c,c ++,objc,obj-c ++ ,java,fortran,ada –enable-java-awt = gtk –disable -dssi –disable-plugin –with-java -home = / […]