c mysql 例項 C 連線mysql例項

2021-10-18 20:45:21 字數 2307 閱讀 5263

usingsystem;usingsystem.configuration;usingmysql.data.mysqlclient;///

///testdatebase 的摘要說明///

public classtestdatebase

public static voidmain(string args)

mysqlconnection mysql=getmysqlcon();//查詢sql

string sqlsearch = "select * from student";//插入sql

string sqlinsert = "insert into student values (12,'張三',25,'大專')";//修改sql

string sqlupdate = "update student set name='李四' where id= 3";//刪除sql

string sqldel = "delete from student where id = 12";//列印sql語句

console.writeline(sqldel);//四種語句物件//mysqlcommand mysqlcommand = getsqlcommand(sqlsearch, mysql);//mysqlcommand mysqlcommand = getsqlcommand(sqlinsert, mysql);//mysqlcommand mysqlcommand = getsqlcommand(sqlupdate, mysql);

mysqlcommand mysqlcommand =getsqlcommand(sqldel, mysql);

mysql.open();//getresultset(mysqlcommand);//getinsert(mysqlcommand);//getupdate(mysqlcommand);

getdel(mysqlcommand);//記得關閉

mysql.close();

string readline=console.readline();

public staticmysqlconnection getmysqlcon()

string mysqlstr= "database=test;data source=127.0.0.1;user id=root;password=root;pooling=false;charset=utf8;port=3306";//string mysqlcon = configurationmanager.connectionstrings["mysqlcon"].connectionstring;

mysqlconnection mysql = newmysqlconnection(mysqlstr);returnmysql;

///建立執行命令語句物件///

public staticmysqlcommand getsqlcommand(string sql,mysqlconnection mysql)

mysqlcommand mysqlcommand= newmysqlcommand(sql, mysql);//mysqlcommand mysqlcommand = new mysqlcommand(sql); = mysql;

returnmysqlcommand;

///查詢並獲得結果集並遍歷///

public static voidgetresultset(mysqlcommand mysqlcommand)

mysqldatareader reader=mysqlcommand.executereader();trycatch(exception)

console.writeline("查詢失敗了!");

}finallycatch(exception ex)

string message=ex.message;

console.writeline("插入資料失敗了!" +message);

///修改資料///

public static voidgetupdate(mysqlcommand mysqlcommand)

catch(exception ex)

string message=ex.message;

console.writeline("修改資料失敗了!" +message);

///刪除資料///

public static voidgetdel(mysqlcommand mysqlcommand)

catch(exception ex)

string message=ex.message;

console.writeline("刪除資料失敗了!" +message);

c mysql 連線串 C 資料庫連線字串

sa登陸 data source initial catalog 資料庫 user id sa,pwd windows登陸 data source initial catalog 資料庫 integrated security true c 資料庫連線字串集合 一 常用連線 1.使用sqlconne...

C MySQL資料庫操作例項

環境設定 示例 include windows.h include stdio.h include stdlib.h include string h include mysql.h include iostream using namespace std intmain j mysql num f...

C MySQL資料庫操作例項

由於課程大實驗需要使用c 操作mysql資料庫,經過一番研究終於成功實現vs2008中與mysql的連線。環境設定 安裝完mysql之後,將安裝目錄中的mysql5 lib debug 目錄下的libmysql.lib檔案拷到vs2008安裝目錄中的vc lib 下,然後在 專案 選項 c c 常規...