C# 如何读取数据库

C# 如何读取数据库,第1张

可以用代码绑定:

using SystemDataSqlClient;

SqlConnection sqlcon = new SqlConnection();

sqlconConnectionString = ConfigurationManagerConnectionStrings["ConnectionString"]ToString(); // 这里修改你的连接字符串

sqlconOpen();

DataSet ds = new DataSet();

SqlDataAdapter sda = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand("select 列 from 表", sqlcon);

sdaFill(ds); // 将结果填充到ds里

comboBox1DisplayMember = "列"; // 这是属性是将数据显示到cbo里

comboBox1ValueMember = "列"; // 这个属性设置的列是隐藏的,可以通过SelectedValue获取

comboBox1DataSource = dsTables[0]DefaultView; //设置cbo的数据源

comboBox1DataBind(); // 如果是web程序,设置好datasource后一定要databind,不然数据没绑定上去

还有种傻瓜方式:

添加个SqlDataSource控件,配置好这个控件的数据源(通过向导可以很容易配置好);

然后把comboBox的DataSource属性设置为这个控件;

接着DisplayMember属性里选择列(ValueMember属性可以根据需要是否也选择)即可。

DataReader 的默认行为是在整个数据行可用时立即以行的形式加载传入数据 但是 对于二进制大对象 (BLOB) 则需要进行不同的处理 因为它们可能包含数十亿字节的数据 而单个行中无法包含如此多的数据 Command ExecuteReader 方法具有一个重载 它将采用 CommandBehavior 参数来修改 DataReader 的默认行为 您可以将 CommandBehavior SequentialAccess 传递到 ExecuteReader 方法来修改 DataReader 的默认行为 以便让 DataReader 按照顺序在接收到数据时立即将其加载 而不是加载数据行 这是加载 BLOB 或其他大数据结构的理想方案 在将 DataReader 设置为使用 SequentialAccess 时 务必要注意访问所返回字段的顺序 DataReader 的默认行为是在整个行可用时立即加载该行 这使您能够在读取下一行之前按任何顺序访问所返回的字段 但是 当使用 SequentialAccess 时 必须按顺序访问由 DataReader 返回的不同字段 例如 如果查询返回三个列 其中第三列是 BLOB 则必须在访问第三个字段中的 BLOB 数据之前返回第一个和第二个字段的值 如果在访问第一个或第二个字段之前访问第三个字段 则第一个和第二个字段值将不再可用 这是因为 SequentialAccess 已修改 DataReader 使其按顺序返回数据 当 DataReader 已经读取超过特定数据时 该数据将不可用 当访问 BLOB 字段中的数据时 请使用 DataReader 的 GetBytes 类型化访问器 该访问器将使用二进制数据填充 byte 数组 您可以指定要返回的特定数据缓冲区大小以及从返回的数据中读取的第一个字节的起始位置 GetBytes 将返回 long 值 它表示所返回的字节数 如果向 GetBytes 传递空的 byte 数组 所返回的长值将是 BLOB 中字节的总数 您可以选择将字节数组中的某索引指定为所读取数据的起始位置 以下示例从 Microsoft SQL Server 中的 pubs 示例数据库中返回发行者 ID 和徽标 发行者 ID (pub_id) 是字符字段 而徽标则是图形 即 BLOB 请注意 由于必须按顺序访问字段 所以将在访问徽标之前访问当前数据行的发行者 ID [Visual Basic]Dim pubsConn As SqlConnection = New SqlConnection(Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;)Dim logoCMD As SqlCommand = New SqlCommand(SELECT pub_id logo FROM pub_info pubsConn)Dim fs As FileStream Writes the BLOB to a file ( bmp) Dim bw As BinaryWriter Streams the binary data to the FileStream object Dim bufferSize As Integer = The size of the BLOB buffer Dim outbyte(bufferSize ) As Byte The BLOB byte() buffer to be filled by GetBytes Dim retval As Long The bytes returned from GetBytes Dim startIndex As Long = The starting position in the BLOB output Dim pub_id As String = The publisher id to use in the file name Open the connection and read data into the DataReader pubsConn Open()Dim myReader As SqlDataReader = logoCMD ExecuteReader(CommandBehavior SequentialAccess)Do While myReader Read() Get the publisher id which must occur before getting the logo pub_id = myReader GetString( ) Create a file to hold the output fs = New FileStream(logo & pub_id & bmp FileMode OpenOrCreate FileAccess Write)bw = New BinaryWriter(fs) Reset the starting byte for a new BLOB startIndex = Read bytes into outbyte() and retain the number of bytes returned retval = myReader GetBytes( startIndex outbyte bufferSize) Continue reading and writing while there are bytes beyond the size of the buffer Do While retval = bufferSizebw Write(outbyte)bw Flush() Reposition the start index to the end of the last buffer and fill the buffer startIndex = startIndex + bufferSizeretval = myReader GetBytes( startIndex outbyte bufferSize)Loop Write the remaining buffer bw Write(outbyte)bw Flush() Close the output file bw Close()fs Close()Loop Close the reader and the connection myReader Close()pubsConn Close()[C#]SqlConnection pubsConn = new SqlConnection(Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;);SqlCommand logoCMD = new SqlCommand(SELECT pub_id logo FROM pub_info pubsConn);FileStream fs; // Writes the BLOB to a file ( bmp) BinaryWriter bw; // Streams the BLOB to the FileStream object int bufferSize = ; // Size of the BLOB buffer byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes long retval; // The bytes returned from GetBytes long startIndex = ; // The starting position in the BLOB output string pub_id = ; // The publisher id to use in the file name // Open the connection and read data into the DataReader pubsConn Open();SqlDataReader myReader = logoCMD ExecuteReader(CommandBehavior SequentialAccess);while (myReader Read()){// Get the publisher id which must occur before getting the logo pub_id = myReader GetString( );// Create a file to hold the output fs = new FileStream(logo + pub_id + bmp FileMode OpenOrCreate FileAccess Write);bw = new BinaryWriter(fs);// Reset the starting byte for the new BLOB startIndex = ;// Read the bytes into outbyte[] and retain the number of bytes returned retval = myReader GetBytes( startIndex outbyte bufferSize);// Continue reading and writing while there are bytes beyond the size of the buffer while (retval == bufferSize){bw Write(outbyte);bw Flush();// Reposition the start index to the end of the last buffer and fill the buffer startIndex+= bufferSize;retval = myReader GetBytes( startIndex outbyte bufferSize);}// Write the remaining buffer bw Write(outbyte);bw Flush();// Close the output file bw Close();fs Close();}// Close the reader and the connection myReader Close();pubsConn Close(); lishixinzhi/Article/program/net/201311/12026

第一种方法:

改改SQL语句试试:

SELECT

sum(语文)

as

语文总分,avg(语文)

as

语文平均分

from

成绩

然后

xxxx

:=

ADOQueryFieldValues['语文总分'];

//取语文总分

xxxx

:=

ADOQueryFieldValues['语文平均分'];

//取语文平均分

第二种方法:逐行读取的语句

ADOQueryActive

:=

True;

zf

:=

0;

icount

:=

0;

while

not

ADOQueryEof

do

begin

zf

:=

zf

+

ADOQueryFieldValues['语文'];

//累加语文总分

Inc(icount);

ADOQueryNext;

end;

pjf

:=

zf

/

icount;

//

平均分

在for循环中,第一句就有问题,始终是该二维数组的第一维的数值在变化,第二维的数值始终是0,也就是第二维的第一位始终有值,其他的没有值

根据数组规则,在索引下标位置的地方,下标为0的是第一个数,下标为1的是第二个数

根据推理:

第二维数值的变化永远是第一位(下标是0),下标为1的值一直是默认0,所以结果就是0

建议你打个断点调试一下,注意数组以及变量的变化,分析原因

Sub 按钮1_Click()

Dim i As Integer, j As Integer, sht As Worksheet 'i,j为整数变量;sht 为excel工作表对象变量,指向某一工作表

'Dim cn As New ADODBConnection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用

'Dim rs As New ADODBRecordset '定义记录集对象,保存数据表

‘工具 ---〉引用 ---〉Microsoft ActiveX data objects

'下面两句就不需要增加引用ADO

Set cn = CreateObject("AdodbConnection")

Set rs = CreateObject("AdodbRecordset")

Dim strCn As String, strSQL As String '字符串变量

Dim strCond As String

strCn = "Provider=sqloledb;Server=R9HDET7;Database=dbname;Uid=username;Pwd=password" '定义数据库链接字符串

'下面的语句将读取数据表数据,并将它保存到excel工作表中:工作表为一张两维表,记录集也是一张两维表

strSQL = "select CUSTOMER_NAME from VSC_BI_CUSTOMER " '定义SQL查询命令字符串

cnOpen strCn '与数据库建立连接,如果成功,返回连接对象cn

rsOpen strSQL, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中

i = 2

Set sht = ThisWorkbookWorksheets("Test") '把sht指向当前工作簿的Test工作表

Do While Not rsEOF '当数据指针未移到记录集末尾时,循环下列 *** 作

shtCells(i, 1) = rs("CUSTOMER_NAME") '把当前记录的字段1的值保存到sheet1工作表的第i行第1列

rsMoveNext '把指针移向下一条记录

i = i + 1 'i加1,准备把下一记录相关字段的值保存到工作表的下一行

Loop '循环

rsClose '关闭记录集,至此,程序将把某数据表的字段1保存在excel工作表sheet1的第1列,行数等于数据表的记录数

cnClose '关闭数据库链接,释放资源

End Sub

提供另一个思路,不知道是不是适用你。

你可以使用客户端数据集。

你说的功能可灵活实现。你可以测试数据量的大小,即只处理数据表中的一部分数据,如100条。当然,客户端的刷新也只处理这100条。

另一个方式,你可以直接把新增的数据,放到客户端的数据集,可以删除的。其原理是记录在另一个记录表,记录变化的数据。这种方式,你逐条的处理都可以,很灵活。

还有,看你说的,不知道理解的对不对。你可以对数据感知组件,关掉刷新功能。这样数据在变化过程中不会刷新。在完成数据的处理后,再打开此功能。比如,你逐条对数据中的列做运算,在运算前可以关闭,处理完再打开。

不清楚的,可进一步沟通。

以上就是关于C# 如何读取数据库全部的内容,包括:C# 如何读取数据库、C#中读取数据库中Image数据、delphi 读取数据库中的数据并计算等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9542584.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存