我用vb6编程已经有一段时间了,我使用open SQL Server connection和command对象来进行数据库事务。我也一直在vb.net中寻找类似的方法,但没有找到任何起点。
我们如何在vb.net应用程序中进行类似的工作?
发布于 2010-05-26 16:22:54
我想你在找SqlConnection和SqlCommand吧。
SqlCommand的MSDN页面显示了如何使用它们的示例:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
发布于 2010-05-28 02:08:16
我建议在可能的情况下使用SqlDataReader检索数据。这是一个更快的选择,而且听起来微软似乎不会对DataSets的未来进行投资。
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
string sql = "Select FirstName, LastName from Customers";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
Customer cust = new Customer();
cust.FirstName = reader["FirstName"].ToString();
cust.LastName= reader["LastName"].ToString();
collection.Add(cust);
}
reader.Close();
}
conn.Close();
}
}https://stackoverflow.com/questions/2911195
复制相似问题