PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   七嘴八舌異言堂 (https://www.pcdvd.com.tw/forumdisplay.php?f=12)
-   -   請c#高手幫忙一下@@ 想一天了!! 無解 (https://www.pcdvd.com.tw/showthread.php?t=946553)

企Gre 2011-10-23 10:03 PM

請c#高手幫忙一下@@ 想一天了!! 無解
 
在2010 VC# 底下
==============================================
mysql mydb
[coid (key)] [cowhere] [comoney] [coincome] [comons] [coday conote]

==============================================
private void button3_Click(object sender, EventArgs e)
{
int coid = Int32.Parse(nmbBox2.Text);
string cowhere = wh2Box4.Text;
string comoney = moneyBox5.Text;
string conote = noteBox6.Text;
string connStr = "server=localhost;user=root;database=mydb;port=3306;password=;Charset=utf8";
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "updata expenses set cowhere=@cowhere,comoney=@comoney , conote=@conote where coid=@coid ";
cmd.Prepare();
cmd.Parameters.AddWithValue("@cowhere", cowhere);
cmd.Parameters.AddWithValue("@comoney", comoney);
cmd.Parameters.AddWithValue("@conote", conote);
cmd.ExecuteNonQuery();
每次都會說錯誤!! 實在無解!!
conn.Close();
button1_Click(this, e);

roger214 2011-10-23 10:09 PM

引用:
作者企Gre
cmd.CommandText = "updata expenses set cowhere=@cowhere,comoney=@comoney , conote=@conote where coid=@coid ";
...


UPDATE 不是 UPDATA ,另求救請將錯誤訊息列出來。

寫程式,最好學會 DEBUG ,別光靠看程式碼抓蟲。

企Gre 2011-10-23 10:14 PM

roger214大大 :like:
原來我是豬頭~update一直沒發現= ="
後來我改成update了!編譯成功! 按下button ~還是會跑出錯誤~~

Mysql.Data.MysqlClient.MysqlException
{"Fatal error encountered during command execution."}
:confused:

企Gre 2011-10-23 10:19 PM

我也希望自己做DeBug 的if!!
但是 功力不強!不知道 cmd.ExecuteNonQuery(); 到底要怎麼下手!!
:nonono:

if (cmd.ExecuteNonQuery();)
Console.WriteLine("\t{0}\t{1}", rdr.GetInt32(0), rdr.GetString(1));
else
Console.WriteLine("No rows returned.");
....囧!!!

roger214 2011-10-23 10:20 PM

引用:
作者企Gre
roger214大大 :like:
原來我是豬頭~update一直沒發現= ="
後來我改成update了!編譯成功! 按下button ~還是會跑出錯誤~~

Mysql.Data.MysqlClient.MysqlException
{"Fatal error encountered during command execution."}
:confused:


嗯 UPDATE 語法去查一下,你 SQL 應該很不熟悉吧?你試試 SET 之後加個 WHERE 1 看看。

另外,cowhere、comoney、conote 必須至少有一筆資料存在,只建好 schema 是不夠的,update 必須在資料表中已有資料,才能進行更新。

Jens Rydén 2011-10-23 10:20 PM

你先確定SQL字串在sql express查詢確定是Ok的後

再設中斷點,執行觸發後,按F10或F11單步debug,

這很簡單吧...連vs 2010好用的debug都不會就完了 :jolin:

還有,你要先確定你的connection到底有沒有成功... :agree: 用try catch來抓就知道了

code如果看不出來哪裡有問題,只好開始慢慢debug... :nonono:

darkangel 2011-10-23 10:21 PM

你有幫 mysql 的 root 設 password 嗎?

roger214 2011-10-23 10:22 PM

引用:
作者企Gre
我也希望自己做DeBug 的if!!
但是 功力不強!不知道 cmd.ExecuteNonQuery(); 到底要怎麼下手!!
:nonono:

if (cmd.ExecuteNonQuery();)
Console.WriteLine("\t{0}\t{1}", rdr.GetInt32(0), rdr.GetString(1));
else
Console.WriteLine("No rows returned.");
....囧!!!


Virtual Studio 就有除錯器了,你可以在執行之前設好中斷點,執行後從中斷點單步執行,觀察你要檢查的程式碼以及變數。

holmes2010 2011-10-23 10:23 PM

引用:
作者企Gre
roger214大大 :like:
原來我是豬頭~update一直沒發現= ="
後來我改成update了!編譯成功! 按下button ~還是會跑出錯誤~~

Mysql.Data.MysqlClient.MysqlException
{"Fatal error encountered during command execution."}
:confused:


 「coid」參數值忘了放進去吧?

企Gre 2011-10-23 10:25 PM

引用:
作者darkangel
你有幫 mysql 的 root 設 password 嗎?

darkangel 沒有耶@@

因為我實作insert的button 沒設密碼也可以pass ^^
所以就沒設mysql密碼了@@


所有的時間均為GMT +8。 現在的時間是03:55 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2026。