WPF_多个表新增
作者:互联网
每一个应用程序中都会存在这新增信息的模块,有时候我们可能是新增一个表,有时候是多个表一起新增,那么,我们要如何实现这功能呢,多个表新增其实和单表新增是一样的,只是多表新增比单表新增麻烦一点下面我给大家演示一下。
首先,我们要做的是在数据库中把存储过程写好,不同的开发人员用的数据库也不一样,我用的是SQL Server 2014 Management Studio,下面是我写的存储过程,我们把要新增的表的方法按顺序从基础表到业务表的格式编写好,然后给每一个新增的字段赋值,给到相应的值,同时在后面写出继承下一个表的ID值,好让下一个表来接收这个表传过去的ID值,具体的写法如下所示:
------------新增客户信息----------------------
IF @Type = 'Window_InsertClient'
BEGIN
INSERT
PW_Client(OpeningBankID, BranchID, CustomerReferred, ClientName, BrevityCode, Linkman, PostCode, Site, Phone, Faxes, E_mail, Remark, UnitCode)
VALUES(@OpeningBankID, @BranchID, @CustomerReferred, @ClientName, @BrevityCode, @Linkman, @PostCode, @Site, @Phone, @Faxes, @E_mail, @Remark, @UnitCode)
--给下一个表继承的ID值
select @@IDENTITY
END
--新增车辆信息
IF @Type = 'Window_InsertVehicle'
BEGIN
INSERT Basic_Vhicle(ClientID,CarTypeID, VehicleTypeID, CarID, ColourID, BusNumber, HackMan, DrPhone, IdentificationCard, ComeTime, FactoryNumber, ChassisNumber, EngineType, EngineNumber, VIN, SpeedBox)
VALUES(@ClientID,@CarTypeID, @VehicleTypeID, @CarID, @ColourID, @BusNumber, @HackMan, @DrPhone, @IdentificationCard, @ComeTime, @FactoryNumber, @ChassisNumber, @EngineType, @EngineNumber, @VIN, @SpeedBox)
END
第二步,写好存储之后,我们到服务端中写服务传送的编码,在编写代码之前我们要先建立一个操作契约,然后在写方法,实例化所有要新增的字段,然后在一一的赋值,调用数据库的方法来传输数据,打开数据通道,具体的写法如下所示:
//新增客户档案信息
//建立操作契约
[OperationContract]
public DataSet Window_InsertClient(int intOpeningIDBankID,int intBranchID, tring strCustomerReferred, string strClientName,string strLinkman, string strPostCode,string strSite,string strPhone,string strFaxes,string strE_mail, string strRemark,string strUnitCode)
{
SqlParameter[] mySqlParameters =
{
//定义传递参数,以及传递参数的类型
new SqlParameter("@Type",SqlDbType.NChar),
new SqlParameter("@OpeningBankID",SqlDbType.Int),
new SqlParameter("@BranchID",SqlDbType.Int),
new SqlParameter("@CustomerReferred",SqlDbType.NChar),
new SqlParameter("@ClientName",SqlDbType.NChar),
new SqlParameter("@Linkman",SqlDbType.NChar),
new SqlParameter("@PostCode",SqlDbType.NChar),
new SqlParameter("@Site",SqlDbType.NChar),
new SqlParameter("@Phone",SqlDbType.NChar),
new SqlParameter("@Faxes",SqlDbType.NChar),
new SqlParameter("@E_mail",SqlDbType.NChar),
new SqlParameter("@Remark",SqlDbType.NChar),
new SqlParameter("@UnitCode",SqlDbType.NChar),
};
//给对象赋值
mySqlParameters[0].Value = "Window_InsertClient";
mySqlParameters[1].Value = intOpeningIDBankID;
mySqlParameters[2].Value = intBranchID;
mySqlParameters[3].Value = strCustomerReferred;
mySqlParameters[4].Value = strClientName;
mySqlParameters[5].Value = strLinkman;
mySqlParameters[6].Value = strPostCode;
mySqlParameters[7].Value = strSite;
mySqlParameters[8].Value = strPhone;
mySqlParameters[9].Value = strFaxes;
mySqlParameters[10].Value = strE_mail;
mySqlParameters[11].Value = strRemark;
mySqlParameters[12].Value = strUnitCode;
DataTable myDataTable = myMethod.QueryDataTable("CustomerManagement", mySqlParameters);
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(myDataTable);
return myDataSet;
}
//1.3新增车辆信息表
//建立操作契约
[OperationContract]
public int Window_InsertVehicle(int intClientID,int intCarTypeID, int intVehicleTypeID,int intCarID, int intColourID, string strBusNumber, string strHackMan, string strDrPhone, string strIdentificationCard, DateTime datComeTime, string strFactoryNumber, string strChassisNumber, string strEngineType, string strEngineNumber, string strVIN, string strSpeedBox)
{
SqlParameter[] mySqlParameters =
{
new SqlParameter("@Type",SqlDbType.NChar),
new SqlParameter("@ClientID",SqlDbType.Int),
new SqlParameter("@CarTypeID",SqlDbType.Int),
new SqlParameter("@VehicleTypeID",SqlDbType.Int),
new SqlParameter("@CarID",SqlDbType.Int),
new SqlParameter("@ColourID",SqlDbType.Int),
new SqlParameter("@BusNumber",SqlDbType.NChar),
new SqlParameter("@HackMan",SqlDbType.NChar),
new SqlParameter("@DrPhone",SqlDbType.NChar),
new SqlParameter("@IdentificationCard",SqlDbType.NChar),
new SqlParameter("@ComeTime",SqlDbType.DateTime),
new SqlParameter("@FactoryNumber",SqlDbType.NChar),
new SqlParameter("@ChassisNumber",SqlDbType.NChar),
new SqlParameter("@EngineType",SqlDbType.NChar),
new SqlParameter("@EngineNumber",SqlDbType.NChar),
new SqlParameter("@VIN",SqlDbType.NChar),
new SqlParameter("@SpeedBox",SqlDbType.NChar),
};
//给对象赋值
mySqlParameters[0].Value = "Window_InsertVehicle";
mySqlParameters[1].Value = intClientID;
mySqlParameters[2].Value = intCarTypeID;
mySqlParameters[3].Value = intVehicleTypeID;
mySqlParameters[4].Value = intCarID;
mySqlParameters[5].Value = intColourID;
mySqlParameters[6].Value = strBusNumber;
mySqlParameters[7].Value = strHackMan;
mySqlParameters[8].Value = strDrPhone;
mySqlParameters[9].Value = strIdentificationCard;
mySqlParameters[10].Value = datComeTime;
mySqlParameters[11].Value = strFactoryNumber;
mySqlParameters[12].Value = strChassisNumber;
mySqlParameters[13].Value = strEngineType;
mySqlParameters[14].Value = strEngineNumber;
mySqlParameters[15].Value = strVIN;
mySqlParameters[16].Value = strSpeedBox;
int count = myMethod.UpdateData("CustomerManagement", mySqlParameters);
return count;
}
最后一步,也是最关键的一步,我们有先获取页面上所有字段的数值,判断字段的值为空还有数据类型是否正确,在判断没有问题的时候,在进行提交,点击保存按钮提交,程序会一步一步的循环,先把基础表的数据新增成功之后,然后在传输这个表的ID到下一个表那里进行新增判断,一步接着一步循环判断,直到将所有要新增的表新增成功,接这样,接完成一个多表新增的功能啦。
public void Btn_Save_Click(object sender, RoutedEventArgs e)
{
//新增车辆信息
try
{
int intBranchID = Convert.ToInt32(BranchName.SelectedValue);
int intOpeningBankID = Convert.ToInt32(OpeningBankName.SelectedValue);
string strClientName = ClientName.Text.Trim();
string strCustomerReferred = CustomerReferred.Text.Trim();
string strLinkman = Linkman.Text.Trim();
string strPhone = Phone.Text.Trim();
string strFaxes = Faxes.Text.Trim();
string strE_mail = E_mail.Text.Trim();
string strSite = Site.Text.Trim();
string strPostCode = PostCode.Text.Trim();
string strUnitCode = UnitCode.Text.Trim();
string strRemark = Remark.Text.Trim();
if (intBranchID > 0 && intOpeningBankID > 0 && strClientName != "" && strLinkman != "" && strPhone != "" && strRemark != "")
{
////执行新增,新增客户
DataTable count = myCustomermanagement.Window_InsertClient(intOpeningBankID, intBranchID, strCustomerReferred, strClientName,
strLinkman, strPostCode, strSite, strPhone, strFaxes, strE_mail, strRemark, strUnitCode).Tables[0];
///获取单元格(用户ID)
int intClientID = Convert.ToInt32(count.Rows[0][0].ToString());
//判断返回数据的行
if (count.Rows.Count > 0)
{
int intCarTypeID = Convert.ToInt32(CarTypeName.SelectedValue);
int intVehicleTypeID = Convert.ToInt32(VehicleName.SelectedValue);
int intCarID = Convert.ToInt32(CarName.SelectedValue);
int intColourID = Convert.ToInt32(ColourName.SelectedValue);
string strBusNumber = BusNumber.Text.Trim();
string strHackMan = HackMan.Text.Trim();
string strDrPhone = DrPhone.Text.Trim();
string strIdentificationCard = IdentificationCard.Text.Trim();
DateTime datComeTime = Convert.ToDateTime(ComeTime.Text);
string strFactoryNumber = FactoryNumber.Text.Trim();
string strChassisNumber = ChassisNumber.Text.Trim();
string strEngineType = EngineType.Text.Trim();
string strEngineNumber = EngineNumber.Text.Trim();
string strVIN = VIN.Text.Trim();
string strSpeedBox = SpeedBox.Text.Trim();
if (intVehicleTypeID > 0 && intCarID > 0 && strBusNumber != "")
{
//执行新增车辆信息
int intCount = Convert.ToInt32(myCustomermanagement.Window_InsertVehicle(intClientID, intCarTypeID, intVehicleTypeID, intCarID,
intColourID, strBusNumber, strHackMan, strDrPhone, strIdentificationCard, datComeTime, strFactoryNumber,
strChassisNumber, strEngineType, strEngineNumber, strVIN, strSpeedBox));
if (intCount > 0)
{
Message dr = new Message("新增客户车辆信息数据成功!");
dr.Show();
//MessageBoxResult dr = MessageBox.Show("新增客户车辆信息数据成功!", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);//弹出确定对话框
ordinaryMaintain.SetNotEditable(contern.Children);
////调用表格数据
SelectCustomerManagement();
SelectVehicle();
//激活新增按钮
btn_Inter.IsEnabled = true;
btn_Inter.Opacity = 0.5;
//初始化操作按钮
btn_Delete.IsEnabled = false;//禁用删除按钮
btn_Delete.Opacity = 0.5;
btn_Save.IsEnabled = false;//禁用保存按钮
btn_Save.Opacity = 0.5;
btn_Cancel.IsEnabled = false;//禁用取消按钮
btn_Cancel.Opacity = 0.5;
}
else if (intCount == -1)
{
MessageBox.Show("客户重复!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation);
}
}
else
{
MessageBox.Show("请把车辆信息数据填写完整!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);//弹出确认对话框
}
}
}
else
{
MessageBox.Show("请把页面数据填写完整!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Warning);//弹出确认对话框
}
}
catch (Exception)
{
throw;
}
}
标签:SqlDbType,string,多个,新增,Value,SqlParameter,new,WPF,mySqlParameters 来源: https://blog.csdn.net/weixin_44548316/article/details/99698234