在我的mysql表中我看起来像这个
function configureDropDownLists(category, products) {
//var appliances = {
//refrigerators: [ 'Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS'],
//dishWasher: ['Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana']
// }
var refrigerators = new Array('Artic King AEB', 'Artic King ATMA', 'Avanti Compact', 'Bosch SS');
var dishWasher = new Array('Bosch - SHXUC', 'Asko DS', 'Blomberg', 'Amana');
switch (category.value) {
case 'refrigerators':
products.options.length = 0;
for (i = 0; i < refrigerators.length; i++) {
createOption(products, refrigerators[i], refrigerators[i]);
}
break;
case 'dishWasher':
products.options.length = 0;
for (i = 0; i < dishWasher.length; i++) {
createOption(products, dishWasher[i], dishWasher[i]);
}
break;
default:
products.options.length = 0;
break;
}
}
function createOption(ddl, text, value) {
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
ddl.options.add(opt);
}
/*
Added on MAY 6th 1:29 PM
Store and display Wattage based on the product selected
1) Define wattage array
2) get wattage array
3) Check for which product is selected and accordingly display the Wattage. This has to happen onChange of the products dropdown.
// define wattage array
var wattageArray = localStorage {
Artic King AEB: 270,
Artic King ATMA: 311,
Avanti Compact:340,
Bosch SS: 214,
Bosch - SHXUC: 200,
Asko DS: 187,
Blomberg: 236,
Amana: 150
};
function getWattageArray() {
var wattageArray = getWattageArray();
// loop and check for key, value
for (var i = 0; i < wattageArray.length; i++) {
var key = wattageArray[i];
var value = wattageArray[i];
var wattageArray = localStorage.getItem("wattageArray");
// check for products drop down and compare it to wattage
for (options.selectedIndex == wattageArray) {
// display using innerHTML. Parse it since it is an integer
var value = parseInt(localStorage.getItem[i]));
document.getElementById("wattage").innerHTML = value;
}
}
SIMPLE SET and DISPLAY TO SEE IF LOCAL STORAGE WORKS
//localStorage.setItem("Artic King AEB",270);
//localStorage.setItem("Artic King ATMA",311);
//localStorage.setItem("Avanti Compact",340);
//localStorage.setItem("Bosch SS",214);
//localStorage.setItem("Bosch - SHXUC",200);
//localStorage.setItem("Asko DS",187);
//localStorage.setItem("Blomberg",236);
//localStorage.setItem("Amana",150);
//var sticky = parseInt(localStorage.getItem("Blomberg"));
//alert(sticky);
function estimatedDailyUse(dailyUse){
var button = document.getElementsByTagName("button");
console.log(button);
if(button.onclick.id =="h1"){
return dailyUse = 1;
alert("You clicked one");
console.log("hi");
}
else if (button.onclick.id =="h3"){
return dailyUse = 3;
}
else if (button.onclick.id =="h6"){
return dailyUse = 6;
}
else if (button.onclick.id =="h24"){
return dailyUse = 24;
}
}
/*
function selectOption(ddl,text,value){
document.getElementById("product").onchange = function() {
this.selectedIndex ==selected;
}
}
if(category.value == selected){
}
if so and so option is selected in Category
make that option as selected
populate products
if so and so option is selected by user in products
make that option as selected
display wattage
}
$(function() {
var categoryValue = localStorage.getItem("categoryValue");
if(categoryValue != null) {
$("select[name=ddl]").val(categoryValue);
}
$("select[name=ddl]").on("change", function() {
localStorage.setItem("categoryValue", $(this).val());
});
})
*/
/*
How to store and display Wattage based on the product selected
1) Define wattage array
2) get wattage array
3) Check for which product is selected and accordingly display the Wattage. This has to happen onChange of the products dropdown.
// define wattage array
var wattageArray = localStorage {
Artic King AEB: 270,
Artic King ATMA: 311,
Avanti Compact:340,
Bosch SS: 214,
Bosch - SHXUC: 200,
Asko DS: 187,
Blomberg: 236,
Amana: 150
};
// call wattage array inside init
window.onload = init;
function init() {
//options.onchange = displayWattage;
var wattageArray = getWattageArray();
for (var i = 0; i < wattageArray.length; i++) {
var key = wattageArray[i];
var value = wattageArray[i];
}
}
function getWattageArray() {
var wattageArray = localStorage.getItem("wattageArray");
for (options.selectedIndex == wattageArray) {
var value = parseInt(localStorage.getItem[i]));
document.getElementById("wattage").innerHTML = value;
}
}
SIMPLE SET and DISPLAY TO SEE IF LOCAL STORAGE WORKS
//localStorage.setItem("Artic King AEB",270);
//localStorage.setItem("Artic King ATMA",311);
//localStorage.setItem("Avanti Compact",340);
//localStorage.setItem("Bosch SS",214);
//localStorage.setItem("Bosch - SHXUC",200);
//localStorage.setItem("Asko DS",187);
//localStorage.setItem("Blomberg",236);
//localStorage.setItem("Amana",150);
//var sticky = parseInt(localStorage.getItem("Blomberg"));
//alert(sticky);
*/
现在我在C#中创建了一个用户控件,允许用户更新除主要&#34; ControlNum&#34;之外的所有列。因此,假设每个列在所述用户控件中都有自己的文本框,以避免在标题列中出现重复,例如,如果用户编辑第2行标题并将其放入&#34; naruto&#34;它也会重复。所以我创建了一个名为checkData()的方法;
ControlNum|Title |NumOfEpis|Description|
001 |naruto |500 |some text |
002 |conan |700 |some text |
它工作正常,但我的问题是如果用户只更新epis和描述的数量并且并不打算更新标题,我的方法仍然会检测到有重复因为我的逻辑是&#34;如果(ctr == 1)&#34; ...我想我错过了一些方法,或者我在这里采取了错误的方法......所以我希望有人会启发我......在这里接受noob YY
答案 0 :(得分:2)
您不希望两个标题具有相同的内容。这可以通过该列上的UNIQUE索引自动实现。但是,如果您不想为此添加索引,则可以将查询更改为
SELECT ControlNum FROM maindatabase.animelist
WHERE TitleAnime=?Title;
然后你的逻辑应该检查ControlNum的差异
int currentNum = int.Parse(a.ToString()));
while (reader.Read())
{
int ctrlNum = reader.GetInt32(0);
if(ctrlNum != currentNum)
ctr++;
}
if (ctr > 0)
......
这样,只有当检索到的ControlNum与您在界面中选择的ControlNum不同时,才会增加计数器
另一种方法如下(见@mbeckish下面的评论)
SELECT 1 FROM maindatabase.animelist
WHERE TitleAnime=?Title AND ControlNum <> ?CN
command.Parameters.AddWithValue("?CN", int.Parse(a.ToString()));
command.Parameters.AddWithValue("?Title", textBox3.Text);
object result = command.ExecuteScalar();
if(result != null)
{
// Found a record with different ControlNum but with the same title
// Error here
}
这可能比第一个更好,因为你不需要循环并且可以使用更简单且性能更高的ExecuteScalar,它只返回第一行的第一列(在本例中为1)而不构建MySqlDataReader对象
答案 1 :(得分:2)
如果您的应用程序支持多个用户,则需要确保在检查重复项和数据库更新之间,其他用户不会进行更改。
最简单的方法是建议mbeckish,在标题列上创建一个UNIQUE约束:
ALTER TABLE maindatabase.animelist
ADD CONSTRAINT U_animelist_TitleAnime UNIQUE (TitleAnime)
然后,数据库引擎将强制执行唯一标题,并且您的客户端可以通过捕获任何约束违例异常来处理用户反馈:
void checkData()
{
SuspendLayout();
try
{
updateData();
}
catch (Exception ex)
{
MySqlException sqlEx = ex as MySqlExecption;
// If there is a constraint violation error.
// (I may have the wrong error number, please test.)
if (sqlEx != null && sqlEx.Number == 1062)
{
my = Form.ActiveForm as MyList;
my.msg = new Message_Box();
my.msg.Descrip.Text = "Record is already in the Database";
my.msg.Title.Text = "Duplicate Record";
my.msg.ShowDialog();
}
else
{
MessageBox.Show("" + ex);
}
}
finally
{
ResumeLayout();
}
}
答案 2 :(得分:0)
更改重复的查找SQL:
SELECT MAX(Title) as Count
FROM maindatabase.animelist
GROUP BY Title , NumOfEpis, Description
如果计数&gt; 1,然后有一个副本。