insert into table1(LearnerTelephones_ID,LearnerTelephones_IsFirst,LearnerTelephones_L_ID , LearnerTelephones_Ln_IDNAME,
LearnerTelephones_Ln_IDAdd,LearnerTelephones_Notes,LearnerTelephones_TelephoneNumber,LearnerTelephones_UseForText)
values(,,,,,,..)
我的数据表如上所述,它有破坏行 - startrow和endrow。 我想从这个数据表中创建插入语句, 对于前者
{{1}}
所以我需要创建4个插入语句。
答案 0 :(得分:0)
您应该首先编写一些对反序列化json字符串有用的类。
class LearnerTelephones
{
public string ID { get; set; }
public bool IsFirstPointOfContact { get; set; }
public LocationType LocationType { get; set; }
public string Notes { get; set; }
public string TelephoneNumber { get; set; }
public bool UseForTextMessages { get; set; }
}
class LocationType
{
public string ID { get; set; }
public IList<NameType> NameType { get; set; }
}
class NameType
{
public string IDNAME { get; set; }
public string IDAdd { get; set; }
}
class RootObject
{
public LearnerTelephones LearnerTelephones { get; set; }
}
然后将newton.json包安装到项目中(如果未安装)。 newton.json package link
然后,您可以轻松地将json字符串解析和反序列化为c#对象 并根据您的要求进行迭代,并可以创建插入语句。
class Program
{
static void Main(string[] args)
{
string json = "{\"LearnerTelephones\":{ \"ID\": \"eaf32af9-07bf-4d87-8aeb-84ac8860dee9\", \"IsFirstPointOfContact\": true, \"LocationType\": { \"ID\": \"ebbbaa62-f1f0-4010-8b0b-ee744eafc1de\", \"NameType\":[{ \"IDNAME\": \"my name\", \"IDAdd\": \"my id address\" }, { \"IDNAME\": \"my name2\", \"IDAdd\": \"my id address2\" } ] }, \"Notes\": null, \"TelephoneNumber\": \"028 3282 3717\", \"UseForTextMessages\": true } }";
RootObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
string sql = string.Empty;
if (obj != null)
{
for (int i = 0; i < obj.LearnerTelephones.LocationType.NameType.Count; i++)
{
var obj1 = obj.LearnerTelephones;
sql += string.Format("insert into table1(LearnerTelephones_ID,LearnerTelephones_IsFirst,LearnerTelephones_L_ID , LearnerTelephones_Ln_IDNAME, LearnerTelephones_Ln_IDAdd,LearnerTelephones_Notes,LearnerTelephones_TelephoneNumber,LearnerTelephones_UseForText) values " +
"('{0}','{1}','{2}','{3}','{4}','{5}','{6}');", obj1.ID, obj1.IsFirstPointOfContact, obj1.LocationType.NameType[i].IDNAME, obj1.LocationType.NameType[i].IDAdd, obj1.Notes, obj1.TelephoneNumber, obj1.UseForTextMessages);
}
}
Console.WriteLine(sql);
Console.Read();
}
}
我想这会很有用。并根据您的要求更新逻辑。