我只是想在Visual Studio中输出数据库表中的所有条目,并收到以下错误:
异常详细信息:System.ArgumentNullException:值不能为null。
我确定数据库的名称是正确的,并且还有条目,因为Insert-Method工作正常。
也许存在解析问题或者我必须在web.config中更改某些内容吗?
这是我的代码。希望有人可以帮助我:
我的模特:
encode in UTF-8 without BOM
我的存储库:
[TableName("kb_Chat_Message")]
[PrimaryKey("id", autoIncrement = true)]
public class ChatMessage
{
public static string TABLE_NAME = "kb_Chat_Message";
public static string ROW_ID = "id";
public static string MESSAGE = "message";
public static string GROUP = "group";
public static string ROW_TIME = "timestamp";
public int id { get; set; }
public string group { get; set; }
public string message { get; set; }
public DateTime timestamp { get; set; }
public ChatMessage(string message, string groupName, DateTime time) {
this.message = message;
this.group = groupName;
this.timestamp = time;
}
}
我的中心:
public class ChatRepository
{
private readonly UmbracoDatabase _database;
public ChatRepository()
{
_database = ApplicationContext.Current.DatabaseContext.Database;
}
public IList<ChatMessage> GetAll()
{
// === Next line throws System.ArgumentNullException ===
return _database.Fetch<ChatMessage>("SELECT * FROM " + ChatMessage.TABLE_NAME);
}
public void Insert(ChatMessage message)
{
_database.Insert(message);
}
}
我的数据库看起来像: Database named "kb_Chat_Message"
答案 0 :(得分:0)
那么也许你应该尝试这样的事情
public IList<ChatMessage> GetAll()
{
var con = ConfigurationManager.ConnectionStrings["Yourconnection"].ToString();
ChatMessage _chatMessage = new ChatMessage();
IList<ChatMessage> _all_chatMessages = new List<ChatMessage>();
using (SqlConnection myConnection = new SqlConnection(con))
{
string oString = "Select * from kb_Chat_Message";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
_chatMessages.RequiredField = oReader["RequiredFieldname"].ToString();
_all_chatMessages.Add(_chatMessage)
}
myConnection.Close();
}
}
return _all_chatMessages;
}
让我知道......