C#操作数据库
C#连接数据库,并查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //连接数据库的连接符的默认必须参数:“Server=服务器地址或者服务器名(.代表本机);Database=数据库名;User ID=用户名称;Password=密码(不解释)”这四个参数是必须的。至于其他的参数自己也不是很懂。日后补充。 2 string connectionString = "Server=.;Database=CCTV;User ID=sa;Password=123456;MultipleActiveResultSets=True"; 3 SqlConnection con = new SqlConnection(connectionString); 4 //操作数据库前,打开数据库连接,这里也可以使用try catch语句捕抓数据库连接异常 5 con.Open(); 6 7 //string a = GetElename().Replace('-', '_'); 8 //string[] arr = a.Split(','); 9 //for (int i = 0; i < arr.Length - 1; i++) 10 //{ 11 12 //数据库操作语句,最常见的select insert delete update 四种语句。 13 string sqlstring = "select Shape.STX ,Shape.STY ,Shape.Z from " + arr[i] + " order by Shape.Z"; 14 //string txtpath = @"F:\shp1\"+arr[i]+".txt"; 15 //对于数据库的查询方式有多种一下是一种 16 SqlCommand sqlCommand = new SqlCommand(); 17 //为command制定数据库连接 18 sqlCommand.Connection = con; 19 //为command制定查询语句 20 sqlCommand.CommandText = sqlstring; 21 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); 22 sqlDataAdapter.SelectCommand = sqlCommand; 23 DataSet dataset = new DataSet(); 24 DataTable datatable = new DataTable(); 25 SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); 26 sqlDataAdapter.Fill(dataset, "t1"); 27 datatable = dataset.Tables[0]; 28 StringBuilder sb = new StringBuilder(); 29 sb.Append("Polyline" + Environment.NewLine); 30 for (int j = 0; j < datatable.Rows.Count-1; j++) 31 { 32 sb.Append(j.ToString()+" 0"+Environment.NewLine); 33 sb.Append("0 "+datatable.Rows[j][0]+" "+datatable.Rows[j][1]+" "+datatable.Rows[j][2]+" "+"0.0"+Environment.NewLine); 34 sb.Append("1 "+datatable.Rows[j+1][0] + " " + datatable.Rows[j+1][1] + " " + datatable.Rows[j+1][2] + " " + "0.0" + Environment.NewLine); 35 Console.Write(datatable.Rows[j][0]); 36 } 37 sb.Append("END");
通过C#读取shapefile文件可以根据一定的条件对shapefile的属性进行查询。但貌似查询完的结果不能按照我想要的要求进行排序,有些郁闷。试图通过借用.Net里的DataTable对象暂时存储查询到的结果(这个结果不包含shape字段)。如果知道如何对shapefile查询结果进行排序的更好的方法也可以留言指导一下。
但在利用DataTable的时候排序也遇到了一些问题,DataTable貌似不能直接进行排序。只要借助DataView的sort属性进行排序。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DataView dv = dt.DefaultView; dv.Sort = "c1 Asc"; DataTable dt2 = dv.ToTable(); 或者 dt.DefaultView.Sort = "Z asc"; //DataTable dtcopy = dt.DefaultView.Table.Copy(); DataTable dtsort = dt.DefaultView.ToTable();
参考:http://www.cnblogs.com/fmxyw/archive/2008/10/08/1306554.html
代码片段,获取查询的结果DataTable返回
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private DataTable GetDataBaseInfo(string whereclause) 2 { 3 string connectionString = ConfigurationManager.AppSettings["connection"]; 4 SqlConnection con = new SqlConnection(connectionString); 5 con.Open(); 6 string sqlstring = "SELECT [NAME],[ELEVATORNAME] FROM [ELEVATORNAME] WHERE NAME='" + whereclause + "'"; 7 SqlCommand sqlCommand = new SqlCommand(); 8 sqlCommand.Connection = con; 9 sqlCommand.CommandText = sqlstring; 10 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); 11 sqlDataAdapter.SelectCommand = sqlCommand; 12 DataSet dataset = new DataSet(); 13 DataTable datatable = new DataTable(); 14 SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); 15 sqlDataAdapter.Fill(dataset, "t1"); 16 datatable = dataset.Tables[0]; 17 return datatable; 18 }