Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the ASP.NET database driver class

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "ASP.NET database driver class how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "ASP.NET database driver class how to achieve" it!

The following code implements an ASP.NET database driver class: DBHelper.

Using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; namespace DBUtility {public static class SQLHelper {private static string connectionString = @ "server=.\ SQLEXPRESS;uid=sa;pwd=;database=MyBookShop"; private static SqlConnection sqlConn; / /

< summary>

/

< /summary>

/ / /

< param name="sql">

< /param>

/ / /

< returns>

< /returns>

Public static SqlDataReader GetDataReader (string sql) {try {sqlConn = new SqlConnection (connectionString); sqlConn.Open (); SqlCommand sqlCmd = new SqlCommand (sql,sqlConn); SqlDataReader sqlDr = sqlCmd.ExecuteReader (CommandBehavior.CloseConnection); return sqlDr } catch (SqlException ex) {throw ex;}} public static object ExecScalar (string sql) {try {sqlConn = new SqlConnection (connectionString); sqlConn.Open () SqlCommand sqlCmd = new SqlCommand (sql, sqlConn); object obj = sqlCmd.ExecuteScalar (); return obj;} catch (SqlException ex) {throw ex } finally {sqlConn.Close ();}} / /

< summary>

/ / ASP.NET database driver class: execute imperative stored procedures with parameters /

< /summary>

/ / /

< param name="procName">

Stored procedure name

< /param>

/ / /

< param name="paras">

Resolve the array of SqlParameter objects assigned to the parameters of the stored procedure / (each SqlParameter object solves the parameter assignment for one parameter)

< /param>

/ / /

< returns>

The return value of the stored procedure

< /returns>

Public static int ExecuteProc1 (string procName, SqlParameter [] paras) {try {sqlConn = new SqlConnection (connectionString); sqlConn.Open (); SqlCommand sqlCmd = new SqlCommand (procName, sqlConn); / / execute stored procedure type sqlCmd.CommandType = CommandType.StoredProcedure SqlCmd.Parameters.AddRange (paras); SqlParameter p = new SqlParameter (); / / the return value of the stored procedure p.Direction = ParameterDirection.ReturnValue; p.SqlDbType = SqlDbType.Int; sqlCmd.Parameters.Add (p); sqlCmd.ExecuteNonQuery () Int v = p.Value==null?-1:Convert.ToInt32 (p.Value); return v;} catch (SqlException ex) {throw ex;} finally {sqlConn.Close () }} /

< summary>

/ / ASP.NET database driver class: execute query stored procedures with parameters /

< /summary>

/ / /

< param name="procName">

Stored procedure name

< /param>

/ / /

< param name="paras">

Resolve the array of SqlParameter objects assigned to the parameters of the stored procedure / (each SqlParameter object solves the parameter assignment for one parameter)

< /param>

/ / /

< returns>

If a / query result set is formed on the database server side after the stored procedure is executed, a data reader object pointing to the result set is returned.

< /returns>

Public static SqlDataReader ExecuteProc2 (string procName, SqlParameter [] paras) {try {sqlConn = new SqlConnection (connectionString); sqlConn.Open (); SqlCommand sqlCmd = new SqlCommand (procName, sqlConn); / / execute stored procedure type sqlCmd.CommandType = CommandType.StoredProcedure SqlCmd.Parameters.AddRange (paras); SqlDataReader sqlDr = sqlCmd.ExecuteReader (CommandBehavior.CloseConnection); return sqlDr;} catch (SqlException ex) {throw ex }} Thank you for your reading, the above is the content of "how to realize the ASP.NET database driver class". After the study of this article, I believe you have a deeper understanding of how to realize the ASP.NET database driver class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report