插入数据库之前的数据验证按钮单击WPF MVVM

时间:2015-08-23 10:22:55

标签: c# wpf mvvm

我创建了一个MVVM应用程序,我在其中使用文本框在一个数据库表中添加客户信息(客户ID,客户名称)。这个应用程序有两种模式,第一种是插入模式,另一种是编辑模式。在插入模式中,我可以在数据库中添加新信息,在编辑模式下我可以编辑现有值,我需要做的就是当我添加插入模式和弹出错误时我必须避免重复的客户ID,并且在编辑模式中我想要编辑现有的cust名称,并将cust id编辑为新的uniq one,如果出现无效的情况弹出错误。名称可以是多个,但id在表中应该是唯一的,这是主要的请求。 到目前为止,我已经创建了应用程序并在数据库中插入数据,但我不知道如何在保存按钮单击上验证此数据。任何帮助高度赞赏。

这是应用程序代码。 的 MainWindow.xaml

<Grid>
   <TabControl >
        <TabItem Header="Insert" >
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="63*"/>
                    <ColumnDefinition Width="118*"/>
                    <ColumnDefinition Width="326*"/>
                </Grid.ColumnDefinitions>
                <Label Content="Custmore ID :" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" />
                <Label Content="Custmore Name :" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
                <TextBox Grid.Row="0"  x:Name="custIdTxt" Text="{Binding Path=CustId}"  Height="23" Grid.Column="2"   Width="150" HorizontalAlignment="Left" Margin="0,4"/>
                <TextBox Height="23" x:Name="custNameTxt"  Text="{Binding Path=CustName}"  Grid.Row="1" Grid.Column="2" Width="150" HorizontalAlignment="Left" Margin="0,4" />
                <Button Content="Save" Command="{Binding Path=InsertCommand}" Grid.Row="2" Grid.Column="2" Height="20" VerticalAlignment="Top" Margin="0,5,240,0"/>

            </Grid>
        </TabItem>
        <TabItem Header="Edit" Margin="-2,-2,-18,-1" >
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>

                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50*"/>
                    <ColumnDefinition Width="90*"/>
                </Grid.ColumnDefinitions>
                <Label Content="Custmore ID :" Grid.Row="0" Grid.Column="0" />
                <Label Content="Custmore Name :" Grid.Row="1" Grid.Column="0" />
                <TextBox Grid.Row="0" x:Name="custId1Txt" Text="{Binding Path=CustId}"  Height="23" Grid.Column="1"  Width="150" HorizontalAlignment="Left"/>
                <TextBox Height="23" x:Name="custName1Txt" Text="{Binding Path=CustName}" Grid.Row="1" Grid.Column="1" Width="150" HorizontalAlignment="Left" />
                <Button Content="Save" Command="{Binding Path=InsertCommand}" Grid.Row="2" Grid.Column="1" Height="20" VerticalAlignment="Top" Margin="0,5,240,0" />

            </Grid>
        </TabItem>
    </TabControl>
    <ListView Name="ListViewCustomerDetails" Grid.Row="3" Margin="0,150,0,0"    ItemsSource="{Binding Path=GridDataTable }"  >
        <ListView.View>
            <GridView x:Name="grdTest" >
                <GridViewColumn Header="CUSTOMER ID" DisplayMemberBinding="{Binding CUSTOMER_ID}"   Width="100"/>
                <GridViewColumn Header="CUSTOMER NAME" DisplayMemberBinding="{Binding CUSTOMER_NAME}"  Width="100" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MainViewModel vm = new MainViewModel();
        this.DataContext = vm;
        vm.GetData();
    }
}

这是viewmodel类 的 MainViewModel.cs

 class MainViewModel : BaseViewModel 
 {
    private string custName;
    private int custId;


    public string CustName 
    { 
        get
        {
            return this.custName;
        }

        set
        {
            this.custName = value;
            OnProprtyChanged("CustName");
        }
    }
    public int CustId
    { 
        get
        {
            return this.custId;
        }
        set
        {
            this.custId = value;
            OnProprtyChanged("CustId");
        }
    }

    private DataTable _dataTable;
    public DataTable GridDataTable
    {
        get
        {
            return _dataTable;
        }
        set
        {
            _dataTable = value;
            OnProprtyChanged("GridDataTable");
        }
    }

    private ICommand _insertCommand;
    public ICommand InsertCommand
    {
        get
        {
            if (_insertCommand == null)
                _insertCommand = new RelayCommand(param => InsertItem());
            return _insertCommand;
        }
    }

    private void InsertItem()
    {
        Hashtable ht = new Hashtable();
        ht.Add(Constants.CONST_CUST_ID, this.CustId);
        ht.Add(Constants.CONST_CUST_NAME, this.CustName);

        Model model = new Model();
        int res=model.InsertRecord(ht);
        if (res > 0)
        {
            //MessageBox.Show(res + " row(s) Inserted.");
            GetData();
        }
    }

    public void ResetGridDataTable()
    {
        this.GridDataTable = null;
    }
    public void GetData()
    {
        ResetGridDataTable();
        Model model = new Model();
        DataTable dt=model.GetAllData();
        this.GridDataTable = dt;
    }
}

模型类 的 Model.cs

class Model
{
    public int InsertRecord(Hashtable ht)
    {
        OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=XE;User ID=system;Password=practice;Unicode=True");
        int result=0;
        try
        {
            int custId = Int32.Parse(ht[Constants.CONST_CUST_ID].ToString());
            string custName = ht[Constants.CONST_CUST_NAME].ToString();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "INSERT INTO CUSTOMER VALUES('" + custId + "','" + custName + "')";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            result = cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    public  DataTable  GetAllData()
    {
        OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=XE;User ID=system;Password=practice;Unicode=True");
        DataTable dt =new DataTable();
        try
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "SELECT * FROM CUSTOMER";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
        return dt;
    }
}

0 个答案:

没有答案