什么时候使用SqlDataAdapter和sqlcommand? [英] when to use SqlDataAdapter and sqlcommand ?

查看:183
本文介绍了什么时候使用SqlDataAdapter和sqlcommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在表格中插入数据

this code insert data in table

str = "insert into table values()";
SqlCommand cmd = new SqlCommand();
cmd.Connection="connection string";
cmd.CommandText = str;
cmd.ExecuteNonQuery();



此代码也在表格中插入数据


and this code also insert data in table

string str = "insert into table values";
SqlDataAdapter adp = new SqlDataAdapter(str, "connection string");
DataSet ds = new DataSet();
adp.Fill(ds);



那么它们之间有什么不同以及使用哪一个很好?



由Jibesh添加的前标签[/ Edit]

推荐答案

使用SqlCommand执行INSTER / UPDATE / DELETE查询和SqlDataAdapter可用于从数据库中检索数据。



您在问题中提供的第二个查询是错误的。该查询应该是SELECT查询而不是INSERT
Use SqlCommand to execute INSTER/UPDATE/DELETE queries and SqlDataAdapter can be used for retrieving data from the database.

The second query you provided in your question is wrong. That query should be a SELECT query rather than INSERT


当您使用数据集时,实际上您将表信息保存在ram中。但是在使用命令时,您正在填充存储在硬盘中的物理表。所以当你使用数据集时,你可以看到更多的速度;)
When you are using Dataset, in fact you are saving the table information in ram. but when using command, you are filling the physical table which is stored in hard. so when you are using dataset, you can see more speed ;)


这篇关于什么时候使用SqlDataAdapter和sqlcommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆