数据适配器VS SQL命令 [英] Data Adapter Vs Sql Command

查看:148
本文介绍了数据适配器VS SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个将是执行为MS-SQL数据库的INSERT语句更好的:


  

的Sql DataAdapter的或SQL命令
  对象?


它们哪会比较好,而插入只有一行键,而插入多行

code使用的一个简单的例子:

SQL命令

 查询字符串=插入到表1(COL1,COL2,COL3)值(@值1,@值2,@ VALUE3);
INT I;CMD的SqlCommand =新的SqlCommand(查询,连接);
//添加参数...
cmd.Parameters.Add(@值1,SqlDbType.VarChar).value的= txtBox1.Text;
cmd.Parameters.Add(@ VALUE2,SqlDbType.VarChar).value的= txtBox2.Text;
cmd.Parameters.Add(@ VALUE3,SqlDbType.VarChar).value的= txtBox3.Text;
cmd.con.open();
I = cmd.ExecuteNonQuery();
cmd.con.close();

SQL数据适配器

 的DataRow博士= dsTab.Tables [表1] NEWROW()。
数据集dsTab =新的DataSet(表1);
SqlDataAdapter的ADP =新的SqlDataAdapter(SELECT * FROM表1,连接);
adp.Fill(dsTab,表1);
博士[COL1] = txtBox1.Text;
博士[COL2] = txtBox5.Text;
博士[COL3] =文本;dsTab.Tables [表1] Rows.Add(DR)。SqlCommandBuilder ProjectBuilder的=新SqlCommandBuilder(ADP);
数据集newSet = dsTab.GetChanges(DataRowState.Added);
adp.Update(newSet,表1);


解决方案

更​​新数据源更容易使用DataAdapters。它更容易做出改变,因为你只需要修改DataSet,并调用Update。

有可能是在使用DataAdapters VS命令之间的性能没有(或很少)的区别。 DataAdapters内​​部使用的连接和Command对象和执行命令来执行你告诉他们做的动作(如填充和更新),因此它的pretty一样一样只使用Command对象。

Which one would be better in executing an insert statement for ms-sql database:

Sql DataAdapter or SQL Command Object?

Which of them would be better, while inserting only one row and while inserting multiple rows?

A simple example of code usage:

SQL Command

string query = "insert into Table1(col1,col2,col3) values (@value1,@value2,@value3)";
int i;

SqlCommand cmd = new SqlCommand(query, connection);
// add parameters...
cmd.Parameters.Add("@value1",SqlDbType.VarChar).Value=txtBox1.Text;
cmd.Parameters.Add("@value2",SqlDbType.VarChar).Value=txtBox2.Text;
cmd.Parameters.Add("@value3",SqlDbType.VarChar).Value=txtBox3.Text;
cmd.con.open();
i = cmd.ExecuteNonQuery();
cmd.con.close();

SQL Data Adapter

DataRow dr = dsTab.Tables["Table1"].NewRow();
DataSet dsTab = new DataSet("Table1");
SqlDataAdapter adp = new SqlDataAdapter("Select * from Table1", connection);
adp.Fill(dsTab, "Table1");
dr["col1"] = txtBox1.Text;
dr["col2"] = txtBox5.Text;    
dr["col3"] = "text";

dsTab.Tables["Table1"].Rows.Add(dr);

SqlCommandBuilder projectBuilder = new SqlCommandBuilder(adp);
DataSet newSet = dsTab.GetChanges(DataRowState.Added);
adp.Update(newSet, "Table1");

解决方案

Updating a data source is much easier using DataAdapters. It's easier to make changes since you just have to modify the DataSet and call Update.

There is probably no (or very little) difference in the performance between using DataAdapters vs Commands. DataAdapters internally use Connection and Command objects and execute the Commands to perform the actions (such as Fill and Update) that you tell them to do, so it's pretty much the same as using only Command objects.

这篇关于数据适配器VS SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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