第一次写的MySQLHelper
一、 第一次写MysqlHelper,用来管理城市的数据库
二、MySQLHelper源代码
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Cater0718
{
public static class MySqlHelper
{
//定义一个连接字符串
//readonly修饰的变量,只能在初始化的时候赋值,或者在构造函数中赋值
//其它地方只能读取,不能修改字符串
private static readonly string constr = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //1、执行增(insert)、删(delete)、改(update)的方法
//cmd.ExecuteNonQuery()
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
} //2、执行查询,返回单个结果的方法
//cmd.ExecuteSclar()
public static Object ExecuteSclar(string sql, params SqlParameter[] pms)
{
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
}
}
} //3、执行查询,返回多行多列结果的方法
//cmd.ExecuteReader()
public static MySqlDataReader ExecuteReader(string sql, params MySqlParameter[] pms)
{
MySqlConnection con = new MySqlConnection(constr);
using (MySqlCommand cmd = new MySqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
try
{
con.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch
{
con.Close();
con.Dispose();
throw;
}
}
}
}
}
三、定义的实例类
1、省份
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Cater0718
{
public class zProvinces
{
public int id { get; set; }
public string provinceid { get; set; }
public string province { get; set; }
}
}
2、城市
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Cater0718
{
public class zCity
{
public int id { get; set; }
public string cityid { get; set; }
public string city { get; set; }
public string provinceid { get; set; }
}
}
3、地区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Cater0718
{
public class zArea
{
public int id { get; set; }
public string areaid { get; set; }
public string area { get; set; }
public string cityid { get; set; }
}
}
四、最后用WinForm写的窗体
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Cater0718
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
string provinceid = comboBox1.SelectedValue.ToString(); List<zCity> list = new List<zCity>();
string sql = "select * from cities where provinceid=@provinceid";
MySqlParameter p1 = new MySqlParameter("@provinceid",MySqlDbType.String) {Value=provinceid };
using (MySqlDataReader reader = MySqlHelper.ExecuteReader(sql,p1))
{
while (reader.Read())
{
zCity model1 = new zCity();
model1.id = reader.GetInt16();
model1.cityid = reader.GetString();
model1.city = reader.GetString();
model1.provinceid = reader.GetString(); list.Add(model1);
}
comboBox2.ValueMember = "cityid";
comboBox2.DisplayMember = "city";
comboBox2.DataSource = list;
}
}
} private void Form1_Load(object sender, EventArgs e)
{
LoadProvince();
} private void LoadProvince()
{
List<zProvinces> list = new List<zProvinces>();
string sql = "select * from provinces";
using (MySqlDataReader reader = MySqlHelper.ExecuteReader(sql))
{
while (reader.Read())
{
zProvinces model = new zProvinces();
model.id = reader.GetInt16();
model.provinceid = reader.GetString();
model.province = reader.GetString(); list.Add(model);
}
comboBox1.ValueMember = "provinceid";
comboBox1.DisplayMember = "province";
comboBox1.DataSource = list;
}
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedItem != null)
{
string cityid = comboBox2.SelectedValue.ToString(); List<zArea> list = new List<zArea>();
string sql = "select * from areas where cityid=@cityid";
MySqlParameter p1 = new MySqlParameter("@cityid", MySqlDbType.String) { Value = cityid };
using (MySqlDataReader reader = MySqlHelper.ExecuteReader(sql, p1))
{
while (reader.Read())
{
zArea model1 = new zArea();
model1.id = reader.GetInt16();
model1.areaid = reader.GetString();
model1.area = reader.GetString();
model1.cityid = reader.GetString(); list.Add(model1);
}
comboBox3.ValueMember = "areaid";
comboBox3.DisplayMember = "area";
comboBox3.DataSource = list;
}
}
}
}
}
四、APP.config的配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="sqlserver" connectionString="Data Source=localhost;Initial Catalog=zone;User ID=admin;Password=123456"/>
</connectionStrings>
</configuration>
第一次写的MySQLHelper的更多相关文章
- 第一次写博客Poj1044
Date bugs Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3005 Accepted: 889 Descript ...
- 第一次写python爬虫
花了4天终于把写完了把国内的几个漏洞平台爬完了,第一次写py,之前一直都在说学习,然后这周任务是把国内的漏洞信息爬取一下.花了1天学PY,剩下的1天一个.期间学习到了很多.总结如下: ======== ...
- Java第一次写的流布局图形界面,留个纪念
package jisuanqi; import java.awt.*; public class MyFrame extends Frame{ //继承Frame类 public MyFrame() ...
- 第一次写博客,关于前端开发deMVC在js中的应用
对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...
- HDU 2064 菜鸡第一次写博客
果然集训就是学长学姐天天传授水铜的动态规划和搜索,今天讲DP由于困意加上面瘫学长"听不懂就是你不行"的呵呵传授,全程梦游.最后面对连入门都算不上的几道动态规划,我的内心一片宁静,甚 ...
- 今天我自己第一次写了一个Windows批处理bat脚本,一起学习一下吧。
今天我自己第一次写了一个Windows批处理bat脚本,备注一下 事情原由:自己使用Java开发了一个加解密的工具.但是当把工具给别人使用的时候,别人还需要把代码编译打包, 然后还需要看一下代码里面的 ...
- 第一次写Web API接口
API是什么?只知道是网络接口,具体怎么写?不会!如何调用?不会!那怎么办? 第一次的经历~~ 需求:为其他项目提供一个接口 功能:为项目提供询盘信息和商家信息,格式为Json字符串 拿过来,就开始做 ...
- 第一次写python
这是一个在BJDP上学习Coding Kata的时候用到的一个练习,原来打算用Java写的,但是一想正好是学习的好机会. 就用Python了.第一次,写的有些复杂. 这个题目是关于购买图书的打折信息的 ...
- 第一次写博客,就写如何向外行介绍自己做的是什么,那个我是做web的
如果想外行问你是做什么的,改如何回答.和内行说java后台就可以了,但外行听不懂,我们该如何描述呢? 我的方法是:我做的是java web开发,不是内外的外,是个英文单词web,全名叫world wi ...
随机推荐
- 一本通例题-生日蛋糕——题解<超强深搜剪枝,从无限到有限>
题目传送 显然是道深搜题.由于蛋糕上表面在最底层的半径确认后就确认了,所以搜索时的面积着重看侧面积. 找维度/搜索面临状态/对象:当前体积v,当前外表面面积s,各层的半径r[],各层的高度h[]. 可 ...
- Internet History, Technology, and Security(week3)——History: The Web Makes it Easy to Use
前言: 上周学习了第一个网络NSFnet,美国国家科学基金会(National Science Foundation,简称NSF)在全美国建立了6个超级计算机中心所互联的一个网络,这周继续学习网络的发 ...
- A* 算法求第 K 短路
一种具有 \(f(n)=g(n)+h(n)\) 策略的启发式算法能成为 A* 算法的充分条件是: 搜索树上存在着从起始点到终了点的最优路径. 问题域是有限的. 所有结点的子结点的搜索代价值 \(> ...
- HTTP入门(二):用Chrome开发者工具查看 HTTP 请求与响应
HTTP入门(二):用Chrome开发者工具查看 HTTP 请求与响应 本文简单总结HTTP的请求与响应. 本文主要目的是对学习内容进行总结以及方便日后查阅. 详细教程和原理可以参考HTTP文档(MD ...
- Java语言支持的变量类型有哪几种
Java语言支持的变量类型有: 类变量:独立于方法之外的变量,用 static 修饰. 实例变量:独立于方法之外的变量,不过没有 static 修饰. 局部变量:类的方法中的变量. 实例: publi ...
- WCF - Home
https://www.tutorialspoint.com/wcf/index.htm WCF Tutorial WCF stands for Windows Communication Found ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_6_字符串的截取方法
本来字符串挺长的,截图成一个短的字符串
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- 一些输出、处理细节&注意点
https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...
- C#后台保存Cookie
一般是: Response.Cookies["backurl"].Expires.AddDays(2); 但是,IE浏览器保存Cookie用 Response.Cookies[&q ...