MySQL数据库工具类之——DataTable批量加入MySQL数据库(Net版)

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;

public class MySqlDbHelper
{

#region 私有变量
private const string defaultConfigKeyName = "MySqlConnection";//连接字符串 Database='数据库';Data Source='IP地址';User Id='sa';Password='sa';pooling=true
private string connectionString;
private string providerName;

#endregion

#region 构造函数

/// <summary>
/// 默认构造函数(DbHelper)
/// </summary>
public MySqlDbHelper()
{
    this.connectionString = ConfigurationManager.ConnectionStrings[defaultConfigKeyName].ConnectionString;
    this.providerName = ConfigurationManager.ConnectionStrings[defaultConfigKeyName].ProviderName;
}

/// <summary>
/// DbHelper构造函数
/// </summary>
/// <param name="keyName">连接字符串名</param>
public MySqlDbHelper(string keyName)
{
    this.connectionString = ConfigurationManager.ConnectionStrings[keyName].ConnectionString;
    this.providerName = ConfigurationManager.ConnectionStrings[keyName].ProviderName;
}

#endregion

public int ExecuteNonQuery(string sql, params  MySqlParameter[] parameters)
{
    ;
    using (MySqlConnection con = new MySqlConnection(connectionString))
    {
        con.Open();
        using (MySqlCommand cmd = new MySqlCommand(sql, con))
        {
            foreach (MySqlParameter parameter in parameters)
            {
                cmd.Parameters.Add(parameter);
            }
            try
            {
                res = cmd.ExecuteNonQuery();
            }
            catch
            {
                res = -;
            }
        }
    }
    return res;
}

public object GetExeScalar(string sql, params MySqlParameter[] parameters)
{
    object res = null;
    using (MySqlConnection con = new MySqlConnection(connectionString))
    {
        con.Open();
        using (MySqlCommand cmd = new MySqlCommand(sql, con))
        {
            foreach (MySqlParameter parameter in parameters)
            {
                cmd.Parameters.Add(parameter);
            }
            res = cmd.ExecuteScalar();
        }
    }
    return res;
}

public DataTable GetDataTable(string sql, params MySqlParameter[] parameters)
{
    DataSet dataset = new DataSet();
    using (MySqlConnection con = new MySqlConnection(connectionString))
    {
        con.Open();
        using (MySqlCommand cmd = new MySqlCommand(sql, con))
        {
            foreach (MySqlParameter parameter in parameters)
            {
                cmd.Parameters.Add(parameter);
            }
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            adapter.Fill(dataset);
        }
    }
    ];
}

/// <summary>
/// DataTable批量加入MYSQL数据库
/// </summary>
/// <param name="dataTable"></param>
/// <returns></returns>
public string InsertByDataTable(DataTable dataTable)
{
    string result = string.Empty;
    )
    {
        return "添加失败!DataTable暂无数据!";
    }
    if (string.IsNullOrEmpty(dataTable.TableName))
    {
        return "添加失败!请先设置DataTable的名称!";
    }
    // 构建INSERT语句
    StringBuilder sb = new StringBuilder();
    sb.Append("INSERT INTO " + dataTable.TableName + "(");
    ; i < dataTable.Columns.Count; i++)
    {
        sb.Append(dataTable.Columns[i].ColumnName + ",");
    }
    sb.Remove(sb.ToString().LastIndexOf();
    sb.Append(") VALUES ");
    ; i < dataTable.Rows.Count; i++)
    {
        sb.Append("(");
        ; j < dataTable.Columns.Count; j++)
        {
            sb.Append("'" + dataTable.Rows[i][j] + "',");
        }
        sb.Remove(sb.ToString().LastIndexOf();
        sb.Append("),");
    }
    sb.Remove(sb.ToString().LastIndexOf();
    sb.Append(";");
    ;
    using (MySqlConnection con = new MySqlConnection(connectionString))
    {
        con.Open();
        using (MySqlCommand cmd = new MySqlCommand(sb.ToString(), con))
        {
            try
            {
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                res = -;
                // Unknown column 'names' in 'field list'
                result = "操作失败!" + ex.Message.Replace("Unknown column", "未知列").Replace("in 'field list'","存在字段集合中!");
            }
        }
    }
    )
    {
        result = "恭喜添加成功!";
    }
    return result;
}

}

posted @ 2020-04-28 15:27  乌卡拉卡  阅读(330)  评论(0编辑  收藏  举报