您的位置:首页 > 数据库

【linq to sql】步骤

2016-04-26 15:43 148 查看
1、新建项目--> 类库   假设命名为A

2、在 A 下 添加新项--> linq to sql class   假设命名为 aa.dbml  此时会看到一个设计页面

3、服务器资源管理器-->新建连接-->添加你的数据库连接

4、把需要的表一个个拖到 设计页面,然后 Ctrl + S 保存

5、生成或重新生成,此时bin目录下会有个 dll 文件

6、在你要使用linq to sql 的项目中引用这个类库然后就可以CRUD了

=========================================================下面是个小小的示例,只有插入操作=======================

using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System;
using Newtonsoft.Json;
using System.Drawing;
using System.Drawing.Imaging;
using Newtonsoft.Json.Linq;
using ConsoleApplication4.com.fengjing.travel;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using LinqToSqlDemo.Orm;
/* http://travel.fengjing.com/HolidaySvc.asmx {"RCode":"fengjing@#*!2014","ClientType":"1","Module":"scenic","Method":"GetScenicPicList","Data":"11749"} 图片
{"RCode":"fengjing@#*!2014","ClientType":"1","Module":"scenic","Method":"GetScenicLBS","Data":{"scenicId":"11749"}} 坐标
{"RCode":"fengjing@#*!2014","ClientType":"1","Module":"scenic","Method":"getscenicofvoice","Data":{"scenicId":"11749","userId":""}} 景区+所有子景点列表
{"RCode":"fengjing@#*!2014","ClientType":"1","Module":"scenic","Method":"getsceniclistofcity","Data":{"PageIndex":3,"PageSize":20,"QueryDict":{"cityname":"无锡","voice":"1"}}} 所有景区第3页
{"RCode":"fengjing@#*!2014","ClientType":"1","Module":"scenic","Method":"getsceniclistofcity","Data":{"PageIndex":1,"PageSize":20,"QueryDict":{"cityname":"无锡","voice":"1"}}} 所有景区第1页
{"RCode":"fengjing@#*!2014","ClientType":0,"Module":"SmartTour","Method":"GetRegionList","Data":{"RegionKey":""}} 所有城市
*/
class City
{
public string Initial { get; set; }
public string Code { get; set; }
public string Parent { get; set; }
public string Name { get; set; }
public string IsSmartTourCity { get; set; }
}
class Scenic
{
public string ScenicId { get; set; }
public string SName { get; set; }
public string PriceNotes { get; set; }
public string City { get; set; }
public string County { get; set; }
public string Level { get; set; }
public string ScenicMes { get; set; }
public string ScenicPic { get; set; }
public string ListionVoiceCount { get; set; }
public string TicketCounT { get; set; }
public string SalePrice { get; set; }
public string voice_Picture { get; set; }
public string isVoice { get; set; }
public string isPiao { get; set; }
public string lon { get; set; }
public string lat { get; set; }
public string Address { get; set; }
public string Score { get; set; }
public string CommentCount { get; set; }
public string CommentList { get; set; }
public string FavoriteCountOfPiao { get; set; }
public string IsFavoriteOfPiao { get; set; }
public string FavoriteCountOfVoice { get; set; }
public string IsFavoriteOfVoice { get; set; }
public string VoiceFile { get; set; }
public string scenicDistance { get; set; }
public string PicCount { get; set; }
public string LocalVoiceCount { get; set; }
public string LocalVoiceTypeCount { get; set; }
}
class Scenic1
{
public string ScenicId { get; set; }
public string SName { get; set; }
public string PriceNotes { get; set; }
public string City { get; set; }
public string County { get; set; }
public string Level { get; set; }
public string ScenicMes { get; set; }
public string ScenicPic { get; set; }
public string ListionVoiceCount { get; set; }
public string TicketCounT { get; set; }
public string SalePrice { get; set; }
public string voice_Picture { get; set; }
public string isVoice { get; set; }
public string isPiao { get; set; }
public string lon { get; set; }
public string lat { get; set; }
public string mapPath { get; set; }
public string Address { get; set; }
public string Score { get; set; }
public string FavoriteCountOfVoice { get; set; }
public string IsFavoriteOfVoice { get; set; }
public string IsScenicTrfface { get; set; }
public string IsScenicNews { get; set; }
public string IsScenicCustom { get; set; }
public string IsScenicFood { get; set; }
public string scType { get; set; }
public string Traffic { get; set; }
public string PicCount { get; set; }
}
class ScenicChild
{
public string ScenicId { get; set; }
public string SpotId { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public string InertTimeStr { get; set; }
public string SummaryTxt { get; set; }
public string ImgPath { get; set; }
public string SpotName { get; set; }
public string FileExt { get; set; }
public string FileNameShort { get; set; }
public string localvoicelist { get; set; }
}
class Spot
{
public string Title { get; set; }
public string XCoord { get; set; }
public string YCoord { get; set; }
public string ScenicId { get; set; }
public string SpotId { get; set; }
public string SpotName { get; set; }
}

namespace ConsoleApplication2
{
class Program
{
private static DataClassesDataContext dataContext = new DataClassesDataContext();
public static HolidaySvc svc = new HolidaySvc();

public static int CityCount; //城市个数
public static int ScenicCount; //每个城市景点个数
public static int ScenicChildCount; //每个景点语音个数
public static int ImgUrlCount; //图片个数
public static int SpotCount; //现场个数
public static City[] city = new City[500];
public static Scenic1 scenic1 = new Scenic1();
public static ScenicChild[] scenicchild = new ScenicChild[100];
public static string[] url = new string[50];
public static Spot[] spot = new Spot[100];
public static void getCityArray()
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":0,\"Module\":\"SmartTour\",\"Method\":\"GetRegionList\",\"Data\":{\"RegionKey\":\"\"}}";
try
{
string ss = svc.RequestServiceData(senddata);
ss = "[" + ss + "]";
JArray jsonObj = JArray.Parse(ss);
JObject j = (JObject)jsonObj.First;
j = (JObject)j["Data"];
jsonObj = (JArray)j["Citys"];

foreach (JObject jObject in jsonObj)
{
city[CityCount] = new City();
city[CityCount].Initial = jObject["Initial"].ToString();
city[CityCount].Code = jObject["Code"].ToString();
city[CityCount].Parent = jObject["Parent"].ToString();
city[CityCount].Name = jObject["Name"].ToString();
city[CityCount].IsSmartTourCity = jObject["IsSmartTourCity"].ToString();
CityCount++;
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("获取城市超时");
Console.ForegroundColor = ConsoleColor.Green;
}
}
public static void CreateScenicArray(string city)
{
List<Scenic> scenic = new List<Scenic>();
ScenicCount = 0;
for (int i = 1; i < 50; i++)
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":\"1\",\"Module\":\"scenic\",\"Method\":\"getsceniclistofcity\",\"Data\":{\"PageIndex\":" + i.ToString() + ",\"PageSize\":20,\"QueryDict\":{\"cityname\":\"" + city + "\",\"voice\":\"1\"}}}";
try
{
string s = svc.RequestServiceData(senddata);
s = "[" + s + "]";
JArray jsonObj = JArray.Parse(s);
JObject j = (JObject)jsonObj.First;
string f = j["Data"].ToString();
if (f == "")
{
break;
}
jsonObj = (JArray)j["Data"];
foreach (JObject jObject in jsonObj)
{
var sc = new Scenic();
sc.ScenicId = jObject["ScenicId"].ToString();
sc.SName = jObject["SName"].ToString();
sc.PriceNotes = jObject["PriceNotes"].ToString();
sc.City = jObject["City"].ToString();
sc.County = jObject["County"].ToString();
sc.Level = jObject["Level"].ToString();
sc.ScenicMes = jObject["ScenicMes"].ToString();
sc.ScenicPic = jObject["ScenicPic"].ToString();
sc.ListionVoiceCount = jObject["ListionVoiceCount"].ToString();
sc.TicketCounT = jObject["TicketCounT"].ToString();
sc.SalePrice = jObject["SalePrice"].ToString();
sc.voice_Picture = jObject["voice_Picture"].ToString();
sc.isVoice = jObject["isVoice"].ToString();
sc.isPiao = jObject["isPiao"].ToString();
sc.lon = jObject["lon"].ToString();
sc.lat = jObject["lat"].ToString();
sc.Address = jObject["Address"].ToString();
sc.Score = jObject["Score"].ToString();
sc.CommentCount = jObject["CommentCount"].ToString();
sc.CommentList = jObject["CommentList"].ToString();
sc.FavoriteCountOfPiao = jObject["FavoriteCountOfPiao"].ToString();
sc.IsFavoriteOfPiao = jObject["IsFavoriteOfPiao"].ToString();
sc.FavoriteCountOfVoice = jObject["FavoriteCountOfVoice"].ToString();
sc.IsFavoriteOfVoice = jObject["IsFavoriteOfVoice"].ToString();
sc.VoiceFile = jObject["VoiceFile"].ToString();
sc.scenicDistance = jObject["scenicDistance"].ToString();
sc.PicCount = jObject["PicCount"].ToString();
sc.LocalVoiceCount = jObject["LocalVoiceCount"].ToString();
sc.LocalVoiceTypeCount = jObject["LocalVoiceTypeCount"].ToString();

scenic.Add(sc);
ScenicCount++;
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("获取景点超时");
Console.ForegroundColor = ConsoleColor.Green;
}
}
if (scenic.Count < 3)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(city + " 城市无景点");
Console.ForegroundColor = ConsoleColor.Green;
return;
}
foreach (var sc in scenic.Skip(2).ToList())
{
try
{
LinqToSqlDemo.Orm.Scenic ss = new LinqToSqlDemo.Orm.Scenic()
{
ScenicId = sc.ScenicId,
SName = sc.SName,
PriceNotes = sc.PriceNotes,
City = sc.City,
County = sc.County,
Level1 = sc.Level,
ScenicMes = sc.ScenicMes,
ScenicPic = sc.ScenicPic,
ListionVoiceCount = sc.ListionVoiceCount,
TicketCounT = sc.TicketCounT,
SalePrice = sc.SalePrice,
voice_Picture = sc.voice_Picture,
isVoice = sc.isVoice,
isPiao = sc.isPiao,
lon = sc.lon,
lat = sc.lat,
Address = sc.Address,
Score = sc.Score,
CommentCount = sc.CommentCount,
CommentList = sc.CommentList,
FavoriteCountOfPiao = sc.FavoriteCountOfPiao,
IsFavoriteOfPiao = sc.IsFavoriteOfPiao,
FavoriteCountOfVoice = sc.FavoriteCountOfVoice,
IsFavoriteOfVoice = sc.IsFavoriteOfVoice,
VoiceFile = sc.VoiceFile,
scenicDistance = sc.scenicDistance,
PicCount = sc.PicCount,
LocalVoiceCount = sc.LocalVoiceCount,
LocalVoiceTypeCount = sc.LocalVoiceTypeCount
};
dataContext.Scenic.InsertOnSubmit(ss);
dataContext.SubmitChanges();
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message + "-----" + city + " 城市有景点但出异常");
Console.ForegroundColor = ConsoleColor.Green;
}

try
{
//CreateScenic1(sc.ScenicId);
CreateScenic1Child(sc.ScenicId);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message + "CreateScenic1错误");
Console.ForegroundColor = ConsoleColor.Green;
}

try
{
CreateImg(sc.ScenicId);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message + "CreateImg错误");
Console.ForegroundColor = ConsoleColor.Green;
}

try
{
CreateSpot(sc.ScenicId);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message + "CreateSpot错误");
Console.ForegroundColor = ConsoleColor.Green;
}
}
}
public static void CreateScenic1(string id)
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":\"1\",\"Module\":\"scenic\",\"Method\":\"getscenicofvoice\",\"Data\":{\"scenicId\":\"" + id + "\",\"userId\":\"\"}}";
string s = svc.RequestServiceData(senddata);
s = "[" + s + "]";
JArray jsonObj = JArray.Parse(s);
JObject j = (JObject)jsonObj.First;
j = (JObject)j["Data"];
scenic1.ScenicId = j["ScenicId"].ToString();
scenic1.SName = j["SName"].ToString();
scenic1.PriceNotes = j["PriceNotes"].ToString();
scenic1.City = j["City"].ToString();
scenic1.County = j["County"].ToString();
scenic1.Level = j["Level"].ToString();
scenic1.ScenicMes = j["ScenicMes"].ToString();
scenic1.ScenicPic = j["ScenicPic"].ToString();
scenic1.ListionVoiceCount = j["ListionVoiceCount"].ToString();
scenic1.TicketCounT = j["TicketCounT"].ToString();
scenic1.SalePrice = j["SalePrice"].ToString();
scenic1.voice_Picture = j["voice_Picture"].ToString();
scenic1.isVoice = j["isVoice"].ToString();
scenic1.isPiao = j["isPiao"].ToString();
scenic1.lon = j["lon"].ToString();
scenic1.lat = j["lat"].ToString();
scenic1.mapPath = j["mapPath"].ToString();
scenic1.Address = j["Address"].ToString();
scenic1.Score = j["Score"].ToString();
scenic1.FavoriteCountOfVoice = j["FavoriteCountOfVoice"].ToString();
scenic1.IsFavoriteOfVoice = j["IsFavoriteOfVoice"].ToString();
scenic1.IsScenicTrfface = j["IsScenicTrfface"].ToString();
scenic1.IsScenicNews = j["IsScenicNews"].ToString();
scenic1.IsScenicCustom = j["IsScenicCustom"].ToString();
scenic1.IsScenicFood = j["IsScenicFood"].ToString();
scenic1.scType = j["scType"].ToString();
scenic1.Traffic = j["Traffic"].ToString();
scenic1.PicCount = j["PicCount"].ToString();
}
public static void CreateScenic1Child(string id)
{
List<ScenicChild> lsc = new List<ScenicChild>();
try
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":\"1\",\"Module\":\"scenic\",\"Method\":\"getscenicofvoice\",\"Data\":{\"scenicId\":\"" + id + "\",\"userId\":\"\"}}";
string s = svc.RequestServiceData(senddata);
s = "[" + s + "]";
JArray jsonObj = JArray.Parse(s);
JObject j = (JObject)jsonObj.First;
j = (JObject)j["Data"];
jsonObj = (JArray)j["spotlist"];
foreach (JObject jObject in jsonObj)
{
var sc = new ScenicChild();
sc.ScenicId = jObject["ScenicId"].ToString();
sc.SpotId = jObject["SpotId"].ToString();
sc.FileName = jObject["FileName"].ToString();
sc.FileSize = jObject["FileSize"].ToString();
sc.InertTimeStr = jObject["InertTimeStr"].ToString();
sc.SummaryTxt = jObject["SummaryTxt"].ToString();
sc.ImgPath = jObject["ImgPath"].ToString();
sc.SpotName = jObject["SpotName"].ToString();
sc.FileExt = jObject["FileExt"].ToString();
sc.FileNameShort = jObject["FileNameShort"].ToString();
sc.localvoicelist = jObject["localvoicelist"].ToString();

lsc.Add(sc);
ScenicChildCount++;
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("声音获取超时!");
Console.ForegroundColor = ConsoleColor.Green;
}
if (lsc.Count < 1)
{
return;
}
try
{
foreach(var sc in lsc)
{
ScenicVoice ss = new ScenicVoice()
{
ScenicId = sc.ScenicId,
SpotId = sc.SpotId,
FileName = sc.FileName,
FileSize = sc.FileSize,
InertTimeStr = sc.InertTimeStr,
SummaryTxt = sc.SummaryTxt,
ImgPath = sc.ImgPath,
SpotName = sc.SpotName,
FileExt = sc.FileExt,
FileNameShort = sc.FileNameShort,
localvoicelist = sc.localvoicelist
};
dataContext.ScenicVoice.InsertOnSubmit(ss);
dataContext.SubmitChanges();
}
}
catch(Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message + "-----" + "声音写入错误!");
Console.ForegroundColor = ConsoleColor.Green;
}
}
public static void CreateImg(string id)
{
ImgUrlCount = 0;
try
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":\"1\",\"Module\":\"scenic\",\"Method\":\"GetScenicPicList\",\"Data\":\"" + id + "\"}";
string s = svc.RequestServiceData(senddata);
s = "[" + s + "]";
JArray jsonObj = JArray.Parse(s);
JObject j = (JObject)jsonObj.First;
jsonObj = (JArray)j["Data"];
foreach (JObject jObject in jsonObj)
{
url[ImgUrlCount] = jObject["url"].ToString();
ImgUrlCount++;
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("图片获取超时!");
Console.ForegroundColor = ConsoleColor.Green;
}
if (ImgUrlCount<1)
{
return;
}

for (int i = 0; i < ImgUrlCount; i++)
{
try
{
Imgs I = new Imgs()
{
cityid = id,
url = url[i],
};
dataContext.Imgs.InsertOnSubmit(I);
dataContext.SubmitChanges();
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("图片URL写入失败");
Console.ForegroundColor = ConsoleColor.Green;
}
}
}
public static void CreateSpot(string id)
{
SpotCount = 0;
try
{
string senddata = "{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":\"1\",\"Module\":\"scenic\",\"Method\":\"GetScenicLBS\",\"Data\":{\"scenicId\":\"" + id + "\"}}";
string s = svc.RequestServiceData(senddata);
s = "[" + s + "]";
JArray jsonObj = JArray.Parse(s);
JObject j = (JObject)jsonObj.First;
jsonObj = (JArray)j["Data"];
foreach (JObject jObject in jsonObj)
{
spot[SpotCount] = new Spot();
spot[SpotCount].Title = jObject["Title"].ToString();
spot[SpotCount].XCoord = jObject["XCoord"].ToString();
spot[SpotCount].YCoord = jObject["YCoord"].ToString();
spot[SpotCount].ScenicId = jObject["ScenicId"].ToString();
spot[SpotCount].SpotId = jObject["SpotId"].ToString();
spot[SpotCount].SpotName = jObject["SpotName"].ToString();
SpotCount++;
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("景点坐标获取超时!");
Console.ForegroundColor = ConsoleColor.Green;
}
if (SpotCount < 1)
{
return;
}
for (int i = 0; i < SpotCount; i++)
{
try
{
LinqToSqlDemo.Orm.Spot sp = new LinqToSqlDemo.Orm.Spot()
{
Title = spot[i].Title,
XCoord = spot[i].XCoord,
YCoord = spot[i].YCoord,
ScenicId = spot[i].ScenicId,
SpotId = spot[i].SpotId,
SpotName = spot[i].SpotName,
};
dataContext.Spot.InsertOnSubmit(sp);
dataContext.SubmitChanges();
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("景点坐标写入失败!");
Console.ForegroundColor = ConsoleColor.Green;
}
}
}
public static void downloadPicture(string url, string path)
{
try
{
Uri uri = new Uri(url);
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
Image image = Image.FromStream(s);
image.Save(path, ImageFormat.Jpeg);//绝对路径
}
catch (Exception e)
{
Console.WriteLine(e.Message + "图片不存在");
}

}
public static void downloadSound(string url, string path)
{
try
{
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//接收响应

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//输出流
Stream responseStream = response.GetResponseStream();

using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
byte[] bufferBytes = new byte[10000];//缓冲字节数组
int bytesRead = -1;
while ((bytesRead = responseStream.Read(bufferBytes, 0, bufferBytes.Length)) > 0)
{
fs.Write(bufferBytes, 0, bytesRead);
}
//关闭写入
fs.Flush();
fs.Close();

}
}
catch (Exception e)
{
Console.WriteLine(e.Message + ",文件不存在");
}
}
public static void CreateALL()
{
List<string> ll = new List<string>();

getCityArray();

for (int i = 0; i < CityCount; i++)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(city[i].Name + " 数据正在写入...");
try
{
LinqToSqlDemo.Orm.City c = new LinqToSqlDemo.Orm.City()
{
Initial = city[i].Initial,
Code = city[i].Code,
Parent = city[i].Parent,
Name = city[i].Name,
IsSmartTourCity = city[i].IsSmartTourCity,
};
dataContext.City.InsertOnSubmit(c);
dataContext.SubmitChanges();
}
catch
{
Console.WriteLine(city[i].Name + " 城市写入错误!");
}

CreateScenicArray(city[i].Name.Replace("市", ""));

Console.WriteLine(city[i].Name + " 数据已写入!");
}
}
static void Main(string[] args)
{
CreateALL();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# linq to sql