using-statement

Global “using” directives in VS2010/C#?

半腔热情 提交于 2020-01-03 11:29:19
问题 I'm pretty sure I know the answer but I'm wondering if there's a way to define a global "using" directive in my C# projects so that I don't have to repeat the directive on top of every code file. My interest is really rooted with the introduction of extension methods in the .NET Framework. The only way to use an extension method is to define a using directive for the namespace containing the extension methods. Without the using directive, I lose Intellisense capabilities for the extension

SqlCommand (Using Statement / Disposing issue)

可紊 提交于 2020-01-01 04:45:10
问题 Take the following example... Using cn As New SqlConnection(ConnectionString) Try Dim cmd As SqlCommand = New SqlCommand With cmd .Connection = cn .Connection.Open() .CommandText = "dbo.GetCustomerByID" .CommandType = CommandType.StoredProcedure .Parameters.Add("@CustomerID", SqlDbType.Int, 4) .Parameters("@CustomerID").Value = CustomerID End With da = New SqlDataAdapter(cmd) da.Fill(ds, "Customer") Catch ex As Exception End Try End Using From my research today is sounds as though this is

C# using statement catch error

天大地大妈咪最大 提交于 2019-12-31 08:13:08
问题 I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year; cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start; cmd.Parameters.Add("@endDate", SqlDbType

C# using statement catch error

不羁的心 提交于 2019-12-31 08:12:08
问题 I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year; cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start; cmd.Parameters.Add("@endDate", SqlDbType

Is C#'s using statement abort-safe?

纵饮孤独 提交于 2019-12-28 05:49:31
问题 I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138), using (StreamReader reader = File.OpenText("file.txt")) { ... } is precisely equivalent to: StreamReader reader = File.OpenText("file.txt"); try { ... } finally { if (reader != null) ((IDisposable)reader).Dispose(); } Suppose, however, that this is true and

Does Java have a using statement?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-28 03:23:10
问题 Does Java have a using statement that can be used when opening a session in hibernate? In C# it is something like: using (var session = new Session()) { } So the object goes out of scope and closes automatically. 回答1: Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn't have anything resembling using . As an example, you can use any variable implementing java.lang.AutoCloseable in the following way: try

Disposing datacontext causes Invalid attempt to call Read when reader is closed

白昼怎懂夜的黑 提交于 2019-12-24 03:42:57
问题 I'm building an MVC 2 app and using linq to sql with stored procs. I created a data access layer that has an internal datacontext class and a public class that I expose applications. In my public class I expose methods which access the datacontext class and convert the data to my own object model classes using linq. In my public class, I would expose a method using the following patter: public IEnumerable<MyObject> ListObjects(int iParameter) { using (MyDataContext db = new MyDataContext) { /

Best practice to avoid multiple disposals with the `using` keyword in C#

拈花ヽ惹草 提交于 2019-12-24 03:05:11
问题 When a variable is IDisposable, we have the using keyword to manage the disposal. But what if we return the value in a method, should we have using twice? StringContent stringToStringContent(string str) { using (StringContent content = new StringContent(str)) { return content; } } void logStringContent() { using (StringContent content = stringToStringContent("test")) { Debug.WriteLine(content.ToString()); return; } } In this example above, I only have 1 new but I have 2 using for the same

How to use object initializers with using statements?

本小妞迷上赌 提交于 2019-12-23 17:46:00
问题 Is there any way to refactor this code to not have to use a temporary variable and still use the syntactic sugar associated with object initializers? FrmSomeForm someTempForm = new FrmSomeForm() { SomePropA = "A", SomePropB = "B", SomePropC = "C" }; using (FrmSomeForm someForm = someTempForm) { someForm.ShowDialog(); } 回答1: using (FrmSomeForm someForm = new FrmSomeForm(){ SomePropA = "A", SomePropB = "B", SomePropC = "C" }) { someForm.ShowDialog(); } doesn't this work? oO 回答2: using

Why is my FileStream object being disposed of when I'm “using” a BinaryReader object?

一个人想着一个人 提交于 2019-12-23 14:36:08
问题 Consider the following function: private int GetSomethingFromFile(FileStream fs) { using (BinaryReader br = new BinaryReader(fs)) { fs.Seek(0, SeekOrigin.Begin); return br.ReadInt32(); } } A FileStream object is passed in as a parameter and a BinaryReader is declared with a using statement. When I try to use that FileStream object, after calling this function, it throws a System.ObjectDisposedException. Why is that FileStream object being disposed of along with the BinaryReader object? 回答1: