mysql connector/c++如何判断是否成功连接到数据库?
sql::mysql::MySQL_Driver*driver;sql::Connection*conn;driver=sql::mysql::get_mysql_dri...
sql::mysql::MySQL_Driver *driver;
sql::Connection *conn;
driver = sql::mysql::get_mysql_driver_instance();
conn = driver->connect("tcp://localhost:3306/test", "root", "000000");
怎么来判断它是否成功连接到数据库了? 展开
sql::Connection *conn;
driver = sql::mysql::get_mysql_driver_instance();
conn = driver->connect("tcp://localhost:3306/test", "root", "000000");
怎么来判断它是否成功连接到数据库了? 展开
2个回答
展开全部
public static class database
{
//数据库链接
static MySqlConnection Conn = null;
//连接数据库
static void OpenConn()
{
if (Conn != null && Conn.State == ConnectionState.Open)
{
Conn.Close();
}
if (Conn == null)
{
Conn = new MySqlConnection(你的数据库连接字符串);
}
if (Conn.State == ConnectionState.Closed)
{
Conn.Open();
}
}
//关闭数据库连接
static void CloseConn()
{
if (Conn != null)
{
Conn.Close();
}
}
//执行SQL语句
public static bool ExecSql(string strSql)
{
try
{
OpenConn();
MySqlCommand Cmd = new MySqlCommand(strSql, Conn);
Cmd.ExecuteNonQuery();
Cmd.Dispose();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
CloseConn();
}
}
//获取DataSet接口
public static DataSet GetDataSet(string strSql, string strDataSetName)
{
try
{
OpenConn();
DataSet dataSet = new DataSet();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(strSql, Conn);
dataAdapter.Fill(dataSet, strDataSetName);
dataAdapter.Dispose();
return dataSet;
}
catch(Exception ex)
{
return null;
}
finally
{
CloseConn();
}
}
//获取MySqlDataReader接口
public static MySqlDataReader GetDataReader(string strSql)
{
try
{
OpenConn();
MySqlCommand Cmd = new MySqlCommand(strSql, Conn);
MySqlDataReader dataReader = Cmd.ExecuteReader();
return dataReader;
}
catch (Exception ex)
{
return null;
}
}
}
要去网上下一个MySql.Data.dll 然后倒入就可以使用上面的代码连接数据库了
{
//数据库链接
static MySqlConnection Conn = null;
//连接数据库
static void OpenConn()
{
if (Conn != null && Conn.State == ConnectionState.Open)
{
Conn.Close();
}
if (Conn == null)
{
Conn = new MySqlConnection(你的数据库连接字符串);
}
if (Conn.State == ConnectionState.Closed)
{
Conn.Open();
}
}
//关闭数据库连接
static void CloseConn()
{
if (Conn != null)
{
Conn.Close();
}
}
//执行SQL语句
public static bool ExecSql(string strSql)
{
try
{
OpenConn();
MySqlCommand Cmd = new MySqlCommand(strSql, Conn);
Cmd.ExecuteNonQuery();
Cmd.Dispose();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
CloseConn();
}
}
//获取DataSet接口
public static DataSet GetDataSet(string strSql, string strDataSetName)
{
try
{
OpenConn();
DataSet dataSet = new DataSet();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(strSql, Conn);
dataAdapter.Fill(dataSet, strDataSetName);
dataAdapter.Dispose();
return dataSet;
}
catch(Exception ex)
{
return null;
}
finally
{
CloseConn();
}
}
//获取MySqlDataReader接口
public static MySqlDataReader GetDataReader(string strSql)
{
try
{
OpenConn();
MySqlCommand Cmd = new MySqlCommand(strSql, Conn);
MySqlDataReader dataReader = Cmd.ExecuteReader();
return dataReader;
}
catch (Exception ex)
{
return null;
}
}
}
要去网上下一个MySql.Data.dll 然后倒入就可以使用上面的代码连接数据库了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询