1
我對MVVM Light for WPF相當陌生。我正在使用VIsual Studio 2013,並使用C#創建了一個項目。我已經得到了xaml中按鈕的邏輯。當用戶點擊這個按鈕時,我想讓應用程序生成一個數據庫和一個數據表。我已經在xaml中獲得了綁定命令,它將觸發中繼命令。我在模型中也有一個方法來生成數據庫和數據表。我在視圖模型中創建了一箇中繼命令,但除此之外,我不太確定接下來要做什麼。任何幫助,將不勝感激。在MVVM Light中需要執行邏輯按下按鈕以生成數據庫
視圖 - XAML
<Button Content="New Project" Margin="0,0,3,0" Command="{Binding AddProjectCommand}" IsEnabled="{Binding CommNotStreaming}" Grid.Column="2" Grid.Row="0"/>
視圖模型 -
public class ProjectConfigViewModel : ViewModelBase
{
//Binding AddProjectCommand
public RelayCommand AddProjectCommand { get; set; }
private string consoleText { get; set; }
private StringBuilder consoleBuilder = new StringBuilder(360);
public ProjectConfigViewModel()
{
this.AddProjectCommand = new RelayCommand(this.AddProject);
}
public void AddProject()
{
//Not really sure what to do here to call the ProjectDbInteraction class
}
}
型號 - 數據庫交互類
public class ProjectDbInteraction
{
//String rawDBConnectionString = "Server=localhost; Database=12_rse_002_db; uid=root; pwd=password; Connection Timeout=5;"; //TODO Either pick a standard for make this edittable
public void CreateProjectDb(string projName)
{
try
{
MySqlConnection connection = new MySqlConnection("DataSource=localhost;UserId=root;pwd=password");
MySqlCommand command = new MySqlCommand("CREATE DATABASE " + projName + ";", connection);
connection.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + projName;
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Projects(ProjectID INT NOT NULL, ProjectName VARCHAR(VARCHAR(255), ProjectStartDate DateTime, ProjectEndDate DateTime, ProjectNotes VARCHAR(MAX) PRIMARY KEY (ProjectID))";
command.ExecuteNonQuery();
//command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
connection.Close();
}
catch (Exception)
{
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
感謝那些工作。我很感激Theodosius。 – yams 2014-11-03 21:48:20