本文目錄一覽:
mysql關閉連接
哥們下面也是2個數據操作方法1是用來執行插入操作2是用來顯示操作3是APP.CONFIG配置文件,需要導入引用mysql.data.dll來連接資料庫
1 public void getconn(string sql)//資料庫操作鏈接方法
{
string conn = ConfigurationSettings.AppSettings[“conn”].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//實例化鏈接
mysql.Open();//開啟
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();//執行
mysql.Close();//關閉資源
}
———————————————————————————————————
2 public static MySqlDataAdapter getadaoter(string sql)//顯示操作
{
string conn = ConfigurationSettings.AppSettings[“conn”].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//實例化鏈接
mysql.Open();//開啟
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mysql);
mysql.Close();
return mda;
//需要在調用的時候進行數據集填充
}
———-3
app.config
?xml version=”1.0″ encoding=”utf-8″ ?
configuration
appSettings
add key=”conn” value=”Server=112.124.17.213;User ID=root;Password=123;Database=goods;CharSet=gbk;”/
!–鏈接資料庫–
add key=”category” value=”st_category”/
/appSettings
/configuration
mysql如何開啟關閉外部連接
使用「mysql -uroot -proot」命令可以連接到本地的mysql服務。
使用「use mysql」命令,選擇要使用的資料庫,修改遠程連接的基本信息,保存在mysql資料庫中,因此使用mysql資料庫。
使用「GRANT ALL PRIVILEGES ON . TO 『root』@』%』 IDENTIFIED BY 『root』 WITH GRANT OPTION;」命令可以更改遠程連接的設置。
GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION;1
使用「flush privileges;」命令刷新剛才修改的許可權,使其生效。
使用「select host,user from user;」查看修改是否成功。
1、開啟遠程訪問:
use mysql;
update user set host = 「%」 where user = 「root」;
flush privileges;
2、 添加用戶
use mysql;
insert into user(host, user, password) values(「%」, 「root」, password(「yourpassword」))
grant all privileges on . to 『root』@』%』 with grant option #賦予任何主機訪問資料庫許可權
flush privileges;
3、關閉遠程訪問:
use mysql;
update user set host = 「localhost」 where user = 「root」 and host= 「%」;
flush privileges;
4、查看用戶許可權:
use information_schema;
select * from user_privileges;
5、查看當前mysql用戶:
use mysql;
select user, host from user;
6、更新用戶:
update mysql.user set password=password(『新密碼』) where User=」phplamp」 and Host=」localhost」;
flush privileges;
7、刪除用戶:
DELETE FROM user WHERE User=」phplamp」 and Host=」localhost」;
flush privileges;
怎麼開啟或關閉mysql資料庫狀態
mysql_connect()函數是連接資料庫的,
mysql_close()是關閉資料庫的,但通常情況下,不需要執行,因為在腳本執行完會自動關閉,
如果想讓連接不關閉的話,用 mysql_pconnect() 建立持久連接!
如何斷開MYSQL資料庫連接
1 根據資料庫連接字元串設置鏈接生命周期 會自行斷開
2 拿,net環境舉例 MySQL.Data.MySqlClient類庫下的 MySqlConnection類 下面有個方法 叫 .Close() 即可斷開連接;
訪問MySqlConnection類下的 Statu (只讀的)欄位 可以獲得當前連接狀態
提示 斷開後的連接 可以隨時 根據需要打開!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/152903.html