日期:2014-05-18  浏览次数:20862 次

100分 关于更新问题
一个物品的价格输错了   现在要修改     下面是具体问题
这个物品的价格在2个表中   其中表1内有多条价格关于这个物品  
表2内物品的价格是表1中这个物品最高价   现在要改表1中一条这个物品的价格
刚好这个价格是最高价   而修改后的价格不是最高价    
我要的是关于价格修改后比较出最高价的循环语句怎么写  
就是说我要循环语句的代码   请具体点   最好有注释
可用的代码分数不会少于60

------解决方案--------------------
up
------解决方案--------------------
主外键 SQL


------解决方案--------------------
最高价为什么还要存一个表呢?直接Max(price)不就出来了
如果想更新表2的最高价可以用Trigger
------解决方案--------------------
首先给你一个方法:
/// <summary>
/// 访问数据库,从数据库中获取数据表。
/// </summary>
/// <param name= "selectCommand "> 参数为查询语句。 </param>
/// <returns> 以数据表的格式返回查询数据。 </returns>
public DataTable GetData(string selectCommand)
{
try
{
// Specify a connection string.
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; "+ "Data Source=myData.mdb ";//数据库名

connection = new OleDbConnection(connectionString);

// Create a new data adapter based on the specified query.
oledbda = new OleDbDataAdapter(selectCommand, connection);

// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
oledbcb = new OleDbCommandBuilder(oledbda);

// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
oledbda.Fill(table);
bindingSource1.DataSource = table;

return (table);
}
catch (OleDbException)
{
MessageBox.Show( "数据库连接失败! ");
return null;
}
}

然后再程序中这样写:
//从表一中取出价格
string strSelect= "Select 价格 from 表1 where 物品=... ";
DataTable myTable=GetData(strSelect);
//循环比较
int highest=0;
foreach(DataCell d in myTable.Row[0].Cells)
{
if(Convert.toInt32(d)> highest)
highest=Convert.toIn32(d);
}
最后得出的highest即是最高价格。
------解决方案--------------------
触发器,当每次更新表1的时候,取得更新的价格和物品代码,然后查询表2中该物品价格,跟插入或更新的价格比较,如果较大,则更新表2

Create Trigger UpdateHighestPrice On 表1
For insert,update
AS
delcare @price decimal(15,4), @itemcode varchar(20)
select @price=price, @itemcode=ItemCode from inserted -- 选取所插入/更新的物品数据
-- 更新表2,如果插入价格不是最高价格就不更新
Update 表2
set Price=@price
where ItemCode=@itemcode AND Price <@price
------解决方案--------------------
修改后
select max(价格) from 表1 where 物品=...

这个不久是最高价吗?干吗还要循环呢?