C 連線資料庫執行SQL語句

2021-09-01 04:02:39 字數 454 閱讀 7841

sqlconnection connection = new sqlconnection(connstring); string sql = "select count(*) from student"; connection.open();// 開啟資料庫連線 sqlcommand command = new sqlcommand(sql, connection); int num = (int)command.executescalar();

執行命令前必須開啟資料庫連線!

方法 說明

executenonquery 執行不返回行的語句,如update等

executereader 返回datareader物件

executescalar 返回單個值,如執行count(*)

executescalar返回型別為object,

返回第一行第一列,

可以轉換為具體型別

jdbc連線資料庫和執行sql語句

連線資料庫 1 public class dbconnection catch classnotfoundexception e 1920 2 建立連線 21 驅動程式管理器呼叫驅動程式物件與指定的資料庫建立連線 22try catch sqlexception e 2728 返回連線物件 29re...

php連線資料庫sql語句

從伺服器開啟php檔案,如何處理中文亂碼問題 每一句php 最後必須要加 分號 header content type text html charset utf 8 如何引入其他php檔案 include php檔案 與資料庫建立連線四步 include 匯入的php檔案,需要就匯入 header...

連線資料庫執行sql指令

1.mysqli連線資料庫資料庫 首先建立資料庫 在物件導向的方式中,mysqli被封裝成乙個類,它的構造方法如下 construct string host string username string passwd string dbname int port string socket 在上述...