请帮讲几句微软msdn上的示例代码是什么意思全部代码如下,我就是其中的 updatecommand 代码看不明白。public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); // Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; // Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection); // Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; // Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter;
}
以下几句我看不明白 // Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command;1.为什么参数名字前要加上 @ 符号呢?不加行吗?是必须加上呢?还是用别的名称也可以。
2.@CustomerID和@oldCustomerID这两个参数都是对应着 CustomerID字段呀,为何建两个参数呢?
3.parameter.SourceVersion = DataRowVersion.Original;这一句是什么意思呀?
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter(); // Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection); // Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; // Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection); // Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; // Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter;
}
以下几句我看不明白 // Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command;1.为什么参数名字前要加上 @ 符号呢?不加行吗?是必须加上呢?还是用别的名称也可以。
2.@CustomerID和@oldCustomerID这两个参数都是对应着 CustomerID字段呀,为何建两个参数呢?
3.parameter.SourceVersion = DataRowVersion.Original;这一句是什么意思呀?
@XXX 表示 XXX这个数据 可以是数据库里本身计有的数据,也可以是外部传入数据库的数据
没有@ 表示 XXX这个数据只是数据库记载的数据 外部传入数据库的数据无法读出
@aaaa
与
aaaa可以理解成,@aaaa是 datatable中已经更新过的值,aaaa是数据库真实表中的值另外,必须以 @ 符号打头吗?用别的名称可以吗?
这是规定吗?
但在赋值时可以不加,比如:command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");这个地方可以不加2.@CustomerID和@oldCustomerID声明两个是它们被赋了不同的值
在这条更新语句中@oldCustomerID是当条件找到这条表中的数据,
而@CustomerID是给这条数据的CustomerID字段赋的新值
就是更新这条CustomerID为@oldCustomerID的数据的CustomerID字段为@CustomerID;3.可以看看http://msdn.microsoft.com/zh-cn/library/system.data.datarowversion(VS.80).aspx
印象中在vs05中@符是必须加,在08中在sql语句中声明这个变量时也必须加@符号
但在赋值时可以不加,比如:command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");这个地方可以不加
2.@CustomerID和@oldCustomerID这两个参数都是对应着 CustomerID字段呀,为何建两个参数呢?
两个值不一样一个是查询条件一个是设置新值,所以两个参数值不一样,如果值一样用一个都成.3.parameter.SourceVersion = DataRowVersion.Original;这一句是什么意思呀?
可以看看http://msdn.microsoft.com/zh-cn/library/system.data.datarowversion(VS.80).aspx
A:是的 这是要传参的形式 所以都要有@符号的.2.@CustomerID和@oldCustomerID这两个参数都是对应着 CustomerID字段呀,为何建两个参数呢?
A:声明对象的不同是为了赋予不同的值的.3.parameter.SourceVersion = DataRowVersion.Original;这一句是什么意思呀?
A:版本控制的问题,可以去Google看下具体实例 也可以像楼上2位说的去MSDN上看看
Original 该行中包含其原始值
在调用 DataRow 对象的 AcceptChanges 方法之后,Original 值变得与 Current 值相同。
在调用 DataTable 对象的 AcceptChanges 方法之后,Original 值变得与 Current 值相同
如果使用 SqlDataAdapter 对象,将使用命名参数,如下所示:
Select * From Customers Where City = @City