1、数据库连接及操作

  在说数据库操作之前,先说一下数据库连接操作字符串的获取

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

  在获取到数据库连接字符串时,在App.config配置文件中可以写关于连接数据库的连接字符串,在这里配置好,在程序中直接通过代码调用连接字符串就行,直接上代码

   <connectionStrings>
<add name="connStr" connectionString="Data Source=(local);Initial Catalog=train;User ID=sa;Password=1234" />
<add name="train.Properties.Settings.trainConnectionString" connectionString="Data Source=(local);Initial Catalog=train;Persist Security Info=True;User ID=sa"
providerName="System.Data.SqlClient" />
</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;
}
}

  

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

  通过c#操作数据库增加一条数据和java的思路逻辑差不多,只不过在在传值赋值的过程中采用的方式不一样,Java中sql语句用问号赋值传值,而c#是通过@加变量名传值,通过

AddWithValue方法赋值。是不是听得云里雾里的,给大家附上一段代码,就可以懂我说的

         /// <summary>
/// 插入站点
/// </summary>
/// <param name="stationName"></param>站点名称
/// <param name="stationEnName"></param>站点英文名
/// <param name="stationLng"></param>站点经度
/// <param name="stationLat"></param>站点纬度
/// <param name="stopTime"></param>停留时间
/// <param name="distance"></param>距离
/// <param name="lastStation"></param>上一站点名称
/// <param name="belongStation"></param>在本线路中的隶属站次
public void insertStation(String stationName, String stationEnName, double stationLng, double stationLat, int stopTime, double distance, String lastStation,int belongStation)
{
SqlConnection con = SqlConnect.getConn();
try
{
String InsertStr = "insert into WorkingLine (StationName,StationEnName,StationLng,StationLat,StationStopTime,StationDistance,LastStationName,SubjectStation) values (@STATIONNAME,@STATIONENNAME,@STATIONLNG,@STATIONLAT,@STATIONSTOPTIME,@STATIONDISTANCE,@LASTSTATIONNAME,@SUBJECTSTATION)";
SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
command.CommandText = InsertStr;
con.Open();
command.Parameters.AddWithValue("@STATIONNAME", stationName);
command.Parameters.AddWithValue("@STATIONENNAME", stationEnName); ;
command.Parameters.AddWithValue("@STATIONLNG", stationLng);
command.Parameters.AddWithValue("@STATIONLAT", stationLat);
command.Parameters.AddWithValue("@STATIONSTOPTIME", stopTime);
command.Parameters.AddWithValue("@STATIONDISTANCE", distance);
command.Parameters.AddWithValue("@LASTSTATIONNAME", lastStation);
command.Parameters.AddWithValue("@SUBJECTSTATION", belongStation);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("添加站点失败");
}
finally
{
con.Close();
}
}

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

          /// <summary>
/// 删除站点
/// </summary>
/// <param name="name"></param>站点名称
public void deleteSta(String name) {
SqlConnection con = SqlConnect.getConn();
try
{
String deleteStr = "delete from WorkingLine where StationName=@STATIONNAME";
SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
command.CommandText = deleteStr;
con.Open();
command.Parameters.AddWithValue("@STATIONNAME", name);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("删除站点失败");
}
finally
{
con.Close();
}
} /// <summary>
/// 修改某一站距上一站的距离
/// </summary>
/// <param name="stationname"></param>站名
/// <param name="distance"></param>距离
public void UpdateDistance(String stationname,double distance) {
SqlConnection con = SqlConnect.getConn();
try
{
String updateDisStr = "update WorkingLine set StationDistance =@DISTANCE where StationName =@STATIONNAME";
SqlCommand commmand = con.CreateCommand();// 绑定SqlConnection对象
commmand.CommandText = updateDisStr;
commmand.Parameters.AddWithValue("@DISTANCE", distance);
commmand.Parameters.AddWithValue("@STATIONNAME", stationname);
con.Open();
commmand.ExecuteNonQuery();//执行命令 }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("修改距离失败");
}
finally
{
con.Close();
}
}

  对于查询数据来说,我们普遍的操作就是用泛型List<E>来接收查询的数据,看代码就明白了

         /// <summary>
/// 查询列车运行线路信息,为DateGridView绑定数据源
/// </summary>
/// <returns></returns>
public List<DateView> SelectGridViewStation()
{
SqlDataReader reader = null;
DateView view = null;
List<DateView> list = new List<DateView>();
SqlConnection con = SqlConnect.getConn();
try
{
String selectGVStr = "select StationName,StationEnName,StationStopTime,StationLng,StationLat,StationDistance from WorkingLine order by SubjectStation asc";
SqlCommand command = con.CreateCommand();// 绑定SqlConnection对象
command.CommandText = selectGVStr;
con.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
view = new DateView()
{
StationName = reader.GetString(),
StationEnName = reader.GetString(),
stopTime=reader.GetInt32(),
lng=reader.GetDouble(),
lat = reader.GetDouble(),
distance=reader.GetDouble()
};
list.Add(view);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("查询线路信息失败");
}
finally
{
reader.Close();
con.Close();
}
return list;
}

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

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

2、DataGridView操作

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

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

C# winform 程序开发知识点总结(干货)的更多相关文章

  1. WinForm程序开发

    WinForm程序开发------------------------------主要页面----------------------------------BaseForm.cs    基类,用于派 ...

  2. winform总结5> winform程序开发注意事项

    1.全局异常捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Ap ...

  3. winForm 程序开发界面参数传递

    1. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u ...

  4. Web应用程序开发知识点回顾

    asp.net 1.<%@ Page Language="C#"AutoEventWireup="true" CodeFile="Home.as ...

  5. WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  6. C#软件winform程序安装包制作及卸载程序制作

    使用vs2010 winform程序开发的软件的人比较多,程序的开发是为了在不同的人不同的机器使用,为了使不同的机器能使用该软件就需要在制作程序安装包,安装包里必须包含该软件运行所选的所有环境,下面就 ...

  7. 使用Microsoft.ExceptionMessageBox.dll捕获WinForm程序中异常信息并弹窗显示

    WinForm程序开发中,在开发模式下对于异常的处理一般都是通过调试的方式来查找异常发生的未知与原因. 下面以“除数为0”的情况来具体说明. Button按钮事件如下: private void bu ...

  8. 微信小程序开发学习资料

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  9. WinForm程序打包工具InnoSetup使用说明图文教程

    WinForm程序打包工具InnoSetup使用说明图文教程 WinForm程序开发测试好了,如果将Debug/Release里面的文件发给客户使用,会让客户觉得你不够专业,但是使用VS自带的打包工具 ...

随机推荐

  1. 我的第一个python web开发框架(9)——目录与配置说明

    和老大聊完后,小白回家接收到相关工具函数包后,就按要求开始干活,首先要做的是熟悉配置参数和了解工具函数有哪些实用的工具. 由于这个项目比较简单,所以不用创建那么多分类,只需要api.common.co ...

  2. 张高兴的 Windows 10 IoT 开发笔记:使用 ULN2003A 控制步进电机

    GitHub:https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/ULN2003A

  3. 自学 Python 3 最好的 入门 书籍 推荐(附 免费 在线阅读 下载链接)

    请大家根据自己的实际情况对号入座,挑选适合自己的 Python 入门书籍: 完全没有任何编程基础:01 号书 少量编程基础,不求全,只希望能以最快的速度入门:02 号书 少量编程基础,有一定的英文阅读 ...

  4. Ubuntu系统下的实用软件推荐

    想要在ubuntu下工作? 又担心影响效率? 这些软件可以帮助你解决问题 ! 基本在windows上可以做到的功能, 在linux中也同样能够实现,而且自己寻找解决方案的过程才是最有趣的! 1.gua ...

  5. HTTP认证方式与https简介

    HTTP认证与https简介 HTTP请求报头: Authorization [ˌɔ:θəraɪˈzeɪʃn] HTTP响应报头: WWW-Authenticate [ɔ:ˈθentɪkeɪt] HT ...

  6. OSW 快速安装部署

    关于在运行Oracle的环境下部署OSW具体好处不再多说,只需要知晓,在日常Oracle定位各类故障,osw的数据可以协助诊断问题.MOS很多文档也多处提到需要osw的监控数据. 一.前期资料准备 1 ...

  7. PHP Curl模拟登录并抓取数据

    使用PHP的Curl扩展库可以模拟实现登录,并抓取一些需要用户账号登录以后才能查看的数据.具体实现的流程如下(个人总结): 1. 首先需要对相应的登录页面的html源代码进行分析,获得一些必要的信息: ...

  8. LeetCode 277. Find the Celebrity (找到明星)$

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  9. Ionic3 启动页以及应用图标

    将新的启动页和应用图标图片(最好是高清png)上传到根目录 resources 使用命令自动生成,通过CMD进入项目所在文件夹,分别执行 ionic cordova resources android ...

  10. 用ajax的同步请求解决登陆注册需要根据服务器返回数据判断是否能提交的问题

    最近在写www.doubilaile.com的登陆注册.需要用ajax请求服务器判断用户名是否存在,用户名和密码是否匹配,进而提交数据.碰到的问题是异步请求都能成功返回数据,但是该数据不能作为紧接着的 ...