2013-10-08 27 views
-1

當我運行我的應用程序時,它會一直到最後,但是當我檢查我的數據庫後,它從不顯示任何數據。這裏是我的代碼:SQL CE不向數據庫輸入信息

private void button1_Click(object sender, EventArgs e) 
    { 
     string saltedcryps = saltpassword(10); 
     string passWithSalt = (textBox1.Text + saltedcryps); 
     string hashedResult = hashPassAndSalt(passWithSalt); 
     if (checkPasswordsMatch() == "B") 
     { 
      SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf"); 
        try 
        { 
         myConnection.Open(); 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.ToString()); 
        } 
        SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');", myConnection); 
        myCommand.ExecuteNonQuery(); 
        myConnection.Close(); 
        this.Hide(); 

     } 
    } 
    private string checkPasswordsMatch() 
    { 
     if (textBox1.Text == "") 
     { 
      MessageBox.Show("Passwords cannot be empty"); 
      return "A"; 
     } 
     else 
     { 
      MessageBox.Show(textBox1.Text == textBox2.Text ? "Thanks for registering!" : "Your passwords do not match"); 
      return "B"; 
     } 
    } 
    private string saltpassword(int size) 
    { 
     RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider(); 
     byte[] buff = new byte[size]; 
     crypto.GetBytes(buff); 
     return Convert.ToBase64String(buff); 
    } 
    private string hashPassAndSalt(string passWithSalt) 
    { 
     HashAlgorithm hashAlg = new SHA256CryptoServiceProvider(); 
     byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(passWithSalt); 
     byte[] bytHash = hashAlg.ComputeHash(bytValue); 
     string base64 = Convert.ToBase64String(bytHash); 
     return base64; 
    } 
} 

這是Button1_Click的,問題在於當它運行myCommand.ExecuteNonQuery()。它從來沒有拋出異常,它只是繼續,沒有實際輸入任何信息...

任何人都有線索?

+0

你爲什麼只在你的'try'塊中打開你的連接?如果你移動'SqlCeCommand myCommand = new SqlCeCommand(「INSERT INTO PW Values('Master','」+ saltedcryps +'','「+ hashedResult +」');「,myConnection); myCommand.ExecuteNonQuery(); myConnection.Close();隱藏();'到你的'try'塊中? – Brian

+0

仍然無法正常工作。 try塊試圖打開連接,如果成功則通過catch塊,然後運行下一個代碼。它基本上是相同的東西 –

+0

在您的bindebug文件夾中查找datbase文件的副本,最好的方法是在連接字符串 – ErikEJ

回答

0

試試這個:

if (checkPasswordsMatch() == "B") 
     { 
      SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf"); 
        try 
        { 
         myConnection.Open(); 
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.ToString()); 
        } 
        SqlCeCommand myCommand = myConnection.CreateCommand(); 
        myCommand.CommandType = CommandType.Text; 
myCommand.CommandText = "INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');" 
        myCommand.ExecuteNonQuery(); 
        myConnection.Close(); 
        this.Hide(); 

     } 

如果這並不工作,儘量把絕對路徑的連接字符串。

+0

新的myConnection.CreateCommand()創建了一條紅線,無法找到類型或命名空間myConnection。和'「+ hashedResult +」');創建「常量中的換行符」 –

+0

,可以工作,但是當我部署應用程序時呢?數據庫不會在客戶端計算機上的相同文件夾中? –

+0

您是否試過這個?:SqlCeConnection myConnection = new SqlCeConnection(「Data Source = /pwdb.sdf」); – Diego

0

該連接字符串看起來不正確;除非你做的事情非常奇怪,你應該使用Data Source=|DataDirectory|pwDB.sdf

爲什麼你認爲你的數據庫「從不顯示任何數據」?你在哪裏查找數據?在項目目錄中的原始源數據中?這幾乎肯定是錯誤的;當你部署你的應用程序時,你是不是在部署你的源代碼?您需要查看部署文件夾,請參閱this answer

+0

。我在項目中做到了? –

+0

這很好,@ tony,但是當你部署應用程序時你沒有部署項目,對嗎?請提供更多細節,因爲我不明白你在做什麼。 –

+0

我正在做一個安全地存儲密碼的應用程序。我提供的這個代碼將密碼註冊到數據庫。因此,當我將軟件發送到客戶端時,如何將數據庫與應用程序一起發送,因爲他們機器中的安裝文件夾與我的計算機上的文件夾不同,所以我如何引用? –

0

把try catch放在ExecuteNonQuery的周圍,這樣你就可以看到什麼是異常,如果存在,否則一定是你的連接字符串(DataSource = pwDB.sdf does not look right to)必須有用戶ID;密碼; dataSource =你的IP和初始目錄:

​​
+0

它不是一個服務器或類似的東西,它是在項目 –

+0

創建那麼,是數據庫呢?如果它只會在用戶計算機上脫機運行,那麼你必須有一個腳本在他們的計算機上創建以及爲了讓他們訪問它 – cvetyab

+0

我以爲你在應用程序中創建了數據庫,然後它將該應用程序複製該數據庫並使用該數據庫? –