C#数据库查询的基本步骤(Sql Server)
来源:互联网 发布:算法导论 数据结构 编辑:程序博客网 时间:2024/06/05 18:15
- 连接字符串。一般定义在项目的web.config文件中。
<configuration>
public string connectionString = ConfigurationManager.ConnectionStrings["OnlineShopConnectionString"].ConnectionString;//通过ConfigurationManager获取连接字符串
<connectionStrings>
<add name="OnlineShopConnectionString"
connectionString="data source=.\SQLEXPRESS; Initial Catalog=OnlineShoppingDatabase; Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
</connectionStrings> - 构建SqlConnection对象。
SqlConnection connection = new SqlConnection(connectionString);
- 打开连接。
connection.Open();
- 构建SqlCommand对象。根据不同的CommandType(Text,StoredProcedure,TableDirect)构建不同的SqlCommand对象。
Text 即为sql语句。
string sql="select * from Student"; SqlCommand command=new SqlCommand(sql,connection);
StoredProcedure为存储过程。
string storedProcdureName="procGetAllStudent";
SqlCommand command=new SqlCommand(storedProcdureName,connection);
command.CommandType=CommandType.StoredProcedure;//同时需指定CommandType
//同时要为存储过程传参。遍历IDataParameter[].foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); }
根据需要的返回值类型,构建返回值。若期望得到SqlDataReader对象。SqlDataReader reader = command.ExecuteReader();若期望得到影响的行数。int row=command.ExecuteNonQuery();若期望得到DataSet/DataTable对象。DataTable dt = new DataTable();SqlDataAdapter adapter = new SqlDataAdapter();adapter.SelectCommand = command;adapter.Fill(dt);
关闭连接。connection.Close();
以下是自己定义的数据库操作的相关类。
public sealed class ConnectionManager { private ConnectionManager() { } [ThreadStatic] public static SqlConnection sqlConnection; const string ConnectionString = "OnlineShopConnectionString"; public static SqlConnection Connection { get { if (sqlConnection == null) { sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString); } return sqlConnection; } } }
public sealed class SqlHelper { private SqlHelper() { //The class cannot be initialized. }
/// <summary> /// Gets a instance of sqlcommand /// </summary> /// <param name="sqlConnection">Represents an open sqlConnection to SQL Server database</param> /// <param name="storedProcName">Stored procedure</param> /// <param name="parameters">Parameter</param> /// <returns>An object of sqlcommand</returns> private static SqlCommand BuildQueryCommand(SqlConnection sqlConnection, CommandType commandType, string commandText, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(commandText, sqlConnection); command.CommandType = commandType; if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } return command; } public static SqlDataReader ExecuteReader(SqlConnection sqlConnection, CommandType commandType, string commandText, SqlParameter[] parameters) { if (null == sqlConnection) { throw new ArgumentNullException(Constant.ConnectionString); } SqlCommand command = BuildQueryCommand(sqlConnection, commandType, commandText, parameters); return command.ExecuteReader(); }
public static int ExecuteNonQuery(SqlConnection sqlConnection, CommandType commandType, string commandText, SqlParameter[] parameters) { if (null == sqlConnection) { throw new ArgumentNullException(Constant.ConnectionString); } SqlCommand command = BuildQueryCommand(sqlConnection, commandType, commandText, parameters);
return command.ExecuteNonQuery(); } }
C# 中数据库操作中的回归操作
private TransactionOptions options = new TransactionOptions(); options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; options.Timeout = new TimeSpan(0, 0, 30);
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) { try {
if (product.RetailPrice > product.DiscountPrice) { ConnectionManager.Connection.Open(); LogHelper.Log(string.Format(CultureInfo.CurrentCulture, Constant.AddProduct + product.ProductId)); result = DalProduct.AddProduct(ConnectionManager.Connection, product); } else { return false; } scope.Complete(); } catch (Exception ex) { LogHelper.Log(ex.Message); throw; } finally { if (ConnectionManager.Connection.State != ConnectionState.Closed) { ConnectionManager.Connection.Close(); } } }
- C#数据库查询的基本步骤(Sql Server)
- C#连接SQL Server并查询信息的一般步骤
- C#连接SQL SERVER数据库的详细步骤!
- C#连接SQL SERVER数据库的详细步骤!
- C#连接SQL SERVER数据库的详细步骤!
- 连接数据库的步骤SQL Server 与mysql 以及一些数据库查询语句
- 数据库的基本SQL查询
- C#:创建 SQL Server 数据库的步骤
- 创建 SQL Server 数据库的步骤
- 保护SQL Server数据库的十个步骤
- C#:创建 SQL Server 数据库的步骤
- JDBC操作SQL Server数据库的步骤
- JDBC连接sql server数据库的步骤
- SQL Server 2005数据库转到SQL Server 2000的步骤
- 4、SQL Server 基本的查询流程
- C#连接SQL数据库的七个步骤
- SQL server 数据库查询
- SQL server 数据库查询
- 在eclipse中查看android源码
- 基于端口映射的KVM虚拟机的创建和使用
- 【wikioi】2822 爱在心中
- c# 基础知识01
- 链接MYSql数据库的学生选课管理系统
- C#数据库查询的基本步骤(Sql Server)
- ORACLE聚簇因子
- 找零钱(sdut 1223
- 黑马程序员.Net学习笔记 02篇.控制流语句
- 友元函数
- 23岁男子因8万彩礼钱酒店掐死未婚妻
- Matlab基本函数-contrast函数
- code
- Mysql 多表删除语法