C# 公共静态类CustomerDAL { 公共静态无效插入(客户p){……} 公共静态列表FillComboBox(){……} 公共无效更新(客户旧客户、客户新客户){……} } } ---------------------------------------------------- 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用类库; 使用系统数据; 名称空间BAL { 公共类客户 { 公共int Id{get;set;} 公共字符串名称{get;set;} ....................... ....................... } } ------------------------------------------------------ 在UI中创建Windows或Web窗体,添加按钮和文本框,并在按钮上单击Save\u事件 如果(txtName.Text==“”) { MessageBox.Show(“一些文本”,“一些标题”, MessageBoxButtons.OK,MessageBoxIcon.Error); txtName.Focus(); } 其他的 { //呼叫余额 var cus=新客户{ Name=txtName.Text } //呼叫达尔 客户数据插入(cus); MessageBox.Show(“信息”,“记录已成功保存在数据库中”, MessageBoxButtons.OK,MessageBoxIcon.Information); txtName.tex=“”; } 希望这对你有帮助。
创建一个解决方案,给它一个合适的名称。然后在解决方案内部创建两个汇编项目BAL和DAL。如果您仍然对汇编感到困惑,那么请避免创建汇编,下面的链接可能会对您有所帮助。C# 公共静态类CustomerDAL { 公共静态无效插入(客户p){……} 公共静态列表FillComboBox(){……} 公共无效更新(客户旧客户、客户新客户){……} } } ---------------------------------------------------- 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用类库; 使用系统数据; 名称空间BAL { 公共类客户 { 公共int Id{get;set;} 公共字符串名称{get;set;} ....................... ....................... } } ------------------------------------------------------ 在UI中创建Windows或Web窗体,添加按钮和文本框,并在按钮上单击Save\u事件 如果(txtName.Text==“”) { MessageBox.Show(“一些文本”,“一些标题”, MessageBoxButtons.OK,MessageBoxIcon.Error); txtName.Focus(); } 其他的 { //呼叫余额 var cus=新客户{ Name=txtName.Text } //呼叫达尔 客户数据插入(cus); MessageBox.Show(“信息”,“记录已成功保存在数据库中”, MessageBoxButtons.OK,MessageBoxIcon.Information); txtName.tex=“”; } 希望这对你有帮助。,c#,ado.net,oledb,C#,Ado.net,Oledb,创建一个解决方案,给它一个合适的名称。然后在解决方案内部创建两个汇编项目BAL和DAL。如果您仍然对汇编感到困惑,那么请避免创建汇编,下面的链接可能会对您有所帮助。 在代码接近数据层之前,应验证用户输入的长度、模式、文本与数字等信息。 这就是正则表达式擅长的地方。关于使用它们有很多信息,所以我不想在这里重复。只要使用正则表达式,您就会发现大量信息。如果您使用.Net版本>3.5,则可以对验证进行注释。它是一个web应用程序还是桌面应用程序?它将是一个桌面应用程序,请查看参数化查询。。即使不是基于
在代码接近数据层之前,应验证用户输入的长度、模式、文本与数字等信息。
这就是正则表达式擅长的地方。关于使用它们有很多信息,所以我不想在这里重复。只要使用正则表达式,您就会发现大量信息。如果您使用.Net版本>3.5,则可以对验证进行注释。它是一个web应用程序还是桌面应用程序?它将是一个桌面应用程序,请查看参数化查询。。即使不是基于网络的,也要正确地开始做,而不是在以后苦苦挣扎。即使是基于桌面的,您也可以使用sql注入。仍然有点困惑,我在哪里添加这些代码位?
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();
}
}
}
private bool CheckValidation( )
{
bool returnBool = true;
if ( string.IsNullOrWhiteSpace(txtName.txt) )
{
//Show a label next to you text box
returnBool = false;
}
return returnBool;
}
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.