C#連結SQLServer實現插入和查詢資料來源代碼

來源:互聯網
上載者:User

標籤: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實現插入和查詢資料來源代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.