static protected MySqlConnection conn;// = "server=127.0.0.1;port=3306;user=hadoop;password=hadoop; database=huichun;SslMode = none;"; #region 修改接口 public static int DoCommand(string sql, string[] para) { int result = -1; conn = new MySqlConnection(ConnetStr); try { conn.Open(); MySqlCommand cmd = new MySqlCommand(sql, conn); for (int i = 0; i < para.Length; i++) { cmd.Parameters.AddWithValue("para" + (i + 1).ToString(), para[i]); } result = cmd.ExecuteNonQuery();//3.执行插入、删除、更改语句。执行成功返回受影响的数据的行数,返回1可做true判断。执行失败不返回任何数据,报错,下面代码都不执行 } catch (Exception ex) { DataAcc.writeTxt(ex.Message); result = -1; } finally { conn.Close(); } return(result); }
protected int ExecuteNonQuery(string sqlStr, string[] para) { int result = -1; //string sql = "insert into user(username,password,registerdate) values('啊宽','123','" + DateTime.Now + "')"; //string sql = "delete from user where userid='9'"; //string sql = "update user set username='******',password='******' where userid='8'"; conn = new MySqlConnection(ConnetStr); try { conn.Open(); MySqlCommand cmd = new MySqlCommand(sqlStr, conn); for (int i = 0; i < para.Length; i++) { cmd.Parameters.AddWithValue("para" + (i + 1).ToString(), para[i]); } result = cmd.ExecuteNonQuery();//3.执行插入、删除、更改语句。执行成功返回受影响的数据的行数,返回1可做true判断。执行失败不返回任何数据,报错,下面代码都不执行 } catch (Exception ex) { DataAcc.writeTxt(ex.Message); result = -1; } finally { conn.Close(); } return(result); }
public bool upLoadFile(string filePath) { string tempPath;// = @System.AppDomain.CurrentDomain.BaseDirectory + "\\LOG.txt"; string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); string[] splitName = fileName.Split('.'); try { switch (splitName[1]) { case "xls": case "xlsx": tempPath = string.Format("{0}\\{1}.pdf", @System.AppDomain.CurrentDomain.BaseDirectory, splitName[0]); if (!FConvertEXL(filePath, tempPath)) { return(false); } break; case "doc": case "docx": tempPath = string.Format("{0}\\{1}.pdf", @System.AppDomain.CurrentDomain.BaseDirectory, splitName[0]); if (!FConvertWORD(filePath, tempPath)) { return(false); } break; case "pdf": tempPath = string.Format("{0}\\{1}.pdf", @System.AppDomain.CurrentDomain.BaseDirectory, splitName[0]); File.Copy(filePath, tempPath); break; default: return(false); break; } FileStream fs = File.Open(tempPath, FileMode.Open); FileData = new byte[fs.Length]; fs.Read(FileData, 0, FileData.Length); Fileid = -1; fileName = tempPath.Substring(tempPath.LastIndexOf("\\") + 1); FileFullName = tempPath.Substring(tempPath.LastIndexOf(".") + 1).ToLower(); DisableFlag = true; fs.Close(); File.Delete(tempPath); return(true); } catch (Exception ex) { DataAcc.writeTxt(ex.Message); return(false); } }
/// <summary> /// sql变量需要以para + 序列命名,如:para1,para2,以1开始 /// </summary> /// <param name="sql"></param> /// <param name="para"></param> /// <returns></returns> protected System.Data.DataTable Select(string sql, string[] para) { System.Data.DataTable dt = new System.Data.DataTable(); //string sql = "select * from user"; conn = new MySqlConnection(ConnetStr); try { conn.Open(); MySqlCommand cmd = new MySqlCommand(sql, conn); for (int i = 0; i < para.Length; i++) { cmd.Parameters.AddWithValue("para" + (i + 1).ToString(), para[i]); } MySqlDataReader reader = cmd.ExecuteReader();//执行ExecuteReader()返回一个MySqlDataReader对象 bool col = true; while (reader.Read())//初始索引是-1,执行读取下一行数据,返回值是bool { if (col) { for (int i = 0; i < reader.FieldCount; i++) { dt.Columns.Add(reader.GetName(i).Trim()); } col = false; } System.Data.DataRow dr = dt.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { dr[reader.GetName(i).Trim()] = reader[i].ToString(); } dt.Rows.Add(dr); //Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString()); //Console.WriteLine(reader.GetInt32(0)+reader.GetString(1)+reader.GetString(2)); //Console.WriteLine(reader.GetInt32("userid") + reader.GetString("username") + reader.GetString("password"));//"userid"是数据库对应的列名,推荐这种方式 } } catch (Exception ex) { DataAcc.writeTxt(ex.Message); dt = null; } finally { conn.Close(); } return(dt); }
public static int DoCommand(string sql) { int result = -1; conn = new MySqlConnection(ConnetStr); try { conn.Open(); MySqlCommand cmd = new MySqlCommand(sql, conn); result = cmd.ExecuteNonQuery();//3.执行插入、删除、更改语句。执行成功返回受影响的数据的行数,返回1可做true判断。执行失败不返回任何数据,报错,下面代码都不执行 } catch (Exception ex) { DataAcc.writeTxt(ex.Message); result = -1; } finally { conn.Close(); } return(result); }