日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

数据库方面的操作示例

發(fā)布時間:2023/12/10 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库方面的操作示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1 連接SQL Server數(shù)據(jù)庫示例 // 連接字符串string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"]; // 創(chuàng)建SqlConnection對象SqlConnection connection = new SqlConnection(ConnectionString); try{// 打開數(shù)據(jù)庫連接connection.Open(); myLabel.Text = "連接數(shù)據(jù)庫成功";}catch{myLabel.Text = "連接數(shù)據(jù)庫失敗";}finally{// 關(guān)閉數(shù)據(jù)庫連接connection.Close(); }<appSettings><add key="ConnectionSqlServer" value="Server=(local);User id=sa;Pwd=sa;Database=Northwind"></add><add key="ConnectionSqlServer1" value="Server=(local);User id=sa;Pwd=sa;"></add><add key="ConnectionSqlServer_tempdb" value="Server=(local);User id=sa;Pwd=sa;Database=tempdb"></add><add key="ConnectionDB2" value="DATABASE=SAMPLE;UID=username;PWD=password"></add><add key="ConnectionOracle" value="Data Source=Oracle8i;Integrated Security=yes"></add></appSettings> <system.web>2 // 連接到 ACCESS 的連接字符串string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" + Server.MapPath("grocertogo.mdb");// 使用OleDb .NET數(shù)據(jù)提供程序創(chuàng)建連接OleDbConnection oleConnection = new OleDbConnection(ConnStr);try{// 打開數(shù)據(jù)庫連接oleConnection.Open();// 顯示連接成功信息myLabel.Text = "Access數(shù)據(jù)庫連接狀態(tài):" + oleConnection.State;}catch(Exception ex){// 如果出現(xiàn)異常,顯示異常信息myLabel.Text = "Access數(shù)據(jù)庫連接狀態(tài):" + ex.ToString();}finally{// 關(guān)閉數(shù)據(jù)庫連接oleConnection.Close(); }3 // 連接到 Oracle 數(shù)據(jù)庫示例string ORACLE_ConnStr = "Data Source=Oracle8i;Integrated Security=yes";// 創(chuàng)建 OracleConnection 對象OracleConnection oConnection = new OracleConnection(ORACLE_ConnStr);try{oConnection.Open();myLabel.Text = "連接到 Oracle 數(shù)據(jù)庫";}catch(Exception ex){myLabel.Text = ex.ToString();}finally{oConnection.Close();} 4 SqlCommand 執(zhí)行SQL命令示例string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];// 創(chuàng)建SqlConnection對象// 創(chuàng)建Command對象SqlConnection thisConnection = new SqlConnection(ConnStr);SqlCommand thisCommand = new SqlCommand();// 關(guān)聯(lián)Connection對象// 賦值SQL語句到CommandText屬性// 指定命令類型是Sql語句thisCommand.Connection = thisConnection;thisCommand.CommandText = "SELECT COUNT(*) FROM Employees";thisCommand.CommandType = CommandType.Text;try{// 打開數(shù)據(jù)庫連接thisCommand.Connection.Open();// 獲取查詢結(jié)果myLabel.Text = thisCommand.ExecuteScalar().ToString();}catch(SqlException ex){// 如果出現(xiàn)異常,在Label標簽中顯示異常信息myLabel.Text = ex.ToString();}finally{// 關(guān)閉數(shù)據(jù)庫連接thisCommand.Connection.Close();}5 SqlDataReader 讀取數(shù)據(jù)示例string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];string Sql = "SELECT LastName, FirstName FROM Employees";SqlConnection thisConnection = new SqlConnection(ConnectionString);SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);thisCommand.CommandType = CommandType.Text;try{// 打開數(shù)據(jù)庫連接thisCommand.Connection.Open();// 執(zhí)行SQL語句,并返回DataReader對象SqlDataReader dr = thisCommand.ExecuteReader();// 以粗體顯示標題myLabel.Text = "<b>LastName FirstName</b><br>";// 循環(huán)讀取結(jié)果集while(dr.Read()){// 讀取兩個列值并輸出到Label中myLabel.Text += dr["LastName"] + " " + dr["FirstName"] + "<br>";}// 關(guān)閉DataReaderdr.Close();}catch(SqlException ex){// 異常處理Response.Write(ex.ToString());}finally{// 關(guān)閉數(shù)據(jù)庫連接thisCommand.Connection.Close();}6 使用DataAdapter填充數(shù)據(jù)到DataSetstring ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];string Sql = "SELECT EmployeeID, LastName, FirstName,Title, TitleOfCourtesy, BirthDate FROM Employees";// 創(chuàng)建SqlConnection對象// 創(chuàng)建DataAdapter對象并初始化SqlConnection thisConnection = new SqlConnection(ConnectionString);SqlDataAdapter adapter = new SqlDataAdapter(Sql, thisConnection);// 創(chuàng)建DataSet對象DataSet data = new DataSet();// 填充數(shù)據(jù)到DataSetadapter.Fill(data);// 數(shù)據(jù)綁定myDataGrid.DataSource = data;myDataGrid.DataBind();7 使用DataTable存儲數(shù)據(jù)庫表內(nèi)容string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];string Sql = "SELECT EmployeeID, LastName, FirstName, BirthDate FROM Employees";// 創(chuàng)建SqlConnection、SqlDataAdapter對象SqlConnection thisConnection = new SqlConnection(ConnectionString);SqlDataAdapter adapter = new SqlDataAdapter(Sql, thisConnection); // 創(chuàng)建DataTable對象DataTable table = new DataTable();// 填充數(shù)據(jù)到DataTableadapter.Fill(table);// 將DataTable綁定到DataGrid控件myDataGrid.DataSource = table;myDataGrid.DataBind();8 將數(shù)據(jù)庫數(shù)據(jù)填充到 XML 文件 // 連接字符串及 SQL 語句string ConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];string Sql = "SELECT CustomerID,CompanyName,Country FROM Customers";// 連接 SqlConnection 及 SqlDataAdapter 對象SqlConnection thisConnection = new SqlConnection(ConnString);SqlDataAdapter adapter = new SqlDataAdapter(Sql, thisConnection);// 創(chuàng)建 DataSet 對象DataSet data = new DataSet();// 填充 DataSetadapter.Fill(data, "Customers");// 將 DataSet 數(shù)據(jù)其及架構(gòu)填充到 Xml 文件data.WriteXml(Server.MapPath(".") + "\\myXml.xml", XmlWriteMode.WriteSchema);// 提示填充是否成功Label1.Text = "填充到XML文件成功";9 ASP.NET 使用存儲過程// 連接字符串string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];// 創(chuàng)建Connection對象SqlConnection myConn = new SqlConnection(ConnStr);// 創(chuàng)建Command對象并和Connection對象關(guān)聯(lián)SqlCommand myCommand = new SqlCommand();myCommand.Connection = myConn;// 指定要執(zhí)行的存儲過程名稱myCommand.CommandText = "CustomersProc";// 使用要執(zhí)行的是存儲過程myCommand.CommandType = CommandType.StoredProcedure;// 創(chuàng)建DataAdapter對象填充數(shù)據(jù)DataSet myDS = new DataSet();SqlDataAdapter adapter = new SqlDataAdapter(myCommand);adapter.Fill(myDS, "Customers");// 將返回的數(shù)據(jù)和DataGrid綁定顯示myDataGrid.DataSource = myDS.Tables["Customers"];myDataGrid.DataBind();10 使用帶輸入?yún)?shù)的存儲過程 string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];// 創(chuàng)建數(shù)據(jù)庫操作對象SqlDataAdapter myAdapter = new SqlDataAdapter();SqlCommand myCommand = new SqlCommand();myCommand.Connection = new SqlConnection(ConnStr);DataTable dt = new DataTable();// 指定要調(diào)用的存儲過程名稱 "Customer_Select"// 指定SqlCommand對象的命令類型為 "StoredProcedure"枚舉值myCommand.CommandText = "Customer_Select";myCommand.CommandType = CommandType.StoredProcedure;// 創(chuàng)建SqlParameter對象,指定參數(shù)名稱、數(shù)據(jù)類型、長度及參數(shù)值SqlParameter para = new SqlParameter("@country", SqlDbType.NVarChar, 15);para.Value = DropDownList1.SelectedValue;myCommand.Parameters.Add(para);// 關(guān)聯(lián)SqlDataAdapter與SqlCommand對象myAdapter.SelectCommand = myCommand;myAdapter.Fill(dt);// 綁定DataGridDataGrid1.DataSource = dt;DataGrid1.DataBind();11 使用帶輸入、輸出參數(shù)的存儲過程示 string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];// 創(chuàng)建 Connection 和 Command 對象SqlConnection myConn = new SqlConnection(ConnStr);SqlCommand myCommand = new SqlCommand("EmployeesProc", myConn);// 指定要執(zhí)行的命令為存儲過程myCommand.CommandType = CommandType.StoredProcedure;// 增加輸入?yún)?shù)并賦值myCommand.Parameters.Add("@TitleOfCourtesy", SqlDbType.NVarChar, 20);myCommand.Parameters["@TitleOfCourtesy"].Value = myDropDownList.SelectedItem.Text;myCommand.Parameters["@TitleOfCourtesy"].Direction = ParameterDirection.Input;// 增加輸出參數(shù)myCommand.Parameters.Add("@empCount", SqlDbType.Int);myCommand.Parameters["@empCount"].Direction = ParameterDirection.Output;// 創(chuàng)建 DataAdapter 對象填充數(shù)據(jù)DataSet myDS = new DataSet();SqlDataAdapter adapter = new SqlDataAdapter(myCommand);adapter.Fill(myDS, "Customers");// 使用 Label 控件顯示輸出參數(shù)的輸出值rtnLabel.Text = myCommand.Parameters["@empCount"].Value.ToString();// 將返回的數(shù)據(jù)和 DataGrid 綁定顯示myDataGrid.DataSource = myDS.Tables["Customers"];myDataGrid.DataBind();12 獲得數(shù)據(jù)庫中表的數(shù)目和名稱 獲取服務器端數(shù)據(jù)庫列表 string listQuery = "SELECT name FROM sysobjects WHERE xtype = 'U'"; string sumQuery = "SELECT COUNT(*) FROM sysobjects WHERE xtype = 'U'string db_query = "sp_helpdb";13 保存圖片到SQL Server數(shù)據(jù)庫示例// HttpPostedFile對象,用于讀取圖象文件屬性HttpPostedFile UpFile = UP_FILE.PostedFile;// FileLength 變量存儲圖片的字節(jié)大小int FileLength = UpFile.ContentLength;try{if (FileLength == 0){txtMessage.Text = "<b>您未選擇上傳的文件</b>";}else{// 創(chuàng)建存儲圖片文件的臨時 Byte 數(shù)組Byte[] FileByteArray = new Byte[FileLength];// 建立數(shù)據(jù)流對象Stream StreamObject = UpFile.InputStream; // 讀取圖象文件數(shù)據(jù),FileByteArray為數(shù)據(jù)儲存體,0為數(shù)據(jù)指針位置、FileLnegth為數(shù)據(jù)長度StreamObject.Read(FileByteArray,0,FileLength); // 數(shù)據(jù)庫操作string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];string query = "INSERT INTO ImageTable (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@ImageData, @ImageContentType, @ImageDescription, @ImageSize)";SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));// 添加各項參數(shù)并賦值myCommand.Parameters.Add("@ImageData", SqlDbType.Image);myCommand.Parameters.Add("@ImageContentType", SqlDbType.VarChar, 50);myCommand.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200);myCommand.Parameters.Add("@ImageSize", SqlDbType.BigInt);myCommand.Parameters["@ImageData"].Value = FileByteArray;myCommand.Parameters["@ImageContentType"].Value = UpFile.ContentType;myCommand.Parameters["@ImageDescription"].Value = txtDescription.Text;myCommand.Parameters["@ImageSize"].Value = FileLength;// 執(zhí)行數(shù)據(jù)庫操作myCommand.Connection.Open();myCommand.ExecuteNonQuery();myCommand.Connection.Close();// 提示上傳成功txtMessage.Text = "<b>上傳文件成功</b>";}} catch (Exception ex) {// 使用 Label 標簽顯示異常txtMessage.Text = ex.Message.ToString();}14 獲得插入記錄標識號的示例 // 數(shù)據(jù)庫連接字符串string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];// 創(chuàng)建插入SQL語句及調(diào)用@@identity函數(shù)返回標識值string insert_query = "insert into Categories (CategoryName,Description) values ('IT', 'Internet');"+ "SELECT @@identity AS 'identity';";// 執(zhí)行數(shù)據(jù)庫操作SqlCommand myCommand = new SqlCommand(insert_query, new SqlConnection(ConnStr));myCommand.Connection.Open();myLabel.Text = myCommand.ExecuteScalar().ToString();myCommand.Connection.Close(); 15 如何讀取Excel表格中的數(shù)據(jù)// 獲取Excep文件的完整路徑string source = File1.Value;string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source + ";Extended Properties=Excel 8.0";string query = "SELECT * FROM [Sheet1$]";OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);DataSet myDataSet = new DataSet();// 將 Excel 的[Sheet1]表內(nèi)容填充到 DataSet 對象oleAdapter.Fill(myDataSet, "[Sheet1$]");// 數(shù)據(jù)綁定DataGrid1.DataSource = myDataSet;DataGrid1.DataMember = "[Sheet1$]";DataGrid1.DataBind();

轉(zhuǎn)載于:https://www.cnblogs.com/hulang/archive/2010/12/27/1917825.html

總結(jié)

以上是生活随笔為你收集整理的数据库方面的操作示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。