在下面的示例中,如果在using语句中引发异常,则连接是否会关闭并处理?
using (var conn = new SqlConnection("..."))
{
conn.Open();
// stuff happens here and exception is thrown...
}
Run Code Online (Sandbox Code Playgroud)
我知道下面这段代码会确保它确实如此,但我很好奇使用声明是如何做到的.
var conn;
try
{
conn = new SqlConnection("...");
conn.Open();
// stuff happens here and exception is thrown...
}
// catch it or let it bubble up
finally
{
conn.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
好的,所以这是那种自以为是的主题之一,但根据您的知识,意见和当前实践,设置以下方案的最佳方法是什么?
我正在构建一个广泛的数据输入应用程序,并且我的意思是我只有基本设置,其中包含大约15-25%的整体程序,我有大约15个部分设置的表单.(他们仍然需要工作)我正在使用SQL Compact 4.0作为我的后端数据库,我不需要更广泛的数据库,因为我没有存储MMO的数据,目前这只是一个本地应用程序.
我希望能够将其设置为显示为单个窗口,只是根据菜单系统更改为各种不同的页面,但我似乎无法找到关于如何实现的良好教程,所以如果有谁知道,请赐教.
然而,问题是如何连接到数据库.我正在使用2个SQLCE数据库,一个存储基于服务和人员的常量数据,另一个存储不断变化的数据或基于第一个数据库输入的新数据.我已经看到了很多不同的方法,如何设置它,目前我正在使用一个我有一个所有其他形式继承的BaseForm.在BaseForm中,我有许多表单共有的方法和变量,从而最大限度地减少了重复的代码量.
这包括两个数据库的连接字符串,以及打开与其中任何一个数据库的连接的2个方法.像这样:
internal SqlCeConnection dataConn = new SqlCeConnection(@"Data Source = |DataDirectory|\opi_data.sdf");
internal SqlCeConnection logConn = new SqlCeConnection(@"Data Source = |DataDirectory|\opi_logs.sdf");
internal SqlCeCommand command;
internal void openDataConnection() // Opens a connection to the data tables
{
try
{
if(dataConn.State == ConnectionState.Closed)
dataConn.Open();
}
catch(SqlCeException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
internal void openLogConnection() // Opens a connection to the log tables
{
try
{
if(logConn.State == ConnectionState.Closed)
logConn.Open();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message, …Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是C#使用块,为什么要使用它?
我正在将旧网站转换为C#,我不确定何时应该使用'使用'.有没有一般指导方针?我知道它的好处,但我并不是100%确定如何使用它.是每次我'新'的方法/财产?
SqlConnection awesomeConn = new SqlConnection(connection);
Run Code Online (Sandbox Code Playgroud) 哪一个更好或更正确?从StreamWriter类创建对象并在方法中经常使用它并最终处理它是否更好?或者是否更好地使用一个对象StringBuilder然后从中创建一个对象StreamWriter并立即处置它?
1)
var Write = new StreamWriter(string.Format("{0}{1}{2}", Environment.CurrentDirectory, Path.DirectorySeparatorChar, "Dummy.txt"));
for (int i = 0; i < 100; i++)
{
//Do something include calculation
Write.WriteLine(something);
}
Write.Flush();
Write.Dispose();
Run Code Online (Sandbox Code Playgroud)
2)
var Str = new StringBuilder();
for (int i = 0; i < 100; i++)
{
//Do something include calculation
Str.AppendLine(something);
}
var Write = new StreamWriter(string.Format("{0}{1}{2}", Environment.CurrentDirectory, Path.DirectorySeparatorChar, "Dummy.txt"));
Write.Write(Str);
Write.Flush();
Write.Dispose();
Run Code Online (Sandbox Code Playgroud) 只是想知道这意味着什么?我在网上看过很多具有以下语法的教程:
using (SqlCeCommand cmd2 = new SqlCeCommand("SELECT city FROM cities JOIN states ON states.id=cities.state WHERE states.state='" + read.GetString(0) + "'", con))
{
SqlCeDataReader readCities = cmd2.ExecuteReader();
while (readCities.Read())
{
parent.Nodes.Add(readCities.GetString(0));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么用它?我尝试搜索Google,但它提供了'using'关键字,用于包含dll和其他文件.
可能重复:
什么是C#使用块,为什么要使用它?
我的问题:使用using(a){do something with a}更好的方式而不是宣布'a'并以这种方式使用它.即:更安全,更快,......
请参阅示例以获得澄清.
例1 :(不使用)
StreamWriter sw;
string line;
sw = new StreamWriter("D:\\NewCon.xml");
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sw.WriteLine("<config>");
for (int i = 0; i >=36; i++)
{
line = "";
line = "<" + xmlnodes[i] + ">";
line += vals[i];
line += "</" + xmlnodes[i] + ">";
sw.WriteLine(line);
}
sw.WriteLine("</config>");
sw.Close();
sw.Dispose();
Run Code Online (Sandbox Code Playgroud)
例2 :(使用时)
string line;
using (sw = new StreamWriter("D:\\NewCon.xml"))
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sw.WriteLine("<config>");
for (int i = 0; i >= …Run Code Online (Sandbox Code Playgroud) 我正在改编这个来源的编码风格指南:http: //www.csharpfriends.com/articles/getarticle.aspx?articleid = 336
在"5.2初始化"下,它建议如下:
如果初始化对话框,请尝试使用using语句:
使用(OpenFileDialog openFileDialog = new OpenFileDialog()){}
这种风格选择的原因是什么?
如果我有以下内容,你好只是为了clairfy:
using (Object1) {
create Object2
}
// bookmark1
Run Code Online (Sandbox Code Playgroud)
将Object2与Object1一起销毁到Object2吗?Object2是StringReader,Object1是MemoryStream.
我想知道是否有必要Close()在下列情况下打电话(如果没有必要,为什么?)
using (var file = System.IO.File.OpenText(myFilePath))
{
...
}
Run Code Online (Sandbox Code Playgroud)
我认为在以下情况下是必要的
StreamWriter file2 = new System.IO.StreamWriter(myFilePath);
newFileContents.ForEach(file2.WriteLine);
file2.Close();
Run Code Online (Sandbox Code Playgroud)
这是对的吗?
编辑:
我认为我的问题很接近() - 具体,可能是阅读和写作之间的区别....
c# ×10
using ×3
idisposable ×2
.net ×1
asp.net ×1
coding-style ×1
connection ×1
database ×1
file-io ×1
sql ×1
syntax ×1