通过调用datalayer获取数据到ViewModel
Getting data into ViewModel by calling datalayer
我正在尝试将存储的数据从 SQLite 数据库中获取到一个简单的文本框中,但我总是得到 null。当数据层被调用时,数据首先正确加载,但在某些时候它再次变为空,我无法弄清楚它在哪里或为什么出错。
这是我的视图模型
public class FirstReadViewModel : NotifyUIBase
{
private string _scriptNotes;
public string ScriptNotes //binded in the View
{
get { return _scriptNotes; }
set { _scriptNotes = value; RaisePropertyChanged("ScriptNotes"); }
}
public FirstReadViewModel()
{
var dbFunctions = new DataLayer();
dbFunctions.GetFirstReadNotes();
}
}
数据层
public void GetFirstReadNotes()
{
String dbConnectionString = @"Data Source =DB.sqlite";
SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string ScriptNotes = reader["scriptnotes"].ToString();
}
reader.Close();
cnn.Close();
}
}
你的数据层方法应该如下所示return只需要先注意:
public string GetFirstReadNotes()
{
string scriptNotes = string.Empty;
String dbConnectionString = @"Data Source =DB.sqlite";
SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
SQLiteDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
scriptNotes = reader["scriptnotes"].ToString();
}
reader.Close();
cnn.Close();
}
return scriptNotes;
}
和FirstReadViewModel
如下:
public FirstReadViewModel()
{
var dbFunctions = new DataLayer();
this.ScriptNotes = dbFunctions.GetFirstReadNotes();
}
你应该使用接口将你的 DataLayer
注入你的 FirstReadViewModel
并稍微更改你的代码
创建接口
interface IFirstReadViewModelDAL
{
string GetFirstReadNotes();
}
使用它,也利用using函数
internal class DataLayer: IFirstReadViewModelDAL
{
public string GetFirstReadNotes()
{
string ScriptNotes;
String dbConnectionString = @"Data Source =DB.sqlite";
using(SQLiteConnection cnn = new SQLiteConnection(dbConnectionString))
{
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ScriptNotes = reader["scriptnotes"].ToString();
}
}
}
return ScriptNotes;
}
}
使用接口
注入你的DataLayer
private IFirstReadViewModelDAL dbFunctions;
private string _scriptNotes;
public string ScriptNotes //binded in the View
{
get { return _scriptNotes; }
set { _scriptNotes = value; RaisePropertyChanged("ScriptNotes"); }
}
public FirstReadViewModel(IFirstReadViewModelDAL injectDAL)
{
dbFunctions = injectDAL;
}
private void LoadData();
{
// use your dbFunctions
}
要注入它,您需要一个单独的 class,比如
public class Controller
{
public static FirstReadViewModel getNewFirstReadViewModel()
{
var dal = new DataLayer();
var vm = new FirstReadViewModel(dal);
return vm;
}
}
我正在尝试将存储的数据从 SQLite 数据库中获取到一个简单的文本框中,但我总是得到 null。当数据层被调用时,数据首先正确加载,但在某些时候它再次变为空,我无法弄清楚它在哪里或为什么出错。
这是我的视图模型
public class FirstReadViewModel : NotifyUIBase
{
private string _scriptNotes;
public string ScriptNotes //binded in the View
{
get { return _scriptNotes; }
set { _scriptNotes = value; RaisePropertyChanged("ScriptNotes"); }
}
public FirstReadViewModel()
{
var dbFunctions = new DataLayer();
dbFunctions.GetFirstReadNotes();
}
}
数据层
public void GetFirstReadNotes()
{
String dbConnectionString = @"Data Source =DB.sqlite";
SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string ScriptNotes = reader["scriptnotes"].ToString();
}
reader.Close();
cnn.Close();
}
}
你的数据层方法应该如下所示return只需要先注意:
public string GetFirstReadNotes()
{
string scriptNotes = string.Empty;
String dbConnectionString = @"Data Source =DB.sqlite";
SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
SQLiteDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
scriptNotes = reader["scriptnotes"].ToString();
}
reader.Close();
cnn.Close();
}
return scriptNotes;
}
和FirstReadViewModel
如下:
public FirstReadViewModel()
{
var dbFunctions = new DataLayer();
this.ScriptNotes = dbFunctions.GetFirstReadNotes();
}
你应该使用接口将你的 DataLayer
注入你的 FirstReadViewModel
并稍微更改你的代码
创建接口
interface IFirstReadViewModelDAL
{
string GetFirstReadNotes();
}
使用它,也利用using函数
internal class DataLayer: IFirstReadViewModelDAL
{
public string GetFirstReadNotes()
{
string ScriptNotes;
String dbConnectionString = @"Data Source =DB.sqlite";
using(SQLiteConnection cnn = new SQLiteConnection(dbConnectionString))
{
cnn.Open();
SQLiteCommand cmd = new SQLiteCommand(cnn);
cmd.CommandText = "SELECT* FROM projects WHERE projectID = 1";
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ScriptNotes = reader["scriptnotes"].ToString();
}
}
}
return ScriptNotes;
}
}
使用接口
注入你的DataLayer
private IFirstReadViewModelDAL dbFunctions;
private string _scriptNotes;
public string ScriptNotes //binded in the View
{
get { return _scriptNotes; }
set { _scriptNotes = value; RaisePropertyChanged("ScriptNotes"); }
}
public FirstReadViewModel(IFirstReadViewModelDAL injectDAL)
{
dbFunctions = injectDAL;
}
private void LoadData();
{
// use your dbFunctions
}
要注入它,您需要一个单独的 class,比如
public class Controller
{
public static FirstReadViewModel getNewFirstReadViewModel()
{
var dal = new DataLayer();
var vm = new FirstReadViewModel(dal);
return vm;
}
}