標籤:c# sqlserver
資料的查詢:private void FullTab() { SqlConnection con1 = new SqlConnection();//建立資料庫庫連結 try { String con = "server=.;database=common_wjdl;uid=sa;pwd=db2008mima";//定義資料庫連接資訊 con1.ConnectionString = con; con1.Open();//開啟資料庫 String sql = "select * from Fileuploaded";//建立語句查詢所有資料 SqlDataAdapter ada1 = new SqlDataAdapter(sql, con);//SqlDataAdapter 對象。 用於填充DataSet (資料集)。 DataSet myset = new DataSet(); SqlCommand cmd = con1.CreateCommand();//建立資料庫命令 cmd.CommandText = sql; SqlDataReader reader = cmd.ExecuteReader(); //從資料庫中讀取資料流存入reader中 } catch (SqlException ee1) { Console.WriteLine(ee1.Message);//列印異常資訊 } catch (IndexOutOfRangeException ee2) { Console.WriteLine(ee2.Message);//列印異常資訊 } } 資料的插入:(大致都一樣,只需修改SQL語句就行) String con = "server=.;database=common_wjdl;uid=sa;pwd=db2008mima";//定義資料庫連接資訊 con1.ConnectionString = con; con1.Open();//開啟資料庫 String sql = "insert into Fileuploaded (FileID,FileName,FilePath,QRCodePath,FileSuffix) Values(‘"+fileNameSave+"‘,‘"+fileName+"‘,‘"+filePath+"‘,‘"+filePath+"‘,‘"+fileSuffix+"‘)";//建立語句向資料庫插入資料 SqlCommand cmd = con1.CreateCommand();//建立資料庫命令 cmd.CommandText = sql; cmd.ExecuteNonQuery();
本文出自 “技術的秘密-劉聰聰的創作” 部落格,請務必保留此出處http://liucongcong.blog.51cto.com/11527480/1931683
C#連結SQLServer實現插入和查詢資料來源代碼