I have been programming 开发者_高级运维in vb6 for few time ago and i used open SQL Server connection and command objects to make database traansactions. I have been searching for similar approaches in vb.net too but not finding any starting point.
How can we work similarly in vb.net application?
I think you're looking for SqlConnection
and SqlCommand
.
The MSDN page for SqlCommand
shows a sample for how they can be used:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
I would recommend using the SqlDataReader when possible for retrieving data. It is a faster option, and it sounds like Microsoft is not investing in the future of 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();
}
}
精彩评论