2014-04-24 96 views
0

我對C#很新,需要一些幫助來驗證可以添加到訪問數據庫的輸入,然後才能插入它。驗證數據庫輸入C#訪問Oledb

如果還沒有輸入內容,大部分驗證都會顯示「沒有輸入任何內容」,或者如果某些內容需要更多字符,那麼「內容太短」。我怎麼能實現這樣的事情?

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Data.OleDb; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using ClassLibrary; 
using System.Data; 

namespace ClassLibrary2 
{ 
    public class Class1 
    { 
     OleDbConnection connection; 
     OleDbCommand command; 

     private void ConnectTo() 
     { 
      connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\CMS\CustomerDatabase.accdb;Persist Security Info=False"); 
      command = connection.CreateCommand(); 
     } 
     public Class1() 
     { 
      ConnectTo(); 
     } 

     public void Insert(Customer p) 
     { 
      try 
      { 
       command.CommandText = "INSERT INTO CustomerData ([Forename], [Surname], [Email Address], [Home Phone Number], [Mobile Phone Number], [Address], [AreaTown], [County], [Postcode]) VALUES('" + p.Forename1 + "', '" + p.Surname1 + "', '" + p.EAddress1 + "', '" + p.HomePhone1 + "' , '" + p.MobNum1 + "' , '" + p.Address1 + "', '" + p.AreaTown1 + "', '" + p.County1 + "', '" + p.Postcode1 + "')"; 
       command.CommandType = CommandType.Text; 
       connection.Open(); 

       command.ExecuteNonQuery(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
      finally 
      { 
       if (connection != null) 
       { 
        connection.Close(); 
       } 
      } 
     } 

     public List<Customer> FillComboBox() 
     { 
      List<Customer> CustomersList = new List<Customer>(); 
      try 
      { 
       command.CommandText = "SELECT * FROM CustomerData"; 
       command.CommandType = CommandType.Text; 
       connection.Open(); 

       OleDbDataReader reader = command.ExecuteReader(); 

       while (reader.Read()) 
       { 
        Customer p = new Customer(); 

        p.Id = Convert.ToInt32(reader["ID"].ToString()); 
        p.Forename1 = reader["Forename"].ToString(); 
        p.Surname1 = reader["Surname"].ToString(); 
        p.EAddress1 = reader["Email Address"].ToString(); 
        p.HomePhone1 = reader["Home Phone Number"].ToString(); 
        p.MobNum1 = reader["Mobile Phone Number"].ToString(); 
        p.Address1 = reader["Address"].ToString(); 
        p.AreaTown1 = reader["AreaTown"].ToString(); 
        p.County1 = reader["County"].ToString(); 
        p.Postcode1 = reader["Postcode"].ToString(); 

        CustomersList.Add(p); 
       } 
       return CustomersList; 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
      finally 
      { 
       if (connection != null) 
       { 
        connection.Close(); 
       } 
      }  
     } 

     public void Update(Customer oldCustomer, Customer newCustomer) 
     { 
      try 
      { 
       command.CommandText = "UPDATE CustomerData SET [Forename] = @newCustomer.Forename1, [Surname] = @newCustomer.Surname1, [Email Address] = @newCustomer.EAddress1, [Home Phone Number]= @newCustomer.HomePhone1, [Mobile Phone Number] = @newCustomer.MobNum1, [Address]= @newCustomer.Address1, [AreaTown] = @newCustomer.AreaTown1, [County]= @newCustomer.County1, [Postcode]= @newCustomer.Postcode1 WHERE [ID] = @oldCustomer.Id"; 
       command.Parameters.AddWithValue("@Forename", newCustomer.Forename1); 
       command.Parameters.AddWithValue("@Surname", newCustomer.Surname1); 
       command.Parameters.AddWithValue("@Email Address", newCustomer.EAddress1); 
       command.Parameters.AddWithValue("@Home Phone Number", newCustomer.HomePhone1); 
       command.Parameters.AddWithValue("@Mobile Phone Number", newCustomer.MobNum1); 
       command.Parameters.AddWithValue("@Address", newCustomer.Address1); 
       command.Parameters.AddWithValue("@AreaTown", newCustomer.AreaTown1); 
       command.Parameters.AddWithValue("@County", newCustomer.County1); 
       command.Parameters.AddWithValue("@Postcode", newCustomer.Postcode1); 
       command.Parameters.AddWithValue("@ID", oldCustomer.Id); 

       command.CommandType = CommandType.Text; 
       connection.Open(); 

       command.ExecuteNonQuery(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
      finally 
      { 
       if (connection != null) 
       { 
        connection.Close(); 
       } 
      } 
     } 

感謝您的幫助,我真的掙扎

+0

如果您使用.Net版本> 3.5,則可以註釋驗證。它是一個網絡或桌面應用程序? – Senthil

+0

這將是一個桌面應用程序 – user3570972

+0

另外,看看參數化查詢..即使不是基於網絡,讓你開始做的正確,VS掙扎以後。即使是基於桌面的,你也可以打開sql注入。 – DRapp

回答

0

如果要存檔此試圖實現檢查每一個驗證的功能,你執行保存功能 前希望下面的示例函數會給你一個想法

 private bool CheckValidation() 
     { 

      bool returnBool = true; 

      if (string.IsNullOrWhiteSpace(txtName.txt)) 
      { 
       //Show a label next to you text box 

       returnBool = false; 
      } 


      return returnBool; 

     } 

如果返回值爲true將數據保存到數據庫ü唱SP 希望這會引導你。如需瞭解更多信息,請參閱SQLHelper類。它會讓你的生活更輕鬆,因此你不需要在任何地方實施連接和SP調用。如果不讓我知道會送你樣板工程。

歡迎.NET世界

問候, Pubudu

+0

仍然有點困惑,我在哪裏添加這些代碼? – user3570972

0
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using ClassLibrary; 
using System.Data; 

namespace DAL 
{ 
    public static class CustomerDAL 
    { 
    public static void Insert(Customer p){.......} 
    public static List<Customer> FillComboBox(){......} 
    public void Update(Customer oldCustomer, Customer newCustomer){.......} 
    } 
} 
---------------------------------------------------- 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using ClassLibrary; 
using System.Data; 

namespace BAL 
{ 
    public class Customer 
    { 
    public int Id {get;set;} 
    public string Name {get;set;} 
    ....................... 
    ....................... 
    } 
} 
------------------------------------------------------ 
In UI Create a Windows or Web Form and add buttons and textbox and on buttonSave_Click event 
If(txtName.Text=="") 
{ 
MessageBox.Show("Some text", "Some title", 
    MessageBoxButtons.OK, MessageBoxIcon.Error); 
    txtName.Focus(); 
} 
else 
{ 
//calling BAL 
var cus=new Customer{ 
Name=txtName.Text 
} 
//Calling DAL 
CustomerDAL.Insert(cus); 
MessageBox.Show("Information", "Record saved successfully inti the database.", 
    MessageBoxButtons.OK, MessageBoxIcon.Information); 
    txtName.tex=""; 

} 

Hope this will help you. 

創建一個解決方案,給它的解決方案內適當name.Then創建兩個組裝技術項目BAL和DAL.If你還在對組裝感到困惑,然後避免創建組件,以下鏈接可能會對您有所幫助。 http://www.tutorialized.com/tutorial/3-Tier-Architecture-in-asp.net-using-c/67931

0

驗證用戶輸入的長度,模式,文本與數字等應在代碼接近數據層之前完成。 這是正則表達式擅長的地方。有很多關於使用它們的信息,所以我不會在這裏重複。只是賓果正則表達式,你會發現豐富的信息。