在vs中用c#做了個(gè)windows窗體文件想把數(shù)據(jù)庫中查詢的東西顯示出來該用啥,怎么編碼
在vs中用c#做了個(gè)windows窗體文件想把數(shù)據(jù)庫中查詢的東西顯示出來該用啥,怎么編碼
拖動(dòng)DataGridView控件到窗體中,然后為DataGridView控件綁定數(shù)據(jù)就能顯示了在你的sql=string.Format(sql,textBox1.Text) 下面添加如下代碼:SqlDataAdapter da=new SqlDataAdapter(sql,conn);DataSet ds=new DataSet();da.Fill(ds);DataGridView1.DataSource=ds.Tables[0]; //DataGridView1為你拖動(dòng)到窗體的控件名稱
vs 2010 DataGridView顯示數(shù)據(jù)
你問題的重點(diǎn)到底是學(xué)習(xí)表格還是學(xué)習(xí)訪問數(shù)據(jù)庫?
如果重點(diǎn)是學(xué)習(xí)表格,那內(nèi)存里新建datatable,再添加列,再添加數(shù)據(jù),直接綁定就可以顯示
如果重點(diǎn)是學(xué)習(xí)數(shù)據(jù)庫,那你上面建立數(shù)據(jù)庫的方式是有點(diǎn)問題,應(yīng)該在數(shù)據(jù)庫里面建而不是在vs里創(chuàng)建mdf文件,即便是用mdf連接字符串也有問題,應(yīng)該是類似如下
Server=.\\SQLExpress;AttachDbFilename=C:\\MyFolder\\MyDataFile.mdf;Database=dbname;Trusted_Connection=Yes;你上面的代碼不報(bào)錯(cuò)是奇跡。
在VS.net中怎樣連接一個(gè)數(shù)據(jù)庫,并將他的內(nèi)容顯示出來
你有數(shù)據(jù)庫嗎?連接你的數(shù)據(jù)庫的位置,加他的 連接條件就可以拉下面是一個(gè)連接數(shù)據(jù)庫的模塊,大家可以使用它來連接SQL數(shù)據(jù)庫,使用起來比ADO控件還簡單:代碼如下:Public rsNew As ADODB.RecordsetPublic CnNew As ADODB.ConnectionPublic addFlag As Boolean\’連接數(shù)據(jù)庫Public Function OpenCn() As BooleanDim Mag As StringOn Error GoTo strErrMagSet CnNew = New ADODB.ConnectionCnNew.ConnectionTimeout = 25CnNew.Provider = \”sqloledb\”CnNew.Properties(\”data source\”).value = \”\” \’SQL服務(wù)器的名CnNew.Properties(\”initial catalog\”).value = \”pubs\”\’庫名CnNew.Properties(\”integrated security\”).value = \”SSPI\”\’登陸類型\’con.Properties(\”user id\”).value = \”sa\”\’con.Properties(\”password\”).value = \”wwww\”CnNew.OpenOpenCn = TrueaddFlag = TrueExit FunctionstrErrMag:Mag = \”數(shù)據(jù)庫末連接\”Call MsgBox(Mag, vbOKCancel, \”錯(cuò)誤:數(shù)據(jù)庫連接\”)addFlag = FalseEndEnd FunctionPublic Sub Clocn()\’閉關(guān)數(shù)據(jù)庫On Error Resume NextIf CnNew.State <> adStateClosed Then CnNew.CloseSet CnNew = NothingEnd SubPublic Function OpenRs百科(ByVal strSql As String) As Boolean\’連接數(shù)據(jù)庫記錄集Dim Mag As StringDim rpy As BooleanOn Error GoTo strErrMagSet rsNew = New ADODB.RecordsetIf addFlag = False Then rpy = OpenCnWith rsNew.ActiveConnection = CnNew.CursorLocation = adUseClient.CursorType = adOpenKeyset.LockType = adLockOptimistic.Open strSqlEnd WithaddFlag = TrueOpenRs = TrueExit FunctionstrErrMag:Mag = \”數(shù)據(jù)庫數(shù)據(jù)集末連接\”Call MsgBox(Mag, vbOKOnly, \”錯(cuò)誤:數(shù)據(jù)庫連接\”)OpenRs = FalseEndEnd FunctionPublic Sub cloRs()\’閉關(guān)數(shù)據(jù)集On Error Resume NextIf rsNew.State <> adStateClosed Then rsNew.CloseSet rsNew = NothingEnd Sub
VS中如何調(diào)用SQL數(shù)據(jù)庫中的數(shù)據(jù),然后顯示出來?
下文展示使用c# 獲取DataTabl額,然后綁定數(shù)據(jù),你自己根據(jù)不同的情況,分別對待吧!
public?DataSet?readDbExecProcReDataSet(string?procName,?object[]?objectValue)?{?DataSet?ds?=?new?DataSet();?using?(SqlConnection?c?=?new?SqlConnection(connStr))?{?try?{?c.Open();?SqlCommand?sqlCmd?=?new?SqlCommand(procName,c);?sqlCmd.CommandType?=?CommandType.StoredProcedure;?sqlCmd.CommandTimeout?=?1200;?sqlCmd.Parameters.Clear();?if?(objectValue?is?SqlParameter[])?{?sqlCmd.Parameters.AddRange(objectValue);?}?else?{?sqlCmd.Parameters.AddRange(getParameters(connStr?+?\”-\”?+?procName,?procName,?objectValue));?}?SqlDataAdapter?sqlAdapter?=?new?SqlDataAdapter();?sqlAdapter.SelectCommand?=?sqlCmd;?sqlAdapter.Fill(ds,?\”dsName\”);?sqlCmd.Parameters.Clear();?c.Close();?}?catch?(Exception?e)?{?}?}?return?ds;?}
如有疑問,及時(shí)溝通!
vs2010 c# 中l(wèi)abel如何顯示數(shù)據(jù)庫SQL server 2008中數(shù)據(jù)
str