这就是我一直这样做的方式。进口/使用上升到顶部,其余的进入一个函数。这是VB版本:
Imports System.Data.SqlClient
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("englishBritishConnectionString").ConnectionString.ToString)
Dim cmd As New SqlCommand("SELECT [input], [ouput] FROM [converstion]", conn)
Dim dr As SqlDataReader = Nothing
Try
conn.Open()
dr = cmd.ExecuteReader()
While dr.Read()
*do whatever with variables*
End While
Catch ex As Exception
lblStatus.Text = "Error: " + ex.Message + vbCrLf + _
"Call stack:" + vbCrLf + ex.StackTrace
Finally
dr.Close()
conn.Close()
End Try
我会看看我能对 C# 版本做些什么,因为我使用 C# 已经有一段时间了。
using System.Data.SqlClient
SqlConnection conn = New SqlConnection(ConfigurationManager.ConnectionStrings("englishBritishConnectionString").ConnectionString.ToString);
SqlCommand cmd = New SqlCommand("SELECT [input], [ouput] FROM [converstion]", conn);
SqlDataReader dr;
Try
{
conn.Open();
dr = cmd.ExecuteReader();
While dr.Read();
{
*do whatever with variables*
}
}
Catch Exception ex
{
lblStatus.Text = "Error: " + ex.Message + " Call stack:" + ex.StackTrace;
}
Finally
{
dr.Close();
conn.Close();
}