A:取出数据表里的数据
单个数据:
using System.Data.SqlClient;
using(SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa;Database=Northwind"))
{
con.Open();
string strsql6 = "select count(sno) from student where cno='" + class1 + "' and cet4>425";
SqlCommand cmd = new SqlCommand(strsql6,con);
this.TextBox1.Text = cmd.ExecuteScalar().ToString();
}
多字段的话,要参考ExecuteReader或者用SqlAdapter的fill方法,装到DataTable里面,然后写for循环,table.Rows[i]["字段名"].ToString()可以读取字段的值
B:为取出数据根据桩号画点画线。泛型的使用
#region 显示测线
private void View_Btn_Click(object sender, EventArgs e)
{
// main.view();
//先循环每一条线
for (int i = 0; i < Describ_dataGridView.Rows.Count; i++)
{
string lineNa = Convert.ToString(Describ_dataGridView.Rows[i].Cells[1].Value);
//画线
this.newLine(lineNa);
//画点
this.newPoint(lineNa);
}
this.Hide();
}
#endregion
#region 画点
private void newPoint(string lineNa)
{
FormMain main = (FormMain)this.Owner;
List<CPoint> list = getPoints(lineNa);
for (int i = 0; i < list.Count; i++)
{
double XPoint = list[i].lng;//从表中取数据
double YPoint = list[i].lat;
IActiveView pActiveView = main.axMapControl1.ActiveView;
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.PutCoords(XPoint, YPoint);
IMarkerElement pMarkerElement;
pMarkerElement = new MarkerElementClass();
ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbol();
pMarkerSymbol.Size = 6;
IElement pElement;
pElement = pMarkerElement as IElement;
pElement.Geometry = pPoint;
pMarkerElement.Symbol = pMarkerSymbol;
//建立文字符号对象,并设置相应的属性
ITextElement pTextEle;
IElement pEles;
pTextEle = new TextElementClass();
pTextEle.Text = "[" + XPoint + "," + YPoint + "]";
//symbol
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Color = GetRGBColor(255, 0, 0);
pTextSymbol.Size = 10;
pTextEle.Symbol = pTextSymbol;
pEles = pTextEle as IElement;
pEles.Geometry = pPoint;
IGraphicsContainer pGraphicsContainer = main.axMapControl1.ActiveView as IGraphicsContainer;
pGraphicsContainer.AddElement((IElement)pEles, 0);
pGraphicsContainer.AddElement((IElement)pMarkerElement, 0);
pActiveView.Refresh();
}
}
#endregion
#region 添加点元素
private void axMapControl1Point(string lineNa)
{
FormMain main = (FormMain)this.Owner;
List<CPoint> list = getPoints(lineNa);
for (int i = 0; i < list.Count; i++)
{
double XPoint = list[i].lng;
double YPoint = list[i].lat;
IMap pMap = main.axMapControl1.Map;
IActiveView activeView = main.axMapControl1.ActiveView;
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.PutCoords(XPoint, YPoint);
IMarkerElement pMarkerElement = new MarkerElementClass();
ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbol();
pMarkerSymbol.Color = GetRGBColor(11, 200, 145);
pMarkerSymbol.Size = 20;
pMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
IElement pElement = (IElement)pMarkerElement;
pElement.Geometry = pPoint;
pMarkerElement.Symbol = pMarkerSymbol;
IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)pMap;
pGraphicsContainer.AddElement((IElement)pMarkerElement, 0);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
#endregion
#region 取点
private List<CPoint> getPoints(string lineNa)
{
DataBaseConnection connection = new DataBaseConnection();
OleDbConnection conn = connection.getConnection();
//创建命令
//OleDbCommand cmd = conn.CreateCommand();
List<CPoint> pList = new List<CPoint>();
OleDbDataAdapter da = new OleDbDataAdapter("select * from point where 测线名称='" + lineNa + "'", conn);
//建立适配器,通过SQL语句去搜索数据库
DataSet ds = new DataSet();
//建立数据集
da.Fill(ds, "point");
//用FILL的方式将适配器已经连接好的数据表填充到数据集MYDS这张表 //用显示控件来显示表
foreach (DataRow var in ds.Tables["point"].Rows) //set.Tables[0].Rows 找到指定表的所有行 0这里可以填表名
{
CPoint cp = new CPoint();
// cp.lineN = var[1].ToString();
cp.pileN = Convert.ToDouble(var[2]);
cp.lng = Convert.ToDouble(var[3]);
cp.lat = Convert.ToDouble(var[4]);
cp.count = ds.Tables["point"].Rows.Count;
pList.Add(cp);
}
return pList;
}
#endregion
#region 画线
private List<CPoint> newLine(string lineNa)
{
FormMain main = (FormMain)this.Owner;
List<CPoint> list = getPoints(lineNa);
for (int i = 0; i < list.Count - 1; i++)
{
double XPoint = list[i].lng;
double YPoint = list[i].lat;
double XPoint2 = list[i + 1].lng;
double YPoint2 = list[i + 1].lat;
IActiveView pActiveView = main.axMapControl1.ActiveView;//定义了数据显示
//画线
IPoint lPoint1 = new ESRI.ArcGIS.Geometry.Point();
lPoint1.PutCoords(XPoint, YPoint);
IPoint lPoint2 = new ESRI.ArcGIS.Geometry.Point();
lPoint2.PutCoords(XPoint2, YPoint2);
IPolyline mpLine = CreatePolyLineByTwoPoint(lPoint1, lPoint2);
ILineElement lineElement = new LineElementClass();
ISimpleLineSymbol pSLS = new SimpleLineSymbol();
IRgbColor pColor = GetRGBColor(0, 255, 0);
pSLS.Color = pColor;
pSLS.Width = 2;
lineElement.Symbol = pSLS;
IElement lElement = lineElement as IElement;
lElement.Geometry = mpLine;
//----
IGraphicsContainer pGraphicsContainer = main.axMapControl1.ActiveView as IGraphicsContainer;
pGraphicsContainer.AddElement((IElement)lineElement, 0);
pActiveView.Refresh();
}
return list;
}
#endregion
#region 根据两点生成一条线
private IPolyline CreatePolyLineByTwoPoint(IPoint p1, IPoint p2)
{
INewLineFeedback m_LineFeed = new NewLineFeedback();
m_LineFeed.Start(p1);
m_LineFeed.AddPoint(p2);
IPolyline m_PolyLine = m_LineFeed.Stop();
return m_PolyLine;
}
#endregion
#region 调颜色
private IRgbColor GetRGBColor(int red, int green, int blue)
{
IRgbColor rGBColor = new RgbColor();
rGBColor.Red = red;
rGBColor.Green = green;
rGBColor.Blue = blue;
return rGBColor;
}
#endregion
C:泛型的使用
把数据存入泛型之后。想要调用直接遍历个数取出调用的参数名字就好。详细看上代码