資料庫顯示記錄
1. C#中查詢資料庫中記錄個數並顯示。
// 獲得數據寫入text(主方法)
private void count(object sender, EventArgs e)
{
string str1 = "select * from 表名 where 條件 ";//讀取整張表
MysqlConnection coon = MySQL.getMySqlCon();//連接資料庫,調方法
MySqlCommand mySqlCommand1 = MySQL.getSqlCommand(str1, coon);
coon.Open();
int record1 = MySQL.getResultCount(mySqlCommand1);
coon.Close();
label1.Text = record1.ToString();
}
//建立執行命令的語句對象
public static MySqlCommand getSqlCommand(String sql, MySqlConnection mysql)
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
return mySqlCommand;
}
//查詢記錄數
public static int getResultCount(MySqlCommand mySqlCommand)
{
MySqlDataReader reader = mySqlCommand.ExecuteReader();
int result = 0;
try
{
while (reader.Read())
{
if (reader.HasRows)
{
result = result + 1;
}
}
}
catch (Exception)
{
}
finally
{
reader.Close();
}
return result;
}
//此方法連接資料庫
public static MySqlConnection getMySqlCon()
{
String mysqlStr = "Database=test3;Data Source=127.0.0.1;User Id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;Allow Zero Datetime=True";
MySqlConnection mysql = new MySqlConnection(mysqlStr);
return mysql;
}
2. 如何在JTable中顯示資料庫中的記錄
查看一下SQL Server的日誌不就可以了:)
進入企業管理器,你的資料庫點開,"管理">>"SQL Server日誌",查看你那天的存檔就可以了。
唉,我可沒有說右擊阿:)
你點開你的資料庫,下面不是有一些選項么,"資料庫","數據轉換服務","管理"......就是這個管理阿。
3. Access資料庫中多表聯合查詢,並顯示記錄
select 商品名稱,規格, 進貨渠道, 備注記事 from 表a where 商品名稱 like ="*線*"
union
select 商品名稱,規格, 進貨渠道, 備注記事 from 表b where 商品名稱 like ="*線*"
union
select 商品名稱,規格, 進貨渠道, 備注記事 from 表c where 商品名稱 like ="*線*"
4. 新人求教SQL資料庫MSHFlexGrid控制項顯示記錄的問題
這部分代碼改成===下面的,代碼基本這樣,自己再優化下吧,不過限於mshflexgrid及本人水平,當出現學號相鄰的學生重名、相鄰科目成績相等的情況時,表格顯示會有些變化
Do While Not mrc.EOF '判斷是否移動到數據集對象的最後一條記錄
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = "" & mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = "" & mrc.Fields(1)
.TextMatrix(.Rows - 1, 2) = "" & mrc.Fields(2)
.TextMatrix(.Rows - 1, 3) = "" & mrc.Fields(3)
.TextMatrix(.Rows - 1, 4) = "" & mrc.Fields(4)
.TextMatrix(.Rows - 1, 5) = "" & mrc.Fields(5)
mrc.MoveNext
Loop
==========================================================
Dim idName As String '當前學生
Dim idCount As Integer '當前學生科目數
Dim idSum As Double '當前學生總成績
Dim j, j, k As Integer
Do While Not mrc.EOF '判斷是否移動到數據集對象的最後一條記錄
.Rows = .Rows + 1
.CellAlignment = 4
i = .Rows - 1
If idName = "" Or idName = mrc.Fields(0) Then
'同一個學生,則累計成績
idCount = idCount + 1
idSum = idSum + mrc.Fields(5)
idName = mrc.Fields(0)
Else
'學生改變,則計算前一個學生成績,然後清零,記錄下一個
For k = i - idCount To i - 1
.TextMatrix(k, 6) = idSum
.TextMatrix(k, 7) = idSum / idCount
Next k
.Refresh
idCount = 0
idSum = 0
idName = mrc.Fields(0)
End If
'所有行、制定列允許合並
.MergeRow(i) = True
.MergeCol(0) = True
.MergeCol(1) = True
.MergeCol(2) = True
.MergeCol(3) = True
.MergeCol(6) = True
.MergeCol(7) = True
.TextMatrix(.Rows - 1, 0) = "" & mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = "" & mrc.Fields(1)
.TextMatrix(.Rows - 1, 2) = "" & mrc.Fields(2)
.TextMatrix(.Rows - 1, 3) = "" & mrc.Fields(3)
.TextMatrix(.Rows - 1, 4) = "" & mrc.Fields(4)
.TextMatrix(.Rows - 1, 5) = "" & mrc.Fields(5)
mrc.MoveNext
Loop
For k = i - idCount To i - 1
.TextMatrix(k, 5) = idSum
.TextMatrix(k, 6) = idSum / idCount
Next k
.Refresh
idCount = 0
idSum = 0
idName = mrc.Fields(0)
5. 怎麼將資料庫記錄顯示在表格中
給你個思路首先可以使用集合也就是說通過連接資料庫返回所讀取的數據,再存到對象里,然後將對象添加到集合里去,不斷循環,知道讀完所有查詢到的數據;最後將集合返回在頁面上也可以使用循環將集合里的數據取出,再強制轉換為你之前所用到的對象類型,最後在使用表達式將結果顯示在頁面上例:<table><%循環開始取數據(輸出下面代碼)%><tr><td><%=數據%></td><td><%=數據%></td></tr><tr><td><%=數據%></td><td><%=數據%></td></tr><%循環結束%></table>
6. 怎樣用連貫操作把資料庫中最新的一條記錄顯示出來
不知你說的「連貫」操作方法是啥意思。
一般提取資料庫最新記錄,需要在數據表中有一個日期時間類型的欄位,用以記錄生成的時間點,然後以此欄位建立索引,使用order by time_file limit 1的SQL語句提取即可。
7. 怎樣查看資料庫表記錄
52.使用完錯誤代碼之後, 應該隨手清空它,以免造成下次錯誤捕獲不正確,我們使用一uju error變數()
53.為了防止多次資料庫操作導致資料庫不完整( )我們可以使用(colse)
54. connection 對象為我們提供了三個與事務相關的方法,請寫上它們:開啟事務:(connedtion.begintrans ),結束事務並保有更改:
connection(.committrans ),結束事務並卻小更改:connection(.rollbacktrans )
55.如果我們查得到數據表中某個欄位的信息需要用recordset對象的(fields )集合中的(name )對象。
56.取得field對象的方法是:set field=recordset.fields.item(i),我們可以通過field對象的(name)屬性得到欄位名,還可以通過(
type)屬性得到欄位的類型。
57.我們可以在記錄集內移動指針,以便操作不同的數據記錄,如果我們要將指針移到最後一條記錄,可以使用(recordset.movelast )方法
,將指針移到首記錄使用(recordset.movefirst )方法,移動到上一條記錄使用(recordset.previous )方法,移動到下一條記錄可使用(
recordset.movenext )方法。
58.刪除記錄,我們可以直接使用SQL語句中的delete語句,也可以將記錄查詢到之後使用recordset對象的( delete)方法來刪除當前記錄。
雖然直接使用Delete語句刪除記錄非常方便,但有時對於一些需要進行復雜判斷才決定是否刪除記錄的情況下,我們還是要使用recordset對象
的(delete )方法來刪除記錄。
59.更新一條記錄除可以使用update語句外,還可以將記錄查詢到之後,改變各欄位的值,然後使用recordset對象的( addnew )方法來更新
數據。雖然直接使用update語句更新數據十分方便,但有時對於需要進行一些不規則更新的時候還是要使用到recordset對象的(addnew )方
法來更新數據記錄。