验证数据库输入C#访问OLEDB [英] Validating Database Input C# Access Oledb

查看:147
本文介绍了验证数据库输入C#访问OLEDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#和需要的验证可以加入到Access数据库插入之前,它的投入一点点帮助。

I am very new to C# and need a little bit of help on validating the input that can be added to the access database before it is inserted.

大部分的验证将是,如果事情没有被输入,然后会出现一个消息框,说'没有进入'如果有什么需要更多的字符,然后'长度太短。我怎么能做到这样呢?

Most of the validations will be if something has not been entered then a message box will appear saying 'nothing entered' or if something needs more characters then 'length too short'. How could i achieve something like this?

下面是我的code:

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();
                }
            }
        }

感谢您的帮助,我真的挣扎

Thanks for your help, im really struggling

推荐答案

有关长度,图案,文字与数字,等用户输入的验证应该在你的code完成获取数据层附近。 这是常规的前pressions是伟大的。有一吨的使用它们的信息,所以我不打算在这里重复。只是bingoogle定期EX pressions,你会发现一个丰富的信息。

Validation of user input regarding length, patterns, text versus numbers, etc. should be done before your code gets near the data layer. This is what regular expressions are great at. There is a ton of information on using them, so I won't try to repeat here. Just bingoogle regular expressions and you will find a wealth of info.

这篇关于验证数据库输入C#访问OLEDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆