我是C#的新手,需要一些帮助来验证可以在插入之前添加到访问数据库的输入。
如果没有输入某些内容,那么大多数验证将会出现一个消息框,显示“没有输入任何内容”或者某些内容需要更多字符,然后“长度太短”。我怎么能实现这样的目标?
这是我的代码:
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 :(得分: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
答案 1 :(得分: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.
创建一个解决方案,给它一个正确的名称。然后在解决方案中创建两个组件项目BAL和DAL。如果你仍然对组件感到困惑,那么避免创建程序集和后续链接可能会帮助你。 http://www.tutorialized.com/tutorial/3-Tier-Architecture-in-asp.net-using-c/67931
答案 2 :(得分:0)
应在代码接近数据层之前验证用户输入的长度,模式,文本与数字等。 这就是正则表达式擅长的。关于使用它们的信息很多,所以我不打算在这里重复一遍。只需使用bingoogle正则表达式,您就会发现大量信息。