|
发表于 2020-1-17 20:57:21
|
显示全部楼层
河南省郑州市
本帖最后由 qingshanlushui 于 2020-1-17 21:45 编辑
...封装增删改查
- 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;
- using MySql.Data.MySqlClient;
- using System.Configuration;
- namespace zx61_MySQLTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
- /// <summary>
- /// 增删改
- /// </summary>
- /// <returns>受影响行数</returns>
- public static int zx61_ExecuteNonQuery(string connStr, string sql, params Mxxarameter[] paras)
- {
- using (MySqlConnection conn = new MySqlConnection(connStr))
- {
- conn.Open();
- MySqlCommand command = new MySqlCommand(sql,conn);
- command.Parameters.AddRange(paras);
- int rel = command.ExecuteNonQuery();
- return rel;
- }
- }
- /// <summary>
- /// cha询返回单行单列结果
- /// </summary>
- /// <returns>结果</returns>
- public object zx61_ExecuteScalar(string connStr, string sql, params Mxxarameter[] paras)
- {
- using (MySqlConnection conn = new MySqlConnection(connStr))
- {
- conn.Open();
- MySqlCommand command = new MySqlCommand(sql, conn);
- command.Parameters.AddRange(paras);
- object rel = command.ExecuteScalar();
- return rel;
- }
- }
- /// <summary>
- /// cha询返回多行多列结果,MySqlDataReader用out返回出去..out之后接收不到 reader .....这样封装就没意义了...然后用下一课的方法...
- /// </summary>
-
- private void button_Insert_Click(object sender, EventArgs e)
- {
- string sql = "INSERT INTO classes values(null,@className,@mamager_id,@lacation_id)";
- List<Mxxarameter> paras = new List<Mxxarameter>();
- paras.Add(new Mxxarameter("@className", "csharp"));
- paras.Add(new Mxxarameter("@mamager_id", 2));
- paras.Add(new Mxxarameter("@lacation_id", 3));
- int rel = zx61_ExecuteNonQuery(connStrxxaras.ToArray());
- Console.WriteLine(rel);
- }
- }
- }
复制代码
|
|