查询 Select
public static Dictionary<string, string> ExecuteSelect(string strSql, int column)
{
List<string> tempColumn = new List<string>();
Dictionary<string, string> dic = new Dictionary<string, string>();
MySqlDataReader reader = null;
MySqlConnection conn = null;
string constructorString = "server=12.1.25.5;User Id=XXXX;password= pwd;Database=my_db";
try
{
conn = new MySqlConnection(constructorString);
conn.Open();
MySqlCommand mycmd = new MySqlCommand(strSql, conn);
reader = mycmd.ExecuteReader();
while (reader.Read())
{
tempColumn.Clear();
for (int i = 0; i < reader.FieldCount; i++)
{
tempColumn.Add(reader[i].ToString().Replace(" ", ""));
}
if (tempColumn.Count > 2)
dic.Add(tempColumn[1].ToLower() + "_" + tempColumn[2].ToLower(), tempColumn[0]);
else
dic.Add(tempColumn[1].ToLower(), tempColumn[0]);
}
}
catch (Exception ex)
{
Console.WriteLine("SqlHelper - ExecuteSelect Error: " + ex.Message + " Sql :" + strSql);
}
finally
{
reader.Close();
conn.Close();
}
return dic;
}
插入 Insert
public static void ExecuteInsert(string strSql)
{
MySqlConnection conn = null;
string constructorString = "server=12.1.25.5;User Id=XXXX;password= pwd;Database=my_db";
try
{
conn = new MySqlConnection(constructorString);
conn.Open();
MySqlCommand mycmd = new MySqlCommand();
mycmd.Connection = conn;
mycmd.CommandType = CommandType.Text;
mycmd.CommandText = strSql;
mycmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("SqlHelper - ExecuteInsert Error: " + ex.Message + " Sql :"+ strSql);
}
finally
{
conn.Close();
}
}
存储过程实例
* create procedure InsertData2(in _Product VARCHAR(255), in _Name VARCHAR(255),in _Code VARCHAR(255), in _Content VARCHAR(255))
begin
insert into tb_defect(Product,Name,Code,Content) values(_Product,_Name,_Code,_Content) ;
end