2013-11-02 33 views
0

我正在轉換一個網上商店系統到另一個系統與C#。這是從DB到DB之間的一些轉換。C#雙入MySql數據庫雙重不工作

我有這樣的代碼在C#:

Double proPriceTmp = Double.Parse(productPrice); 
Double realPrice = ((proPriceTmp/121) * 100); 

string insertProduct = "INSERT INTO `" + ocPrefix + "product` (`product_id`, `model`, `quantity`, `stock_status_id`, `image`, `manufacturer_id`, `shipping`, `price`, `tax_class_id`,`date_available`, `weight`, `weight_class_id`, `length`, `width`, `height`,`length_class_id`,`subtract`,`minimum`,`sort_order`,`status`,`date_added`,`date_modified`,`viewed`) VALUES ('" + product_id + "', @param_model, '" + product_quantity + "', '" + stock_status_id + "', '" + image + "', '" + manufacturer_id + "', '" + shipping + "', '" + realPrice + "', '" + numericUpDown1.Value + "', '" + date_available + "', '" + product_weight + "', '" + weigth_class_id + "', '" + product_length + "', '" + product_width + "', '" + product_height + "', '" + length_class_id + "', '" + substracting + "', '" + minimum + "', '" + sort_order + "', '" + status + "', '" + product_created + "', '" + product_modified + "', '" + viewed + "')"; 
MySqlCommand cmdInsertProduct = new MySqlCommand(insertProduct, connOc); 
cmdInsertProduct.CommandText = insertProduct; 
cmdInsertProduct.Parameters.AddWithValue("@param_model", product_name); 
cmdInsertProduct.ExecuteNonQuery(); 
cmdInsertProduct.Dispose(); 

這是價格變量(調試器感謝)值:

productPrice = "1,20000"; 
proPriceTmp = 1.2; 
realPrice = 0.99173553719008256; 

但在DB價格0.0000。我已經嘗試過手工插入信息,而且工作(用這麼長的數字),所以這應該工作。

表看起來是這樣的:

CREATE TABLE `oc_product` (
    `product_id` int(11) NOT NULL AUTO_INCREMENT, 
    `model` varchar(64) NOT NULL, 
    `sku` varchar(64) NOT NULL, 
    `upc` varchar(12) NOT NULL, 
    `ean` varchar(14) NOT NULL, 
    `jan` varchar(13) NOT NULL, 
    `isbn` varchar(13) NOT NULL, 
    `mpn` varchar(64) NOT NULL, 
    `location` varchar(128) NOT NULL, 
    `quantity` int(4) NOT NULL DEFAULT '0', 
    `stock_status_id` int(11) NOT NULL, 
    `image` varchar(255) DEFAULT NULL, 
    `manufacturer_id` int(11) NOT NULL, 
    `shipping` tinyint(1) NOT NULL DEFAULT '1', 
    `price` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `points` int(8) NOT NULL DEFAULT '0', 
    `tax_class_id` int(11) NOT NULL, 
    `date_available` date NOT NULL, 
    `weight` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `weight_class_id` int(11) NOT NULL DEFAULT '0', 
    `length` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `width` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `height` decimal(15,8) NOT NULL DEFAULT '0.00000000', 
    `length_class_id` int(11) NOT NULL DEFAULT '0', 
    `subtract` tinyint(1) NOT NULL DEFAULT '1', 
    `minimum` int(11) NOT NULL DEFAULT '1', 
    `sort_order` int(11) NOT NULL DEFAULT '0', 
    `status` tinyint(1) NOT NULL DEFAULT '0', 
    `date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `viewed` int(5) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`product_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=utf8; 

因此,沒有任何人知道這是爲什麼不工作像它應該做的?即使是在朝着正確的方向輕輕一推,就足夠了...

編輯

至於質疑,這裏是insertProduct值。正如你所看到的,是價格那裏的權利...

"INSERT INTO `oc_product` (`product_id`, `model`, `quantity`, `stock_status_id`, `image`, `manufacturer_id`, `shipping`, `price`, `tax_class_id`,`date_available`, `weight`, `weight_class_id`, `length`, `width`, `height`,`length_class_id`,`subtract`,`minimum`,`sort_order`,`status`,`date_added`,`date_modified`,`viewed`) VALUES ('51', @param_model, '999999999', '7', 'data/old/letter_trein.jpg', '0', '1', '0,991735537190083', '11', '2013-06-09', '600,000', '1', '0,000', '0,000', '0,000', '1', '0', '0', '0', '1', '2013-01-09 12:26:02', '2013-10-24 10:23:47', '0')" 
+1

哇,這一切都沒有使用參數化查詢? – Steve

+0

'insertProduct'的值是什麼? –

+0

您的意思是插入成功,沒有錯誤,但只有價格字段保持0? – LINQ2Vodka

回答

2

在您的查詢中,你把每一個值,因爲他們是一個字符串。實際上,當你需要編寫一個數字字段時,你不會在值的周圍使用單引號。例如,插入product_id字段的值時,不應使用引號。如果使用參數化查詢,所有這些混亂都可以避免。

這將使工作將您的值解釋爲框架代碼,並且您不必擔心如何在字符串內正確表示它們(更不用說整個Sql Injection問題了),並且您的字符串將是更具可讀性。

您的查詢中已經有一個參數,將其擴展爲EVERY值。
但請注意數值。例如'product_id'變量應該是一個數字而不是一個字符串。

string insertProduct = "INSERT INTO `" + ocPrefix + "product` (`product_id`, `model`, " + 
         "`quantity`, `stock_status_id`, `image`, `manufacturer_id`, " + 
         "`shipping`, `price`, `tax_class_id`,`date_available`, `weight`, " + 
         "`weight_class_id`, `length`, `width`, " + 
         "`height`,`length_class_id`,`subtract`,`minimum`,`sort_order`," + 
         "`status`,`date_added`,`date_modified`,`viewed`) " + 
         "VALUES (@pid, @param_model, @qty, @stk, @img, @mid, @ship, " + 
         "@price, @num, @date', @weight,@classid, @length, @width," + 
         "@height,@lenid,@sub,@min, @sort,@status,@created,@modified,@view)"; 
MySqlCommand cmdInsertProduct = new MySqlCommand(insertProduct, connOc); 
cmdInsertProduct.Parameters.AddWithValue("@pid", product_id); 
cmdInsertProduct.Parameters.AddWithValue("@param_model", product_name); 
...... 

在這一點之後,我想你可能會有NOT NULL字段引起的其他問題沒有DEFAULT表達。我認爲你應該將它們添加到你的查詢中

+0

我已經在其他地方使用。但我會在這裏嘗試一下... – Mathlight

+0

謝謝,那就是訣竅。但爲什麼在那之前它沒有工作? – Mathlight

+0

@Mathlight小數點分隔符? :) – LINQ2Vodka

2

它看起來像你在歐洲(?)語言環境中運行,它使用逗號,作爲小數點分隔符 - 在SQL字符串中,你有'0,991735537190083'而不是'0.991735537190083'。現在,您可能會更改爲在您的realPrice上使用ToString()的重載來指定要使用的語言環境,或者您可以更改爲參數化整個查詢,這也會防範SQL注入攻擊。

+0

啊哈,我確實是歐洲人。而作爲史蒂夫mentiod,查詢的參數確實是一個竅門。感謝您的解釋:D – Mathlight