c#
  • ms-access
  • 2013-07-06 48 views 0 likes 
    0

    在MS-Access中使用時,該代碼運行和性能更新,但通過使用數據庫時,它給語法錯誤C#訪問數據庫錯誤

    string item = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); 
    
    string h="update Follow_Date set Current_Date='" + dateTimePicker1.Value.ToLongDateString() + "', Current_Time='" + dateTimePicker3.Value.ToLongTimeString() + "', Type='" + 
              comboBox1.SelectedItem.ToString() + "', Remarks='" + 
              textBox1.Text + "', Next_Follow_Date='" + dateTimePicker2.Value.ToLongDateString()+ "' where Follow_Id='" + 
              item.ToString() +"'"; 
    
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\lernovo\Documents\JDB.mdb"); 
    
    con.Open(); 
    
    OleDbCommand cmd = new OleDbCommand(h, con); 
    cmd.ExecuteNonQuery(); 
    

    錯誤是syntax error

    +1

    嘗試使用參數化查詢。 – Praveen

    +1

    我強烈建議你從創建SQL查詢時使用字符串連接的方式移開。這是一個等待發生的sql注入災難 – TGH

    +0

    什麼語法錯誤?錯誤行說什麼? – Rahul

    回答

    0

    貌似微不足道的錯誤...

    您必須打開它在執行查詢之前的數據庫連接..

    conn=new conn(db param); 
    try 
    { 
        conn.open() 
    } 
    catch(Exception e) 
    { 
        e.getMessage(); 
    } 
    
    +1

    不下調,但他已經打開連接。正確地查看問題。 – Rahul

    +0

    無法找到錯誤,但 – user2555677

    +0

    先生它writtent con.Open() – user2555677

    3
    string item = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); 
    
    string h="update Follow_Date set @Current_Date, @Current_Time, @Type, @Remarks, @Next_Follow_Date where @Follow_Id"; 
    
    try 
    { 
    Using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\lernovo\Documents\JDB.mdb")) 
    { 
        con.Open(); 
    
        Using (OleDbCommand cmd = new OleDbCommand(h, con)) 
        {  
        cmd.Parameters.Add("Current_Date", dateTimePicker1.Value.ToLongDateString()); 
        cmd.Parameters.Add("Current_Time", dateTimePicker3.Value.ToLongTimeString()); 
        cmd.Parameters.Add("Remarks", textBox1.Text); 
        cmd.Parameters.Add("Type", comboBox1.SelectedItem.ToString()); 
        cmd.Parameters.Add("Next_Follow_Date", dateTimePicker2.Value.ToLongDateString()); 
        cmd.Parameters.Add("Follow_Id", item.ToString()); 
        cmd.ExecuteNonQuery(); 
        } 
    } 
    } 
    catch(SQLException ex) 
    { 
    System.Console.WriteLine(ex.Message, ex.StackaTrace) 
    } 
    

    你不關閉數據庫連接和嘗試使用Parameter而不是串聯(Probe to SQL Injection)。

    抓住你的錯誤信息,並追蹤它使用堆棧跟蹤。嘗試使用Using語句正確處理對象。

    相關問題