[英]Passing Null values into database
I have written a service that reads XML files from a directory creates objects out of the data and passes the data into the database. 我编写了一个服务,它从目录中读取XML文件,从数据中创建对象并将数据传递到数据库中。 I had this working before but for some reason the service is now creating a table row however its inserting 0's into the database automatically instead of the data from the XML files.
我之前有这个工作,但由于某种原因,该服务现在创建一个表行,但它自动插入0到数据库而不是XML文件中的数据。 My code is as follows:
我的代码如下:
This web service method fires the XMLloader
class: 此Web服务方法将触发
XMLloader
类:
[WebMethod]
public void RunService()
{
//Call XML loader to get collection of XML documents
foreach (GamePlay value in XMLLoader.Start())
{
value.addNewGamePlay();
}
//call inject data method for each collection
}
This XMLLoader
class returns the array of GamePlay
objects: 此
XMLLoader
类返回GamePlay
对象的数组:
public static Array Start()
{
string[] filePaths = Directory.GetFiles(@"C:\Users\bradleya\Desktop\XML\");
List<GamePlay> gameObj = new List<GamePlay>();
foreach (string value in filePaths)
{
XmlDocument xd = new XmlDocument();
xd.Load(value);
XmlNode documentNode = xd.SelectSingleNode("/GamePlayData/GamePlay");
GamePlay newGame = new GamePlay();
newGame.setType(Convert.ToString(documentNode.SelectSingleNode("type").InnerText));
newGame.setGamePlayID(Convert.ToInt32(documentNode.SelectSingleNode("GamePlayID").InnerText));
newGame.setParticipantID(Convert.ToInt32(documentNode.SelectSingleNode("ParticipantID").InnerText));
newGame.setGameVersionID(Convert.ToInt32(documentNode.SelectSingleNode("GameVersionID").InnerText));
newGame.setGameID(Convert.ToInt32(documentNode.SelectSingleNode("GameID").InnerText));
newGame.setGameScenarioID(Convert.ToInt32(documentNode.SelectSingleNode("GameScenarioID").InnerText));
newGame.setStartDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("Start").InnerText));
newGame.setEndDateTime(Convert.ToDateTime(documentNode.SelectSingleNode("End").InnerText));
newGame.setSuccess(Convert.ToBoolean(documentNode.SelectSingleNode("Success").InnerText));
gameObj.Add(new GamePlay(gameObj));
}
return gameObj.ToArray();
}
This method is fired in the object class that passes the object variable to the data access layer: 在将对象变量传递给数据访问层的对象类中触发此方法:
public void addNewGamePlay()
{
// dataaccess method call to add GamePlay
// (from the DB) is then set for the object
DataAccessConn.createNewGamePlay(this.ParticipantID, this.GameVersionID, this.GameID, this.GameScenarioID, this.StartDateTime, this.EndDateTime, this.success);
}
Finally the data is passed into the database via the data access layer method here: 最后,数据通过数据访问层方法传递到数据库:
public static void createNewGamePlay(int ParticipantID, int GameVersionID,int GameID,int GameSenarioID,DateTime Start,DateTime End,Boolean success)
{
SqlConnection oConn = new SqlConnection();
oConn.ConnectionString = @"Data Source=SNICKERS\SQLEXPRESS;Initial Catalog=VervePhaseOne;Integrated Security=True";
oConn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = oConn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "addGamePlay";
cmd.Parameters.Add(new SqlParameter("@ParticipantID", SqlDbType.Int));
cmd.Parameters["@ParticipantID"].Value = ParticipantID;
cmd.Parameters.Add(new SqlParameter("@GameVersionID", SqlDbType.Int));
cmd.Parameters["@GameVersionID"].Value = GameVersionID;
cmd.Parameters.Add(new SqlParameter("@GameID", SqlDbType.Int));
cmd.Parameters["@GameID"].Value = GameID;
cmd.Parameters.Add(new SqlParameter("@GameScenarioID", SqlDbType.Int));
cmd.Parameters["@GameScenarioID"].Value = GameSenarioID;
cmd.Parameters.Add(new SqlParameter("@Start", SqlDbType.DateTime));
cmd.Parameters["@Start"].Value = DateTime.Now;
cmd.Parameters.Add(new SqlParameter("@End", SqlDbType.DateTime));
cmd.Parameters["@End"].Value = DateTime.Now;
cmd.Parameters.Add(new SqlParameter("@success", SqlDbType.Bit));
cmd.Parameters["@success"].Value = success;
cmd.ExecuteNonQuery();
}
The XML file I'm trying to pass is: 我试图传递的XML文件是:
<?xml version="1.0" encoding="utf-8" ?>
<GamePlayData>
<GamePlay>
<type>add</type>
<GamePlayID>1</GamePlayID>
<ParticipantID>1</ParticipantID>
<GameVersionID>1</GameVersionID>
<GameID>1</GameID>
<GameScenarioID>1</GameScenarioID>
<Start>22/01/2012 23:59:59</Start>
<End>22/01/2012 23:59:59</End>
<Success>False</Success>
</GamePlay>
</GamePlayData>
Just to clarify - My issue is my service is creating a table row however the variables are setting automatically to 0. Anyone know why this would be from the code? 只是为了澄清 - 我的问题是我的服务是创建一个表行但是变量自动设置为0.任何人都知道为什么这将来自代码?
Thanks in advance. 提前致谢。
EDIT -- As requested store procedure added, thanks 编辑 - 正如所要求的商店程序添加,谢谢
CREATE PROCEDURE [dbo].[addGamePlay]
@ParticipantID int,
@GameVersionID int,
@GameID int,
@GameScenarioID int,
@Start dateTime,
@End dateTime,
@Success varchar(10)
AS
INSERT INTO GamePlay(ParticipantID,GameVersionID,GameID,GameScenarioID,StartDateTime,EndDateTime,Success)
VALUES (@ParticipantID,@GameVersionID,@GameID,@GameScenarioID,@Start,@End,@Success)
return @@identity
It looks like you're not adding newGame
to your list after you've hydrated it from the XML file: 在您从XML文件中对其进行水合后,您似乎没有将
newGame
添加到列表中:
gameObj.Add(new GamePlay(gameObj));
I think should be: 我认为应该是:
gameObj.Add(newGame);
In your array.Start methods should this line: 在你的array.Start方法应该这一行:
gameObj.Add(new GamePlay(gameObj));
be : 是:
gameObj.Add(newGame);
? ?
i think all the places in which you are encountering a 0 instead or null are simple int types 我认为你遇到0或者null的所有地方都是简单的int类型
this happens because there can be no null values for values types in .Net and actually 0 is the null values of an int 发生这种情况是因为.Net中的值类型不能有空值,实际上0是int的空值
so you should probably change the definition of GamePlay
class from using int
to int?
所以你应该把
GamePlay
类的定义从使用int
更改为int?
OR Nullable<int>
OR
Nullable<int>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.