C#比如我写了个Sql语句:"SELECT * FROM Student".然后我将查询回来的数据绑定到DataGraView中..但是数据 C#连接数据库,查询后怎么输出查询结果?
C#\u7684SqlParameter\u662f\u600e\u4e48\u7528\u7684\uff0c\u6bd4\u5982\u6211\u60f3\u628a"select * from student where \u4e13\u4e1a = \u2018\u8ba1\u7b97\u673a\u2019"\uff0c\u8fd9\u53e5\u6539\u6210\u7528SqlParameter[] sp=new SqlParameter[2]; //\u53c2\u6570\u4e2a\u6570\u548c\u5b58\u50a8\u8fc7\u7a0b\u53c2\u6570\u4e2a\u6570\u76f8\u540c
sp[0]=new SqlParameter(\u53c2\u6570\u540d\uff0c\u6570\u503c); //\u53c2\u6570\u540d\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u53c2\u6570\u540d\u5fc5\u987b\u76f8\u540c
sp[1]=new SqlParameter(\u53c2\u6570\u540d\uff0c\u6570\u503c);
\u5728\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b\u65f6\u5c06sp\u7684\u53c2\u6570\u5faa\u73af\u6dfb\u52a0\u7ed9SqlCommand:
foreach (SqlParameter parm in sp){
cmd.Parameters.Add(parm);
}
C#\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u67e5\u8be2\u540e\u600e\u4e48\u8f93\u51fa\u67e5\u8be2\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e0b\u65b9\u5f0f
1\uff1a\u8f93\u51fa\u7684\u7ed3\u679c\u53ef\u4ee5\u662f\u96c6\u5408\u5bf9\u8c61
2\uff1a\u8f93\u51fa\u7684\u7ed3\u679c\u53ef\u4ee5\u662fDataSet\u6216\u8005DataTable
\u4e0b\u9762\u7684\u6848\u4f8b\u63d0\u4f9b\u4e86\u4e24\u79cd\u65b9\u5f0f\uff1a
\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u96c6\u5408\u5bf9\u8c61\uff0c\u5185\u90e8\u4f7f\u7528\u7684\u662fDataSet
public IList GetAllStudents1()
{
IList dataList = new List();
DataSet dataSet = new DataSet();\u3000\u3000// \u58f0\u660e\u5e76\u521d\u59cb\u5316DataSet
SqlDataAdapter dataAdapter;\u3000\u3000 // \u58f0\u660eDataAdapter
using (SqlConnection conn = new SqlConnection(connString))
{
// \u5b9a\u4e49command\u5bf9\u8c61
SqlCommand command = new SqlCommand(dboOwner + ".usp_SelectStudentsAll", conn);
command.CommandType = CommandType.StoredProcedure;
//Command\u5b9a\u4e49\u5e26\u53c2\u6570\u7684SQL\u8bed\u53e5\u7684\u53c2\u6570
//command.Parameters.Add("@stuID", SqlDbType.Int);
//\u7ed9\u8f93\u5165\u53c2\u6570\u8d4b\u503c
//command.Parameters["@stuID"].Value = 5;
conn.Open();
// \u521d\u59cb\u5316 DataAdapter
dataAdapter = new SqlDataAdapter(command);
// \u586b\u5145 DataSet
dataAdapter.Fill(dataSet, "dataSetName");
// \u5904\u7406\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e
foreach (DataRow row in dataSet.Tables[0].Rows)
{
Student model = new Student();
model.StuId = Convert.ToInt32(row["stuID"]);
model.StuName = Convert.ToString(row["stuName"]);
dataList.Add(model);
}
}
return dataList;
}
Create View V_Student AS
Select name, case when sex=1 then '男' else ‘女' end as sex, age,
case when grade=1 then '一年级' when grade=2 then '二年级' end as grade
From Student
之后再把查询该视图返回的数据绑定到DataGridView上.
你再C# load里面遍历一遍DataGraView
-----------------------
我搞成ASP.net了 。。。。
其实你可以处理sql语句达到你要的效果
select when sex case true then ‘男’ case false '女' from studect 限于oracle
如果你用的是sqlserver 网上搜搜
oracle:select decode(sex,true,'男','女') as sex from sutdent;
sqlserver:select case sex when true then '男' else '女' end ;from student;
把SQL语句用decode修改下呗,
绛旓細1銆佲C鈥滄槸鎷奸煶鈥渃hu鈥濈殑棣栧瓧姣嶏紝涓轰簡鏂逛究璧疯缂╁啓涓衡淐鈥濓紝鎸囩殑鏄鐢枫佸濂崇殑鎰忔濄2銆丼C鏄痵uperchat鐨勭缉鍐欙紝鏄粯璐圭暀瑷鍔熻兘銆傝繖涓姛鑳芥渶寮濮嬫槸YouTube2017骞翠笂绾跨殑锛孊绔欎篃鎺ㄥ嚭浜嗚繖涓姛鑳姐傝繖鏄竴绉嶅湪鐩存挱杩囩▼涓繛鎺ョ矇涓濅笌涓绘挱鐨勬柟寮忥紝鍚屾椂涔熷彲缁欎富鎾甫鏉ョ浉搴旂殑缁忔祹鏀剁泭銆傞氳繃杩欎竴鍔熻兘锛岀矇涓濆彲浠ヤ互鏄庝寒...
绛旓細C鏄暟瀛︿腑鐨勪竴绉嶅父鏁帮紝甯稿嚭鐜板湪鍚勭寮忓瓙涓傚叾浠h〃鐨勬槸涓涓浐瀹氱殑鏁板硷紝閫氬父鐢ㄦ潵琛ㄧず鏌愮鐗瑰畾鐨勭墿鐞嗛噺鎴栬呮暟瀛﹀父閲忋侰鐨勬剰涔夊湪涓嶅悓鐨勪笂涓嬫枃涓湁鎵涓嶅悓锛屾瘮濡侰鍙兘浠h〃鍏夐燂紝鍦嗗懆鐜囨垨鑰呭叾浠栨暟瀛︿笂鐨勫父閲忋傚湪鏁板涓婏紝C浠h〃浜嗛渶瑕佺敤浣滆绠楀熀纭鐨勪竴浜涢噸瑕佹暟鍊硷紝鍥犳涓嶅悓鐨凜鍊艰骞挎硾搴旂敤浜庡悇绉嶆暟瀛﹀垎鏀腑銆傚湪...
绛旓細C鎸囩殑.鏄幇鍦ㄦ祦琛岀殑C鍨嬬埍鎯呰銆侰鍨嬬埍鎯呰 鈥淐鍨嬬埍鎯呰鈥濇寚鐨勬槸瀵圭幇浠g埍鎯呭叧涓绉嶆柊鐨勫畾涔夈傚湪鐖辨儏涓紝鏈夎繘鏈夐銆佺壍鎵嬪垎鎵嬬殑寰幆锛屽凡涓嶅啀鏄釜绋濂囬鏅紝鑰屸滄湁濮嬫湁缁堚濓紝鍦ㄢ淐鍨嬬埍鎯呰鈥濅腑鏈変簡宕柊鐨勫畾涔夆斺旀湁寮濮嬶紝灏卞繀鏈夌粓浜嗕箣鏃躲傚畠鐨勬渶澶х壒鐐瑰湪浜庝汉浠笉鍐嶅皢闈掓槬鑰楁崯鍦ㄤ笉鑹叧绯讳笂銆 C鍨...
绛旓細C鏈変粈涔堢壒娈婂惈涔1 C锛堝ぇ鍐欙級 銆c锛堝皬鍐欙級鏄嫳鏂囧瓧姣嶉『鏁扮涓変釜锛屼縿璇瓧姣嶉『鏁扮19涓備緥濡傦細鑻辫鍗曡瘝cloud鍜屸滆嫃鑱斺濈殑淇勮缂╁啓小小小袪鐨勭涓涓瓧姣嶅氨鏄痗銆傚瓧姣嶇殑鍚箟 鍦ㄦ暟瀛﹀強璁$畻鏈虹瀛︿腑锛岃〃绀哄崄鍏繘鍒剁殑12銆侻S-DOS锛2.0鎴栦互鍚庣増鏈級鍙婂井杞绐椾綔涓氱郴缁熺О绗竴涓疄浣撶‖鐩樹笂鐨勫彲寮鏈猴紙active锛...
绛旓細c鐨勬剰鎬濓細1銆佸湪鍖栧涓紝琛ㄧず纰崇殑鍖栧绗﹀彿銆2銆佸湪涔愮悊涓紝琛ㄧず锛氶煶闃朵腑鐨C闊筹紝璋冨彿涓簬C闊冲紑濮嬬殑闊充箰鐨凜澶ц皟鍙奀灏忚皟锛屾媿瀛愯鍙蜂腑鐨4/4鎷嶅瓙銆3銆佸湪缃楅┈鏁板瓧涓紝琛ㄧず100銆4銆佸湪鍥介檯鍗曚綅鍒朵腑锛岃〃绀虹數鑽烽噺鐨勫崟浣嶁滃簱浠戔濄5銆佸湪璁$畻鏈虹瀛︿腑锛屾湁C璇█銆丆++銆丆#銆丱bjective-C绛夈6銆佸湪钀ュ吇瀛︿腑锛岃〃绀...
绛旓細c鏄瓧姣嶇鍙枫C锛堝ぇ鍐欙級 銆乧锛堝皬鍐欙級鏄嫳鏂囧瓧姣嶉『鏁扮涓変釜锛屼縿璇瓧姣嶉『鏁扮19涓備緥濡傦細鑻辫鍗曡瘝cloud鍜屸滆嫃鑱斺濈殑淇勮缂╁啓小小小袪鐨勭涓涓瓧姣嶅氨鏄痗銆傝捣婧愶細锛1锛夊瓧姣岰鐨勪骇鐢熷彲鑳芥槸鐢变簬涓涓姇鎺锋鐨勭鍙凤紝鍍忓湪鍙ゅ焹鍙婄殑璞″舰鏂囧瓧閲岋紝骞跺緢鏃╁嚭鐜板湪闂棌鐨勪功闈㈠綋涓-澶х害鍦ㄥ叕鍏冨墠1500骞寸殑瑗垮鍗婂矝銆傦紙...
绛旓細c鍦ㄧ埍鎯呯殑鍚箟鏄粈涔,鐢ㄥ悇绉嶅悇鏍风殑绗﹀彿鏉ヤ唬鏇挎垜浠兂瑕佽〃杈剧殑鐪熷疄鎰忔濇槸鎴戜滑瀵逛竴浠芥劅鎯呯殑鍚搫琛ㄧず,涓栭棿涓囩墿瀛樺湪鐨勬剰涔夊氨鏄垜浠祴浜堢殑鍐呮兜,涓嬮潰鍒嗕韩c鍦ㄧ埍鎯呯殑鍚箟鏄粈涔堛 c鍦ㄧ埍鎯呯殑鍚箟鏄粈涔1 C鎸囩殑鏄幇鍦ㄦ祦琛岀殑C鍨嬬埍鎯呰銆 C鍨嬬埍鎯呰 鈥淐鍨嬬埍鎯呰鈥濇寚鐨勬槸瀵圭幇浠g埍鎯呭叧涓绉嶆柊鐨勫畾涔夈傚湪鐖辨儏涓,鏈夎繘鏈夐銆...
绛旓細C鍦ㄥ寲瀛︿腑鏈変袱绉嶅惈涔夛細1銆佽〃绀烘祿搴︼紝鍗曚綅涓簃ol/L锛岃绠楀紡涓猴細C=n/V. C=1000蟻蠅/M銆傚惈涔夛細浠1鍗囨憾娑蹭腑鎵鍚憾璐ㄧ殑鎽╁皵鏁拌〃绀虹殑娴撳害銆備互鍗曚綅浣撶Н閲屾墍鍚憾璐ㄧ殑鐗╄川鐨勯噺锛堟懇灏旀暟锛夋潵琛ㄧず婧舵恫缁勬垚鐨勭墿鐞嗛噺锛屽彨浣滆婧惰川鐨勬懇灏旀祿搴︼紝鍙堢О璇ユ憾璐ㄧ墿璐ㄧ殑閲忔祿搴︺傛憾璐ㄥ惈閲忚秺澶氾紝娴撳害瓒婂ぇ銆傛祿搴﹀彲浠ョ敤涓瀹...
绛旓細鏁板绗﹀彿c鍙充笂瑙掓槸1鍙充笅瑙掓槸4鐨勬剰鎬濇槸鏁板涓殑缁勫悎锛屽彸涓嬭鐨4琛ㄧず鎬讳綋涓暟锛屽彸涓婅鐨1琛ㄧず鎶藉彇涓暟锛屼笖浠4涓噷闈㈡娊鍙栦竴涓紝C¹₄=4梅1=4銆傜粍鍚堬細涓鑸湴锛屼粠n涓笉鍚岀殑鍏冪礌涓紝浠诲彇m锛坢鈮锛変釜鍏冪礌涓轰竴缁勶紝鍙綔浠巒涓笉鍚屽厓绱犱腑鍙栧嚭m涓厓绱犵殑涓涓粍鍚堛傛墍鏈夎繖鏍风殑缁勫悎鐨勬绘暟...
绛旓細c++鐨勫叿浣撳惈涔夊涓嬶細C++鏄C璇█鐨勭户鎵匡紝瀹冩棦鍙互杩涜C璇█鐨勮繃绋嬪寲绋嬪簭璁捐锛屽張鍙互杩涜浠ユ娊璞℃暟鎹被鍨嬩负鐗圭偣鐨勫熀浜庡璞$殑绋嬪簭璁捐锛岃繕鍙互杩涜浠ョ户鎵垮拰澶氭佷负鐗圭偣鐨勯潰鍚戝璞$殑绋嬪簭璁捐銆侰++鎿呴暱闈㈠悜瀵硅薄绋嬪簭璁捐鐨勫悓鏃讹紝杩樺彲浠ヨ繘琛屽熀浜庤繃绋嬬殑绋嬪簭璁捐锛屽洜鑰孋++灏遍傚簲鐨勯棶棰樿妯¤岃锛屽ぇ灏忕敱涔嬨侰++涓嶄粎鎷ユ湁...