c如何讀取SQL中的資料,C 中怎麼讀取SQL資料庫表中的資料後顯示在一個TEXTBOX中

時間 2022-06-30 11:05:03

1樓:

string strconn = "data source=資料庫伺服器名稱或ip;initial catalog=資料庫名稱;persist security info=true;user id=sa;password=password"

sqlconnection conn=new sqlconnection(strconn) ;

sqlcommand cmd = new sqlcommand();

cmd.commandtext = "select * from table";

cmd.connection = conn;

sqldataadapter da=new sqldataadapter (cmd);

conn.open();

dataset ds=new dataset ();

ds.fill(da);

da.dispose();

cmd.dispose()

conn.close();

conn.dispose();

c#中怎麼讀取sql資料庫表中的資料後顯示在一個textbox中

2樓:乖乖

string connstring=//連線字串string sql=「"//資料庫查詢語句connection con=new connection(connstring);

command cmd=new command(sql,con);

con.open();

sqldatareader dr=cmd.excutereader;

if(dr.read())

dr.close();

con.close();

3樓:匿名使用者

樓上所說可以··

但是如果確定是身份證的話···

那就是唯一的···

唯一的用model 物件返回單一的足以···不需要用到dataset

不知道你的專案中寫了實體類沒有!

返回一個實體類就足以

4樓:匿名使用者

先定義一個textbox空間 名為tb1

點選按鈕執行

select * from man where 身份證號=『「+tb1.text+」』

返回datatable資料 將dt中的資料dt.row[*][「姓名」]dt.row[*][「年齡」]賦值給tb1

例如tb1.text=「姓名:」+dt.row[*][「姓名」].tostring()+"年齡:"+dt.row[*][「年齡」].tostring()

不知道這樣能不能實現你的功能

5樓:天地樑心

把資料讀到之後存在一個變數裡面,然後把變數顯示在textbox就行了啊

6樓:百度使用者

把sql語句查出的資料放進一張表裡,在逐個顯示就行了,判斷查出的表的行數是否為0,如果為0則用messagebox顯示「所查資料不存在」

c#從sql讀取表中的資料

c# 應用程式如何讀取sql資料

7樓:匿名使用者

private void form1_load(object sender, eventargs e)

rd.close();

}拼寫問題自己改一下。

8樓:匿名使用者

如果查詢結果只有一行:

messagebox.show(convert.tostring(cmd.executescalar()));

如果查詢結果有多行:

sqldatareader dr = cmd.executereader();

while (dr.read())

關鍵掌握cmd.executenonquery(); cmd.executereader();cmd.

executescalar();三種方法,上網查一查。msdn也會對你很有幫助。

c#讀取sql中的一個數值

9樓:來元彤

//抱歉,手邊沒有編譯器,但應該沒問題!~

這是一種,我認為最簡單:

conn.open();

sql="select studentage from student where studentname="+textbox1.text.trim();

sqlcommand cmd=new sqlcommand (sql,conn);

object i=cmd.executescalar();

textbox2.text=i.tostring();

conn.close();

樓上用的

using(sqldatareader dr = cmd.executereader())

為二種方法,但沒有關閉資料庫

第三種是用 sqldataadapt

ex:sqldataadapter da = new sqldataadapter(sql, conn);

dataset ds = new dataset();

//填充資料

da.fill(ds);

textbox2.text=ds.table[0].rows[0][0].tostring();

不要忘記關閉資料庫!~

10樓:

sql="select studentage from student where studentname="+textbox1.text.trim();

然後讀取就可以了呀

11樓:匿名使用者

string sql=("select stuentage from student where studentname=",textbox1.text.trim());

12樓:匿名使用者

以sqlserver為例,大概**如下。

strsql="select studentage from student where studentname="+textbox1.text.trim(); //拼接sql語句(注:

這不是最好的方式,會存在注入漏洞,但按你現問的這個問題,還沒到要考慮這個漏洞的時候,只要會這麼用就可以了)

sqlconnection connection = new sqlconnection("connectionstring資料庫連線串")

connection.open()

sqlcommand cmd = new sqlcommand(strsql,connection )//connection 為你建立的連線物件

using(sqldatareader dr = cmd.executereader())

} 還不明白可參考msdn幫助手冊

c#如何讀取sql中的image資料

13樓:匿名使用者

image資料型別不一定就是**,也可以是物件

你可以讀取出來轉換為 byte,然後再轉化為你儲存時的型別

14樓:匿名使用者

sql2005可以存**,就是轉換成二進位制了。

一般存**的話,我是存**路徑,不把**存進資料庫中,

15樓:匿名使用者

sql能存image?

c#如何讀取資料庫中某一列中的值

16樓:

連線資料庫後,先用sql語句取出資料庫中的這一列,然後用if迴圈,例如:用datareader則用read()方法

17樓:老狼的詩

如果程式讀的只一張表的內容請參考下方**

datatable dt=new datatable();

using (sqldataadapter adapter = new sqldataadapter(sql, conn))

} finally}

18樓:ts毛毛雨

請再說詳細點兒,關於那個if

c 中關於getline函式讀取資料的問題

getline的原型如下 getline char c,int i,char c 表示讀入i個字元,或者遇到結束符c為止的字元數,儲存到c中。getline char int 表示讀入i個字元到c中。注意讀入的字元數應比實際的大1,因為讀入的是字串,字串將會以 0 作為結束,如果你要讀入3個字元,那...

在C中,如何使用sql語句向資料庫中新增資訊

禹仙居安然 正常情況下,sql server中能實現的語句在c 中aqlcommand中執行是沒有問題,我們做開發的時候一般是sql server中先驗證查詢語句沒問題了才往c 中新增。您這裡導致這種情況的可能是 c 中字串的拼裝需要注意的一些問題,你可以在執行sqlcommand的 處斷點,然後檢...

c向sql中插入資料時如何自動生成編號作為主鍵

不識臺北路 建立 的時候將主鍵設定為identitycreate table test id int primary key identity,value nvarchar 128 not null 插入資料的時候,不要給主鍵賦值,主鍵會自動增長insert into test values val...