C# winform 数据库操做知识点总结(干货)

一、数据库链接及操做java

  在说数据库操做以前,先说一下数据库链接操做字符串的获取sql

  首先,点击服务器资源管理器,接下来选中数据链接右键点击添加链接,填入你要链接的服务器名称,点击单选框使用SQL Server 身份验证,填入用户名和密码,而后选择你要连接的数据库名称,点击测试链接,弹出链接成功,而后点击肯定,此时,数据库已经链接成功。在服务器资源管理器下的数据链接下就能够看到你刚才连接的数据库,选中右键属性你就能够看见链接字符串数据库

  在获取到数据库链接字符串时,在App.config配置文件中能够写关于链接数据库的链接字符串,在这里配置好,在程序中直接经过代码调用链接字符串就行,直接上代码c#

1   <connectionStrings>
2     <add name="connStr" connectionString="Data Source=(local);Initial Catalog=train;User ID=sa;Password=1234" />
3     <add name="train.Properties.Settings.trainConnectionString" connectionString="Data Source=(local);Initial Catalog=train;Persist Security Info=True;User ID=sa"
4       providerName="System.Data.SqlClient" />
5   </connectionStrings>

 

  在数据库链接时经过获取App.confi文件中的链接字符串链接数据库,代码以下服务器

    /// <summary>
    /// 数据库链接
    /// </summary>
    public class SqlConnect
    {
       /// <summary>
       /// 链接字符串获取
       /// </summary>
        private static string connectString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

        /// <summary>
        /// 创建数据库链接
        /// </summary>
        /// <returns></returns>
        public static SqlConnection getConn()
        {
            SqlConnection con = new SqlConnection(connectString);
            
            //Console.WriteLine("链接成功");
            return con;
        }
    }

  

  接下来讲一说数据库增删查改相关的操做ide

  经过c#操做数据库增长一条数据和java的思路逻辑差很少,只不过在在传值赋值的过程当中采用的方式不同,Java中sql语句用问号赋值传值,而c#是经过@加变量名传值,经过测试

AddWithValue方法赋值。是否是听得云里雾里的,给你们附上一段代码,就能够懂我说的spa

 1         /// <summary>
 2         /// 插入站点
 3         /// </summary>
 4         /// <param name="stationName"></param>站点名称
 5         /// <param name="stationEnName"></param>站点英文名
 6         /// <param name="stationLng"></param>站点经度
 7         /// <param name="stationLat"></param>站点纬度
 8         /// <param name="stopTime"></param>停留时间
 9         /// <param name="distance"></param>距离
10         /// <param name="lastStation"></param>上一站点名称
11         /// <param name="belongStation"></param>在本线路中的隶属站次
12         public void insertStation(String stationName, String stationEnName, double stationLng, double stationLat, int stopTime, double distance, String lastStation,int belongStation)
13         {
14             SqlConnection con = SqlConnect.getConn();
15             try
16             {
17                 String InsertStr = "insert into WorkingLine (StationName,StationEnName,StationLng,StationLat,StationStopTime,StationDistance,LastStationName,SubjectStation) values (@STATIONNAME,@STATIONENNAME,@STATIONLNG,@STATIONLAT,@STATIONSTOPTIME,@STATIONDISTANCE,@LASTSTATIONNAME,@SUBJECTSTATION)";
18                 SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
19                 command.CommandText = InsertStr;
20                 con.Open();
21                 command.Parameters.AddWithValue("@STATIONNAME", stationName);
22                 command.Parameters.AddWithValue("@STATIONENNAME", stationEnName); ;
23                 command.Parameters.AddWithValue("@STATIONLNG", stationLng);
24                 command.Parameters.AddWithValue("@STATIONLAT", stationLat);
25                 command.Parameters.AddWithValue("@STATIONSTOPTIME", stopTime);
26                 command.Parameters.AddWithValue("@STATIONDISTANCE", distance);
27                 command.Parameters.AddWithValue("@LASTSTATIONNAME", lastStation);
28                 command.Parameters.AddWithValue("@SUBJECTSTATION", belongStation);
29                 command.ExecuteNonQuery();
30             }
31             catch (Exception ex)
32             {
33                 Console.WriteLine(ex.Message);
34                 Console.WriteLine("添加站点失败");
35             }
36             finally
37             {
38                 con.Close();
39             }
40         }        

 

  删除,修改一条数据相对来讲没有那么复杂,直接上代码code

 1          /// <summary>
 2         /// 删除站点
 3         /// </summary>
 4         /// <param name="name"></param>站点名称
 5         public void deleteSta(String name) {
 6             SqlConnection con = SqlConnect.getConn();
 7             try
 8             {
 9                 String deleteStr = "delete from WorkingLine where StationName=@STATIONNAME";
10                 SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
11                 command.CommandText = deleteStr;
12                 con.Open();
13                 command.Parameters.AddWithValue("@STATIONNAME", name);
14                 command.ExecuteNonQuery();
15             }
16             catch (Exception ex)
17             {
18                 Console.WriteLine(ex.Message);
19                 Console.WriteLine("删除站点失败");
20             }
21             finally
22             {
23                 con.Close();
24             }
25         }
26 
27         /// <summary>
28         /// 修改某一站距上一站的距离
29         /// </summary>
30         /// <param name="stationname"></param>站名
31         /// <param name="distance"></param>距离
32         public void UpdateDistance(String stationname,double distance) {
33             SqlConnection con = SqlConnect.getConn();
34             try
35             {
36                 String updateDisStr = "update WorkingLine set StationDistance =@DISTANCE where StationName =@STATIONNAME";
37                 SqlCommand commmand = con.CreateCommand();// 绑定SqlConnection对象
38                 commmand.CommandText = updateDisStr;
39                 commmand.Parameters.AddWithValue("@DISTANCE", distance);
40                 commmand.Parameters.AddWithValue("@STATIONNAME", stationname);
41                 con.Open();
42                 commmand.ExecuteNonQuery();//执行命令 
43 
44             }
45             catch (Exception ex)
46             {
47                 Console.WriteLine(ex.Message);
48                 Console.WriteLine("修改距离失败");
49             }
50             finally
51             {
52                 con.Close();
53             }
54         }

 

  对于查询数据来讲,咱们广泛的操做就是用泛型List<E>来接收查询的数据,看代码就明白了对象

 1         /// <summary>
 2         /// 查询列车运行线路信息,为DateGridView绑定数据源
 3         /// </summary>
 4         /// <returns></returns>
 5         public List<DateView> SelectGridViewStation()
 6         {
 7             SqlDataReader reader = null;
 8             DateView view = null;
 9             List<DateView> list = new List<DateView>();
10             SqlConnection con = SqlConnect.getConn();
11             try
12             {
13                 String selectGVStr = "select StationName,StationEnName,StationStopTime,StationLng,StationLat,StationDistance from WorkingLine order by SubjectStation asc";
14                 SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
15                 command.CommandText = selectGVStr;
16                 con.Open();
17                 reader = command.ExecuteReader();
18                 while (reader.Read())
19                 {
20                     view = new DateView()
21                     {
22                         StationName = reader.GetString(0),
23                         StationEnName = reader.GetString(1),
24                         stopTime=reader.GetInt32(2),
25                         lng=reader.GetDouble(3),
26                         lat = reader.GetDouble(4),
27                         distance=reader.GetDouble(5)
28                     };
29                     list.Add(view);
30                 }
31             }
32             catch (Exception ex)
33             {
34                 Console.WriteLine(ex.Message);
35                 Console.WriteLine("查询线路信息失败");
36             }
37             finally
38             {
39                 reader.Close();
40                 con.Close();
41             }
42             return list;
43         }

 

  如今拿到数据了,咱们怎么显示在对应的界面上呢!在winfrom窗体程序中,我习惯了用DataGridView控件,只须要为其绑定数据源就行了,我说的数据源是经过代码去实现的,请看

  1 SelectStation selSta = new SelectStation(); 2 listDV = selSta.SelectGridViewStation(); 3 dataGridView1.DataSource = listDV; 

二、DataGridView操做

  DataGridView控件默认选中第一行数据,若是不想让其选中,只需一步:dataGridView1.Rows[1].Selected = false;

  选取DataGridView控件中某一行某一列的值:dataGridView1.Rows[m ].Cells[n].Value.ToString();其中m,n分别表示行和列

相关文章
相关标签/搜索