我需要从我的模型中加载来自DB的行,我看过一个教程,但是我在转换类型时遇到错误 这是我的模型代码:
public class Question
{
public Question() {
Answers = new List<Answer>();
}
public int Id { get; set;}
public string Text { get; set; }
public List<Answer> Answers { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Boolean Active { get; set; }
public ActionResult Questions()
{
String connectionString = "Password=********;Persist Security Info=True;User ID=sa;Initial Catalog=PoolMananger;Data Source=.;MultipleActiveResultSets=true";
String sql = "SELECT * FROM Questions";
SqlCommand cmd = new SqlCommand(sql, conn);
var model = new List<Question>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var Question = new Question();
Question.Text = (rdr["Text"] as string);
Question.StartDate = rdr["StartDate"];
Question.EndDate = rdr["EndDate"];
Question.Active = rdr["Active"];
model.Add(Question);
}
}
return View(model);
}
}
我在“SqlCommand cmd = new SqlCommand(sql, conn);
”上收到错误,错误在“conn
”中。也在“return View(model)
;
答案 0 :(得分:2)
# Load dataset
smartphone = pd.read_csv('all-users_w4_filtered.csv')
# Define Independent variable (X) and Dependent variable (Y)
x=smartphone[['mood_mean', 'valence_mean', 'app.social_mean', 'app.other_mean']].values
y=smartphone['target'].values
z=smartphone['benchmark'].values
# Reshape data
x = x.reshape(-1, 4)
y = y.reshape(-1, 1)
# First shuffle the data, then perform k-fold cross-validation
kf = KFold(n_splits=5, shuffle=True, random_state=0)
scores = cross_val_score(regr, x, y, cv=kf)
print ('5-fold shuffled cross validation scores: ', scores)
print ('Mean of cross-validation:', scores.mean())
for train_index, test_index in kf.split(x):
#print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = x[train_index], x[test_index]
y_train, y_test = y[train_index], y[test_index]
此时您的SqlCommand cmd = new SqlCommand(sql, conn);
尚未存在。因此,在定义它的conn
内移动此行。
我认为其他错误发生在
using
将强制转换添加到正确的类型:
Question.StartDate = rdr["StartDate"];
(当然,与其他行做类似的事情)
答案 1 :(得分:1)
在将它们分配给类变量之前,您必须转换它们(从服务器返回)。试试这个。
Question.StartDate = Convert.ToDateTime(rdr["StartDate"]);
Question.EndDate = Convert.ToDateTime(rdr["EndDate"]);
Question.Active = Convert.ToBoolean(rdr["Active"]);
请注意,这不会检查任何空值,并假设您没有从服务器返回空值。
答案 2 :(得分:0)
我发现你的代码中存在几个问题。
公共课问题 { 公共问题() { 答案=新的List(); }
public int Id { get; set; }
public string Text { get; set; }
public List<Answer> Answers { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Boolean Active { get; set; }
public ActionResult Questions()
{
String connectionString =
"Password=********;Persist Security Info=True;User ID=sa;Initial Catalog=PoolMananger;Data Source=.;MultipleActiveResultSets=true";
String sql = "SELECT * FROM Questions";
var model = new List<Question>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var Question = new Question();
Question.Text = rdr["Text"].ToString();
Question.StartDate = (DateTime) rdr["StartDate"];
Question.EndDate = (DateTime) rdr["EndDate"];
Question.Active = (Booolean) rdr["Active"];
model.Add(Question);
}
}
return View(model);
}
}