我想在我的代码后面创建一个数据库连接。然后,后面的代码将获取两个字段的所有值,并将它们放入多个deminsional数组中。如果有标准或经过验证的方法,请告诉我。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:englishBritishConnectionString %>"
SelectCommand="SELECT [input], [ouput] FROM [converstion]">
</asp:SqlDataSource>
这是与asp.net的标准数据库连接,以获得更准确的英特尔。
答案 0 :(得分:1)
这就是我一直以来的做法。进口/使用上升,其余进入功能。这是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();
}