SqlCommand的与功放之间的混淆; SqlDataAdapter的功放、SqlCommand、SqlDataAdapter

2023-09-04 01:20:15 作者:心里葬着未亡人

大家我是一个学生,新的.NET和专门MVC3发展,而且为我的项目之一,我已经努力过它,所以经过学习阶段 问题和困惑,我面对的是关于DB-连接,whast我leanreeð关于从数据库中检索的记录是这样的:

everyone I am a student and new to .NET and specially MVC3 development but for one of my project I’ve to work over it and so going through the learning phase Issue and confusion I am facing is regarding DB-Connectivity, whast I leanree d regarding retrieving records from a database is something like this:

//Method One:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM table";
var cmd = new SqlCommand(cmdString, conn); 
var mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter.Fill(myDataSet, "design");
// making a new SqlCommand object with stringQuery and SqlConnection object THEN a new SqlDataAdapter object with SqlCommand object and THEN filling up the table with the resulting dataset.

但是,当我被检查出 MSDN库我发现这SqlDataAdapter的提供了一个构造函数的SqlDataAdapter(字符串,字符串)直接需要的SelectCommand和一个连接字符串初始化因此跳过的SqlCommand中的作用,是这样的:

But while I was checking out MSDN Library i found out that SqlDataAdapter offers a constructors SqlDataAdapter(String, String) that directly takes a SelectCommand and a connection string to initiate thus skipping the role of SqlCommand in between, like this:

//Method Two:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM table";
var mySqlDataAdapter = new SqlDataAdapter(cmdString, conn);
mySqlDataAdapter.Fill(myDataSet, "design");

看起来短,pretty的给我,但我很困惑在这里说,如果这是可能以这种方式那么为什么大多数的书/教师的推移,早期(的SqlCommand的方式)。

Looks short and pretty to me, But I am confused here that if this is possible in this way then why most of the books/Teachers goes by earlier (SqlCommand’s way).

什么是真正的SqlCommand和SqlDataAdapter的?的区别 在哪种方法更好的一个还是两个? 怕我使用的方法两个快捷方式可能影响安全或性能明智?

提前Apologising如果我听起来非常新手或模糊!请问AP preciate任何帮助,可以清除我的概念了!谢谢! :)

Apologising in advance if I sound very newbie or blurred! Will appreciate any help that could clear my concepts up! Thankyou! :)

推荐答案

Errorstacks总结是正确的:

Errorstacks summed it right:

SqlAdapter用于填充的数据集。 的SqlCommand可用于任何目的,你心里有创建/读取/更新/删除操作相关,存储过程的执行等等。

另外:

的SqlCommand可以对原始字符串在安全性方面的用途一大好处 - 他们可以保护你免受SQL注入攻击。只使用参数用于由用户代替的String.Format(...)提供的值。

我个人的preference是包装在SqlCommand的任何SQL字符串,并添加SqlParameters它,以避免SQL注入受到恶意用户。 关于这两种方法的性能 - 我不认为有任何区别。 (如果有人能证明我错了 - 做到这一点!)。 所以我建议如果有必要坚持更长的变体1和使用的命令加参数。

My personal preference is to wrap ANY sql strings in SqlCommand and add SqlParameters to it in order to avoid Sql Injection by malicious users. Regarding performance of the two approaches - I don't expect that there is any difference. (If someone can prove me wrong - do it!). So I would suggest to stick with the longer variant 1 and use commands plus parameters if necessary.

补充说明的一点 - 数据集和数据表已经超出了游戏最近由于LINQ2SQL和实体框架 当然,但是普通的老SqlCommands /适配器/读者的知识是欢迎的:)

A bit of a side note - Datasets and DataTables are a bit out of game recently due to Linq2Sql and Entity Framework. But of course the knowledge of plain old SqlCommands/Adapters/Readers is welcome :)