****
编辑****
ExcuteNonQuery
: 执行【insert(增)】、【updata(改)】、【delete(删)】语句ExcuteReader
: 执行多行查询,返回DataReader对象ExcuteScalar
: 执行单行查询,返回查询结果的首行数据DataTable dt = new DataTable();
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);
// 将数据填充到dataTable中
DataAdapter.Fill(dt);
DataSet ds = new DataSet();
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);
// 将数据填充到dataSet中
DataAdapter.Fill(ds);
DataSet ds = new DataSet();
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);
// 将数据填充到dataSet中
DataAdapter.Fill(ds);
DataTable td = ds.Tables[0]
public MySqlConnection createConnect()
{
// 数据连接的基本信息对象
MySqlConnectionStringBuilder scsb = new MySqlConnectionStringBuilder();
scsb.UserID = "root";
scsb.Server = "127.0.0.1";
scsb.Port = 3306;
scsb.Password = "123456";
scsb.Database = "account_center";
MySqlConnection mySqlConnection = new MySqlConnection(scsb.ConnectionString);
mySqlConnection.Open();
Console.WriteLine("数据库连接成功!");
return mySqlConnection;
}
public MySqlConnection createConnect2()
{
// 数据连接的基本信息对象
string conStr = "server=127.0.0.1;database=account_center;username=root;password=123456;";
MySqlConnection mySqlConnection = new MySqlConnection(conStr);
mySqlConnection.Open();
return mySqlConnection;
}
MySqlDataAdapter 方式
查询数据表数据(无参数)
public void queryNoArg(MySqlConnection conn)
{
string sql = "select * from user_test ";
DataTable dt = new DataTable();
try
{
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);
// 将数据填充到dataTable中
DataAdapter.Fill(dt);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// 关闭连接
conn.Close();
}
// 打印表数据
printData(dt);
}
打印数据方法
public void printData(DataTable dt)
{
if (dt.Rows.Count < 0)
{
return;
}
//打印所有列名
string columnName = string.Empty;
for (int i = 0; i < dt.Columns.Count; i++)
{
columnName += dt.Columns[i].ColumnName + " | ";
}
Console.WriteLine(columnName);
Console.WriteLine("-------------------------");
//打印每一行的数据
foreach (DataRow row in dt.Rows)
{
string columnStr = string.Empty;
foreach (DataColumn column in dt.Columns)
{
columnStr += row[column] + " | ";
}
Console.WriteLine(columnStr);
}
}
输出结果
name | age | create_date |
-------------------------
张三 | 12 | 2023/1/4 17:17:24 |
李四 | 33 | 2023/1/4 17:17:24 |
查询数据表数据(带参数)
public void queryByArg(MySqlConnection conn)
{
string sql = "select * from user_test where name=@p1 and age=@p2 ";
DataTable dt = new DataTable();
try
{
// 建立命令执行对象
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
// 设置命令的类型,普通的sql命令是字符串的用Text即可 ,如果是存储过程则用 CommandType.StoredProcedure
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@p1", "张三");
cmd.Parameters.AddWithValue("@p2", 12);
MySqlDataAdapter DataAdapter = new MySqlDataAdapter(cmd);
// 将数据填充到dataTable中
DataAdapter.Fill(dt);
// 释放资源
DataAdapter.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// 关闭连接
conn.Close();
}
// 打印表数据
printData(dt);
}
输出结果
name | age | create_date |
-------------------------
张三 | 12 | 2023/1/4 17:17:24 |
MySqlDataReader 方式
查询数据表数据
public void ExecuteReader(MySqlConnection connection)
{
string sql = "select * from user_test ";
MySqlCommand cmd = new MySqlCommand(sql, connection);
MySqlDataReader myReader = null;
try
{
myReader = cmd.ExecuteReader();
// 打印数据
printByReader(myReader);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
connection.Close();
cmd.Dispose();
}
}
打印数据方法
public void printByReader(MySqlDataReader myReader)
{
if (myReader==null)
{
return;
}
//打印所有列名
string columnName = string.Empty;
for (int i = 0; i < myReader.FieldCount; i++)
{
columnName += myReader.GetName(i) + " | ";
}
Console.WriteLine(columnName);
Console.WriteLine("-------------------------");
//打印每一行的数据
while (myReader.Read())
{
string columnStr = string.Empty;
for (int i = 0; i < myReader.FieldCount; i++)
{
columnStr += myReader[i].ToString() + " | ";
}
Console.WriteLine(columnStr);
}
// 释放资源
myReader.Close();
}
输出结果
name | age | create_date |
-------------------------
张三 | 12 | 2023/1/4 17:17:24 |
李四 | 33 | 2023/1/4 17:17:24 |
修改表数据都用 ExecuteNonQuery() 方法
ExecuteNonQuery() 适用于 添加、修改、删除 相关的sql操作
public int updateData(MySqlConnection conn)
{
// 添加数据
string addSql = "insert into user_test(name,age,create_date) values('王五',14,now())";
// 修改数据
string updateSql = "update user_test set age=55 where name='李四'";
// 删除数据
string delSql = "delete from user_test where name = '张三'";
MySqlCommand cmd = new MySqlCommand(addSql, conn);
try
{
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (Exception e)
{
conn.Close();
//throw e;
Console.WriteLine(e.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
return -1;
}
数据库事务(MySqlTransaction类)
public void TransactionTest(MySqlConnection conn)
{
string sql = "insert into user_test(name,age,create_date) values('bbb',224,now())";
// 开启事务
MySqlTransaction transaction = conn.BeginTransaction();
try
{
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// 事务回滚
transaction.Rollback();
conn.Close();
}
finally
{
if (conn.State != ConnectionState.Closed)
{
//事务要么回滚要么提交,即Rollback()与Commit()只执行一个
transaction.Commit();
conn.Close();
}
}
}
全部0条评论
快来发表一下你的评论吧 !