当前位置:首页 » 操作系统 » 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; }

热点内容
中文解压缩文件 发布:2024-05-06 05:13:24 浏览:195
android短信删除 发布:2024-05-06 04:58:42 浏览:259
安卓手机236开发者选项在哪里 发布:2024-05-06 04:11:13 浏览:260
sql过滤条件 发布:2024-05-06 04:05:18 浏览:563
ifconfiglinux 发布:2024-05-06 03:47:59 浏览:533
c语言开发集成环境 发布:2024-05-06 03:47:06 浏览:607
脚本uzi比赛视频 发布:2024-05-06 03:46:19 浏览:824
php给文本框赋值 发布:2024-05-06 03:21:24 浏览:26
androidjsonkey 发布:2024-05-06 03:07:31 浏览:732
python主线程子线程 发布:2024-05-06 03:07:20 浏览:764