string GetSerials = "SELECT SerialNumber from Warranty";
string TestUpdateDates = "UPDATE Warranty SET StartDate = '@StartDate', EndDate = '@EndDate' WHERE SerialNumber = '@result'";
//string TestUpdateDates2 = "UPDATE Warranty SET StartDate = cDate(Format('@StartDate', 'MM/dd/yyyy')), EndDate = cDate(Format('@EndDate', 'MM/dd/yyyy')) WHERE(SerialNumber = '@result')";
DataTable dataTable = new DataTable();
using (var conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Blah\Blah\Blah\Warranty.accdb"))
using (OleDbCommand serialCommand = new OleDbCommand(GetSerials, conn1))
{
conn1.Open();
dataTable.Load(serialCommand.ExecuteReader());
foreach (DataRow row in dataTable.Rows)
{
var result = row["SerialNumber"].ToString();
WebRequest request = WebRequest.Create("urlpart1" + result + "urlpart2");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using (var streamr = new StreamReader(response.GetResponseStream()))
using (OleDbCommand testupdateCommand = new OleDbCommand(TestUpdateDates, conn1))
using (OleDbCommand updateCommand = new OleDbCommand(UpdateDates, conn1))
using (OleDbCommand deleteCommand = new OleDbCommand(DeleteIncomplete, conn1))
{
json = streamr.ReadToEnd();
List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json);
MyObject obj = list[0]; //Base Warranty
// obj -- Base Warranty
var StartDate = obj.Start;
var EndDate = obj.End;
//testupdateCommand.Parameters.Add("@StartDate", OleDbType.Date).Value = StartDate;
//testupdateCommand.Parameters.Add("@EndDate", OleDbType.Date).Value = EndDate;
testupdateCommand.Parameters.AddWithValue("@StartDate", StartDate);
testupdateCommand.Parameters.AddWithValue("@EndDate", EndDate);
testupdateCommand.Parameters.AddWithValue("@result", result);
我正在尝试使用JSON响应中的值更新数据库。我想循环遍历[&#34; SerialNumber&#34;]列中的所有值,并使用该序列号,我能够获得开始日期和结束日期来自网址。我想将这些值存储到相同的序列号中,我用它将url放入相应的字段(StartDate,EndDate)。
我已经注释了我试图做的其他一些方法,但我得到的输出是只有我得到的第一个SerialNumber被更改,它不会继续循环我的列100个值。如果我添加
MessageBox.Show(result); //SerialNumber
MessageBox.Show(StartDate.ToString());
MessageBox.Show(EndDate.ToString());
它通过MessageBox.Show循环,但它不会以这种方式更新我的数据库。为什么它不会自己迭代?
我正在使用ACCESS2013如果这很重要
答案 0 :(得分:2)
不要引用参数:)
string GetSerials = "SELECT SerialNumber from Warranty";
string TestUpdateDates = "UPDATE Warranty SET StartDate = @StartDate, EndDate = @EndDate WHERE SerialNumber = @result";
//string TestUpdateDates2 = "UPDATE Warranty SET StartDate = cDate(Format(@StartDate, 'MM/dd/yyyy')), EndDate = cDate(Format(@EndDate, 'MM/dd/yyyy')) WHERE(SerialNumber = @result)";
DataTable dataTable = new DataTable();
using (var conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Blah\Blah\Blah\Warranty.accdb"))
using (OleDbCommand serialCommand = new OleDbCommand(GetSerials, conn1))
{
conn1.Open();
dataTable.Load(serialCommand.ExecuteReader());
foreach (DataRow row in dataTable.Rows)
{
var result = row["SerialNumber"].ToString();
WebRequest request = WebRequest.Create("urlpart1" + result + "urlpart2");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using (var streamr = new StreamReader(response.GetResponseStream()))
using (OleDbCommand testupdateCommand = new OleDbCommand(TestUpdateDates, conn1))
using (OleDbCommand updateCommand = new OleDbCommand(UpdateDates, conn1))
using (OleDbCommand deleteCommand = new OleDbCommand(DeleteIncomplete, conn1))
{
json = streamr.ReadToEnd();
List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json);
MyObject obj = list[0]; //Base Warranty
// obj -- Base Warranty
var StartDate = obj.Start;
var EndDate = obj.End;
//testupdateCommand.Parameters.Add("@StartDate", OleDbType.Date).Value = StartDate;
//testupdateCommand.Parameters.Add("@EndDate", OleDbType.Date).Value = EndDate;
testupdateCommand.Parameters.AddWithValue("@StartDate", StartDate);
testupdateCommand.Parameters.AddWithValue("@EndDate", EndDate);
testupdateCommand.Parameters.AddWithValue("@result", result);