當前位置:首頁 » 操作系統 » c提取資料庫數據

c提取資料庫數據

發布時間: 2022-06-26 04:04:34

『壹』 在c語言中,如何提取一個txt資料庫文件中的信息

簡單的用C讀取文件的例子代碼片段。
... ...
FILE *fp;
char buffer[1000];
fp=fopen( "input.txt", "r" );
while ( fread(&buffer, sizeof(buffer), 1, fp )==1)
{
... ...
//對從文件讀出來的數據在此處進行處理
... ...
}
fclose(fp);
... ...

『貳』 c語言從自定義資料庫文件中提取文件

沒看懂lz想表達什麼意思?
lz舉個例子先。。
----------------
還是沒看懂。lz不是自定義了的資料庫文件格式么,那全部按照自定義的結構來讀寫該文件就行了。
您是說的「載入資源函數」的輸入參數是文件吧?而你想給他傳你自定義的二進制流?那麼這些"函數"不是lz自己寫的么?如果不是lz自己寫的,那麼又怎麼能傳自定義的數據格式呢?函數都有自己所要求的文件格式的。。

『叄』 sqlite資料庫怎麼用c語言提取其中的一行數據

select * from Table LIMIT 0,1

『肆』 c語言關於從資料庫讀取數據寫文件

#include<stdio.h>
execsqlincludesqlca;

intmain(){
execsqlbegindeclaresection;
charuserpasswd[30]="openlab/123456";
struct{
intid;
charname[30];
doublesalary;
}emp;
execsqlenddeclaresection;
execsqlconnect:userpasswd;

selectid,first_name,salaryfrom
s_emporderbysalary;
execsqlopenempcursor;
;
for(;;){
execsqlfetchempcursorinto:emp;
printf("%d:%s:%lf ",emp.id,emp.name,
emp.salary);
}
execsqlcloseempcursor;
execsqlcommitworkrelease;
}

把數據存到結構體里。

『伍』 c語言怎麼從資料庫取出數據實例

使用pro*c,在c語言中嵌入SQL語句

『陸』 請問如何用C或C++把mysql資料庫中的某一列數據讀出來,然後把這一列數據存放在數組中急用!!!謝謝!

MYSQL_ROW row; 是mysql內部的類型,可能是一個結構體A,mysql先執行以下desc table的操作,依照表結構創建欄位,也可能只是得到一行數據有幾列,分別是什麼類型。
row = mysql_fetch_row(result)會取一樣的數據,mysql裡面每成功調用一次這個函數mysql_fetch_row就會取一行,類似
vector<struct A> v_a;
··········賦值v_a·········
全局定義 int x = 0;
function: mysql_fetch_row
while (x < v_a.size())
row = v_a[x++];

mysql_fetch_lengths是得到一個row裡面有多少數據,也就是table里的欄位數

後面取的過程就很簡單了,某個欄位row[i] 為空的時候,不做row[i] ? row[i] : "NULL" 取到的會是個'\0',這個對於程序處理是比較危險的,為空的時候就附值一個標識是比較常用的做法。

fyi

『柒』 如何用c語言提取excel中的數據

1.方法一:採用OleDB讀取EXCEL文件:

把EXCEL文件當做一個數據源來進行數據的讀取操作,實例如下:

publicDataSetExcelToDS(stringPath)

{

stringstrConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"DataSource="+Path+";"+"ExtendedProperties=Excel8.0;";

OleDbConnectionconn=newOleDbConnection(strConn);

conn.Open();

stringstrExcel="";

OleDbDataAdaptermyCommand=null;

DataSetds=null;

strExcel="select*from[sheet1$]";

myCommand=newOleDbDataAdapter(strExcel,strConn);

ds=newDataSet();

myCommand.Fill(ds,"table1");

returnds;

}

對於EXCEL中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到

stringstrConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"DataSource="+Path+";"+"ExtendedProperties=Excel8.0;";

OleDbConnectionconn=newOleDbConnection(strConn);

DataTableschemaTable=objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);

stringtableName=schemaTable.Rows[0][2].ToString().Trim();

另外:也可進行寫入EXCEL文件,實例如下:
publicvoidDSToExcel(stringPath,DataSetoldds)

{

//先得到匯總EXCEL的DataSet主要目的是獲得EXCEL在DataSet中的結構

stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+path1+";ExtendedProperties=Excel8.0";

OleDbConnectionmyConn=newOleDbConnection(strCon);

stringstrCom="select*from[Sheet1$]";

myConn.Open();

OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);

ystem.Data.OleDb.OleDbCommandBuilderbuilder=newOleDbCommandBuilder(myCommand);

//QuotePrefix和QuoteSuffix主要是對builder生成InsertComment命令時使用。

builder.QuotePrefix="[";//獲取insert語句中保留字元(起始位置)

builder.QuoteSuffix="]";//獲取insert語句中保留字元(結束位置)

DataSetnewds=newDataSet();

myCommand.Fill(newds,"Table1");

for(inti=0;i<oldds.Tables[0].Rows.Count;i++)

{

在使用ImportRow後newds內有值,但不能更新到Excel中因為所有導入行的

DataRowState!=Added

DataRownrow=aDataSet.Tables["Table1"].NewRow();

for(intj=0;j<newds.Tables[0].Columns.Count;j++)

{

nrow[j]=oldds.Tables[0].Rows[i][j];

}

newds.Tables["Table1"].Rows.Add(nrow);

}

myCommand.Update(newds,"Table1");

myConn.Close();

}

2.方法二:引用的com組件:Microsoft.Office.Interop.Excel.dll讀取EXCEL文件
首先是Excel.dll的獲取,將Office安裝目錄下的Excel.exe文件Copy到DotNet的bin目錄下,cmd到該目錄下,運行TlbImpEXCEL.EXEExcel.dll得到Dll文件。再在項目中添加引用該dll文件.
//讀取EXCEL的方法(用范圍區域讀取數據)

privatevoidOpenExcel(stringstrFileName)

{

objectmissing=System.Reflection.Missing.Value;

Applicationexcel=newApplication();//lauchexcelapplication

if(excel==null)

{

Response.Write("<script>alert('Can'taccessexcel')</script>");

}

else

{

excel.Visible=false;excel.UserControl=true;

//以只讀的形式打開EXCEL文件

Workbookwb=excel.Application.Workbooks.Open(strFileName,missing,true,missing,missing,missing,

missing,missing,missing,true,missing,missing,missing,missing,missing);

//取得第一個工作薄

Worksheetws=(Worksheet)wb.Worksheets.get_Item(1);

//取得總記錄行數(包括標題列)

introwsint=ws.UsedRange.Cells.Rows.Count;//得到行數

//intcolumnsint=mySheet.UsedRange.Cells.Columns.Count;//得到列數

//取得數據范圍區域(不包括標題列)

Rangerng1=ws.Cells.get_Range("B2","B"+rowsint);//item

Rangerng2=ws.Cells.get_Range("K2","K"+rowsint);//Customer

object[,]arryItem=(object[,])rng1.Value2;//getrange'svalue

object[,]arryCus=(object[,])rng2.Value2;

//將新值賦給一個數組

string[,]arry=newstring[rowsint-1,2];

for(inti=1;i<=rowsint-1;i++)

{

//Item_Code列

arry[i-1,0]=arryItem[i,1].ToString();

//Customer_Name列

arry[i-1,1]=arryCus[i,1].ToString();

}

Response.Write(arry[0,0]+"/"+arry[0,1]+"#"+arry[rowsint-2,0]+"/"+arry[rowsint-2,1]);

}

excel.Quit();excel=null;

Process[]procs=Process.GetProcessesByName("excel");

foreach(Processproinprocs)

{

pro.Kill();//沒有更好的方法,只有殺掉進程

}

GC.Collect();

}

3.方法三:將EXCEL文件轉化成CSV(逗號分隔)的文件,用文件流讀取(等價就是讀取一個txt文本文件)。


先引用命名空間:usingSystem.Text;和usingSystem.IO;

FileStreamfs=newFileStream("d:\Customer.csv",FileMode.Open,FileAccess.Read,FileShare.None);

StreamReadersr=newStreamReader(fs,System.Text.Encoding.GetEncoding(936));

stringstr="";

strings=Console.ReadLine();

while(str!=null)

{str=sr.ReadLine();

string[]xu=newString[2];

xu=str.Split(',');

stringser=xu[0];

stringdse=xu[1];if(ser==s)

{Console.WriteLine(dse);break;

}

}sr.Close();

另外也可以將資料庫數據導入到一個txt文件,實例如下:
//txt文件名

stringfn=DateTime.Now.ToString("yyyyMMddHHmmss")+"-"+"PO014"+".txt";

OleDbConnectioncon=newOleDbConnection(conStr);

con.Open();

stringsql="selectITEM,REQD_DATE,QTY,PUR_FLG,PO_NUMfromTSD_PO014";

//OleDbCommandmycom=newOleDbCommand("select*fromTSD_PO014",mycon);

//OleDbDataReadermyreader=mycom.ExecuteReader();//也可以用Reader讀取數據

DataSetds=newDataSet();

OleDbDataAdapteroda=newOleDbDataAdapter(sql,con);

oda.Fill(ds,"PO014");

DataTabledt=ds.Tables[0];

FileStreamfs=newFileStream(Server.MapPath("download/"+fn),FileMode.Create,FileAccess.ReadWrite);

StreamWriterstrmWriter=newStreamWriter(fs);//存入到文本文件中

//把標題寫入.txt文件中

//for(inti=0;i<dt.Columns.Count;i++)

//{

//strmWriter.Write(dt.Columns[i].ColumnName+"");

//}

foreach(DataRowdrindt.Rows)

{

stringstr0,str1,str2,str3;

stringstr="|";//數據用"|"分隔開

str0=dr[0].ToString();

str1=dr[1].ToString();

str2=dr[2].ToString();

str3=dr[3].ToString();

str4=dr[4].ToString().Trim();

strmWriter.Write(str0);

strmWriter.Write(str);

strmWriter.Write(str1);

strmWriter.Write(str);

strmWriter.Write(str2);

strmWriter.Write(str);

strmWriter.Write(str3);

strmWriter.WriteLine();//換行

}

strmWriter.Flush();

strmWriter.Close();

if(con.State==ConnectionState.Open)

{

con.Close();

}

『捌』 C# 如何把資料庫內的數據讀取出來並賦值給一自定義的變數

//連接資料庫假設為Access資料庫
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=domain.mdb;Persist Security Info=True";
OleDbConnection myconn = new OleDbConnection(strCon);
//查詢數據
DataSet ds = new DataSet();
try
{
myconn.Open();
OleDbDataAdapter oda = new OleDbDataAdapter("select * from A", myconn);
oda.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (myconn.State == System.Data.ConnectionState.Open)
{
myconn.Close();
}
}

//賦值
DataTable dt = ds.Table[0];
string x = dt.Rows[0]["a"].toString();
string y = dt.Rows[0]["b"].toString();
string y = dt.Rows[0]["c"].toString();

『玖』 EXCEL從資料庫提取數據

1、打開源數據文件,把首行中的對應的數據,復制行到空白位置。

『拾』 求教如何在 c/c++中讀取資料庫中表的數據!!!!!!!!!

int CountLines(char *filename) { ifstream ReadFile; int n=0; char line[512]; ReadFile.open(filename,ios::in);//ios::in 表示以只讀的方式讀取文件 if(ReadFile.fail())//文件打開失敗:返回0 { return 0; } else//文件存在 { while(!ReadFile.eof()) { ReadFile.getline(line,512,'\n'); n++; } return n; }

熱點內容
刷機安卓10狀態欄圓角如何修復 發布:2024-04-24 21:24:00 瀏覽:133
創建sql資料庫的命令是什麼 發布:2024-04-24 21:23:58 瀏覽:41
不是安卓該如何下載畫質怪獸 發布:2024-04-24 21:18:51 瀏覽:699
php載入網頁 發布:2024-04-24 21:15:20 瀏覽:159
遠程伺服器更換ip地址 發布:2024-04-24 21:14:43 瀏覽:526
什麼時候上線華為方舟編譯器軟體 發布:2024-04-24 21:10:21 瀏覽:629
安卓布局文件指的是哪個 發布:2024-04-24 21:08:05 瀏覽:887
磁帶存儲價格 發布:2024-04-24 21:04:22 瀏覽:900
誤刪除文件夾恢復工具 發布:2024-04-24 20:31:57 瀏覽:383
php介面編寫 發布:2024-04-24 20:31:06 瀏覽:68