c如何訪問資料庫
1、C/C++與資料庫交互,像 mssql/ mysql / oracle 等,一般都有成熟的第三方庫,這些庫裡面無非就是封裝了與資料庫通訊的方式和通訊協議搜一下要用的資料庫相關的 API 文檔,會說得很清楚任何文件都是二進制數據,關鍵是數據存儲的組織方式通用擴展名的文件,像gif/doc/jpg/wav,格式都是固定的。
2、舉個例子,連接SQL:
// 打開資料庫
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=%s"), m_strUnEntryptMdbFilePath,m_strMDBPassword);
// 創建連接
HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
_ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
// 聲明表單指針
_RecordsetPtr pBandRecordset;
pBandRecordset.CreateInstance(__uuidof(Recordset));
// 執行語句
CString strSQL(L"SELECT * FROM [Band]");
m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
// 提取某一項 例如BandInfo
int iBandInfo = wcscmp(colum, L"BandInfo");
while(!recordsetPtr->adoEOF)
{
var = recordsetPtr->GetCollect(colum);
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
recordsetPtr->MoveNext();
}
『貳』 c與資料庫連接的詳細步驟
C#連接資料庫有以下幾個步驟:
1:使用配置的資料庫連接串,創建資料庫連接 Connection 對象
2:構建操作的sql語句
3:定義command對象
4:打開數據連接
5:執行命令
舉一個例子,刪除操作
public class StudentService
{
//從配置文件中讀取資料庫連接字元串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString();
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
AdoNetModels.Student model = new Student();
#region 刪除數據1
public int DeleteStudent(int stuID)
{
int result = 0;
// 資料庫連接 Connection 對象
SqlConnection connection = new SqlConnection(connString);
// 構建刪除的sql語句
string sql = string.Format("Delete From Student Where stuID={0}", stuID);
// 定義command對象
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
result = command.ExecuteNonQuery(); // 執行命令
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return result;
}
#endregion
『叄』 如何在C/C++程序中使用資料庫
一般要看使用的資料庫。如果 操作 sql server 需要用到 ADO 驅動,這種驅動使用MFC做的包裝類比較多一些,在控制台直接編寫代碼可能稍顯繁瑣。
如果操作mysql,在安裝mysql的時候,有相應的include頭文件和庫文件,可以在自己的IDE開發環境中進行設置。
『肆』 c語言怎麼連接mysql資料庫 代碼
//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路徑
//在工程設置-》鏈接》庫模塊中添加 libmysql.lib
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <winsock.h>
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2]);
}
mysql_free_result(res);
mysql_close(conn);
}
===============================
#if defined(_WIN32) || defined(_WIN64) //為了支持windows平台上的編譯
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
//定義資料庫操作的宏,也可以不定義留著後面直接寫進代碼
#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相當於 char *argv[]
{
MYSQL mysql,*handle; //定義資料庫連接的句柄,它被用於幾乎所有的MySQL函數
MYSQL_RES *result; //查詢結果集,結構類型
MYSQL_FIELD *field ; //包含欄位信息的結構
MYSQL_ROW row ; //存放一行查詢結果的字元串數組
char querysql[160]; //存放查詢sql語句字元串
//初始化
mysql_init(&mysql);
//連接資料庫
if (!(handle = mysql_real_connect(&mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]));
//查詢資料庫
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle));
}
//存儲結果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle));
}
printf("number of fields returned: %d\n",mysql_num_fields(result));
//讀取結果集的內容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0]) ) ;
}
//釋放結果集
mysql_free_result(result);
//關閉資料庫連接
mysql_close(handle);
system("PAUSE");
//為了兼容大部分的編譯器加入此行
return 0;
}
『伍』 如何使用mysql的C介面訪問mysql資料庫
調用mysql資料庫API。
去官網下載mysql c API庫文件,然後安裝一下,每個調用資料庫的函數都有相關解釋,直接參照函數解釋進行編程就行了。
注意編寫makefile的時候把相關依賴庫加入
『陸』 c語言如何和資料庫連接
C函數庫沒有相應的資料庫連接介面函數。
只能夠嘗試用二進制或文本模式讀寫文件,來模擬相應的資料庫操作等。
可以嘗試下C++庫類,裡面有資料庫連接的介面
『柒』 C語言連接Access資料庫如何實現標準的c語言
1、C/C++與資料庫交互,像 mssql/ mysql / oracle 等,一般都有成熟的第三方庫,這些庫裡面無非就是封裝了與資料庫通訊的方式和通訊協議搜一下要用的資料庫相關的 API 文檔,會說得很清楚任何文件都是二進制數據,關鍵是數據存儲的組織方式通用擴展名的文件,像gif/doc/jpg/wav,格式都是固定的。
2、舉個例子,連接SQL:
//打開資料庫
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=%s;JetOLEDB:DatabasePassword=%s"),m_strUnEntryptMdbFilePath,m_strMDBPassword);
//創建連接
HRESULThr=m_pConnection.CreateInstance(_uuidof(Connection));
_ConnectionPtrm_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
//聲明表單指針
_RecordsetPtrpBandRecordset;
pBandRecordset.CreateInstance(__uuidof(Recordset));
//執行語句
CStringstrSQL(L"SELECT*FROM[Band]");
m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
//提取某一項例如BandInfo
intiBandInfo=wcscmp(colum,L"BandInfo");
while(!recordsetPtr->adoEOF)
{
var=recordsetPtr->GetCollect(colum);
if(var.vt!=VT_NULL)
strName=(LPCSTR)_bstr_t(var);
recordsetPtr->MoveNext();
}
『捌』 C/C++ 怎麼操作ACCESS資料庫啊
C++利用ado編程,首先要引用一個庫文件
#import "c:\program files\common files\system\ado\msado15.dll"no_namespaces rename("EOF" adoEOF")
然後用AfxOleInit()初始化組件
定義一個_ConnectionPtr 指針,然後利用這個指針打開資料庫字元串,
你去網個看一下,很多教程的
http://hi..com/lninglove/blog/item/3f6cec22959e4ca34723e833.html
『玖』 如何在c程序中調用access資料庫
c語言不能連接資料庫,只能用文件保存數據;
我在學習的時候也曾遇到過這樣的問題,請教老師,老師告訴我C語言主要用來開發系統的,還有就是用來開發游戲的。一般用文件保存數據。不會用到資料庫;
要用資料庫的語言有VB,java,c#,Dephi等等
『拾』 c/c++寫伺服器一般用什麼方式訪問資料庫的
要做伺服器端的話資料庫就是放在你的伺服器上的, 資料庫會提供相應的訪問介面, 具體使用方式可以搜一下"C++連接資料庫"之類的
http是客戶端訪問伺服器才用得到, 直接操作資料庫的總是伺服器端而不是客戶端