1. 程式人生 > >mysql_demo

mysql_demo

HERE href class www. sca all nal fill state

mysql的下載安裝可參考此博客很詳細:https://www.cnblogs.com/xiongzaiqiren/p/8970203.html

完成後采用工具端: Nvicat(mysql的一個可視化工具網上有下載的無需安裝打開即可用) 著個在查詢時可以正常執行但是總是會報錯:

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘information_schema.PROFILING.SEQ‘ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

執行如下信息後恢復:

set sql_mode = ‘‘;
set sql_mode = ‘NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES‘;

對與C#連接需要下載MySql.data.dll 並引用後,需要 using MySql.Data.MySqlClient;

操作連接非常類似於連接sqlserver,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace my_sql_demo
{
class Program
{
static void Main(string[] args)
{
string str = "data source=127.0.0.1;initial catalog=mysql;user id=sa;password=Syfjc021;port=3306";

//string str = "host=127.0.0.1;user id=sa;password=Syfjc021;database=mysql";

MySqlConnection con = new MySqlConnection(str);

string sql_select = " select * from test_table; ";

string sql_insert = " insert into test_table(ruid,emp_no,emp_name)values(1,‘ss‘,‘cxq‘); ";

string sql_update = " update test_table set emp_no=‘shanbei‘ where ruid =1; ";

string sql_delte = " delete from test_table where ruid=1; ";

con.Open();

//select


for (int i = 0; i < 4; i++)
{
string sql = string.Empty;

switch (i)
{
case 0:
sql = sql_select;
break;
case 1:
sql = sql_insert;
break;
case 2:
sql = sql_update;
break;
case 3:
sql = sql_delte;
break;
default:
break;
}


using (MySqlCommand cmd = new MySqlCommand(sql_select, con))
{
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();


}

MySqlCommand cmd_exe = new MySqlCommand(sql, con);

cmd_exe.CommandType = System.Data.CommandType.Text;

cmd_exe.ExecuteScalar();


MySqlDataAdapter mda = new MySqlDataAdapter(cmd);
System.Data.DataTable dt = new System.Data.DataTable("data");

mda.Fill(dt);

if (dt != null && dt.Rows.Count > 0)
{
foreach (System.Data.DataRow item in dt.Rows)
{
Console.WriteLine("{0}---{1}---{2}---{3}", item["ruid"], item["emp_no"], item["emp_name"],sql);
}
}
else
{
Console.WriteLine("null or o rows--->" + sql);
}

}

}


Console.ReadKey();

}
}
}

mysql_demo