public void Add(Apply_info model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Apply (");
strSql.Append(" Id,Name )");
strSql.Append(" values (");
strSql.Append(" @Id,@Name )");
strSql.Append(" ;select @@IDENTITY");
SqlParameter[] parameters={
new SqlParameter("@Id",SqlDbType.Int,4),
new SqlParameter("@Name",SqlDbType.NVarChar,100)
};
parameters[0].Value=model.Id;
parameters[1].Value=model.Name;
DbHelper.ExecuteNonQuery(CommandType.Text,strSql.ToString(),parameters); //返回受影响行数
}
DbHelper.ExecuteScalar(CommandType.Text,sql,null); //返回第一行第一列的数据
DbHelper.ExecuteNonQuery(CommandType.Text,sql,null); //返回受影响的行数
DbHelper.ExecuteTable(CommandType.StoredProcedure,"GetBestUser",param); //返回一个数据表
DbHelper.ExecuteTables(); //返回一个表集合
DbHelper.ExecutePage(allFields,tablesAndWhere,indexField,orderField,PageIndex,PageSize,out RecordCount,out PageCount,null);//返回一页数据DataTable
DbHelper.ExecuteReader(CommandType.Text,sql,param); //获得一个DataReader数据集
DbHelper.ExecuteReaderPage(); //根据分页条件返回一页数据
DbHelper.ExecuteDataSet(); //返回一个数据表
DbHelper.ExecuteInsert(); //批量插入数据
DbHelper.ExecuteSqlTran(); //批量插入数据
DbHelper.IsWriteConnection() //获得链接字符串
DbHelper.GetTableName(); //获得写语句中的表名
DbHelper.SetTableList(); //记录用户写过的表
DbHelper.SetCache(); //设置服务器缓存
DbHelper.GetCache(); //检查缓存中是否存在此标记
DbHelper.SetCookie(); //将标识添加到浏览器cookie
DbHelper.GetCookie(); //得到浏览器Cookie标识
DbHelper.ReadConStr //读链接
DbHelper.WriteConStr //写链接
增:
insert into Apply (Id,Name) Values (@Id,@Name);select @@IDENTITY
insert into Apply(Id,Name) Values ("+Id+","+Name+")
@@IDENTITY--(@@identity表示的是最近一次向具有identity属性(即自增列)的表插入数据时对应的自增列的值,是系统定义的全局变量。
一般系统定义的全局变量都是以@@开头,用户自定义变量以@开头。)
删:
truncate table Apply //删除表格的数据,下次添加数据ID将从1重新开始!
delete from Apply where Id=@Id and Name=@Name
delete from Apply where Id="+Id+"
改:
update Apply set Name=@Name where Id=@Id
string str=" update Apply set Name="+Name+" where Id="+Id;
DbHelper.ExecuteNonQuery(CommandType.Text,str,null);
查:
select count(*) from Apply where Id=@Id and Name=@Name //查询有多少条数据
select count(1) from Apply where Id=@Id and Name=@Name //查询有多少条数据
select count(ID) from Apply where Id=@Id and Name=@Name //查询有多少条数据
select Name from Apply WITH(NOLOCK) where Id=@Id //WIITH(NOLOCK)可以改善在线大量查询的环境中数据集被LOCK的现象从而改善查询的效能。
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text,str,null));//返回第一行第一列的数据
string str="select * from Apply where Id="+Id+" and Name="+Name;
return DbHelper.ExecuteTable(CommandType.Text,str,null); //返回一个数据表
string str="select top 1 * from Apply where Id="+Id+" and Name="+Name+" order by Id desc "; //查询一条数据
string str="select Id,Type,Name from selected s left join( select count(SelectedId) as num,Selected from voter group by SelectedId) v";
str+=" on v.SelectedId=s.ID where s.Type="+type+" order by v.num desc"; -----按投票数排序查询出相关数据!
注:sql语句中的 聚合函数 有:count()求总和;sum()求和;avg()求平均;max()求最大;min()求最小;
聚合函数如果遇到null值的话,就会跳过去,不计算的!除count外!! 函数名(字段):对这一个字段进行聚合。
----------------------------------------------------------
sql语句中:
//读写分离::为了确保数据库产品的稳定性,很多数据库拥有双机热备功能。
也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器;
第二台数据库服务器,主要进行读的操作。
//启用的是 读写分离
object obj=DbHelper.ExecuteScalar(CommandType.Text,strSql.toString(),parameters);
//加了 false 不采用 读写分离 线上的数据库 就不需要同步
object obj=DbHelper.ExecuteScalar(DBConfig.CnString,CommandType.Text,strSql.ToString(),false,parameters);
//DBConfig.CnString 为数据库连接字符数(string connectionString) 在web.config中配置有 <connectionString></connectionString>
//connectionString=ConfigurationManager.ConnectionStrings["name"].ConnectionString;
public static object ExecuteScaler(string connectionString,CommandType cmdType,string cmdText,bool isSeparate,params DbParameters[] commandParameters)
{
DbConnection connection=Provider.CreateConnection();
try
{
DbCommand cmd=Provider.CreateCommand();
connection.ConnectionString=connectionString;
PrepareCommand(cmd,connection,null,cmdType,cmdText,isSeparate,commandParameters);
object obj2=cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj2;
}
catch
{
throw;
}
finally
{
connection.Close();
}
}
--------------------------------------
.ExecuteNonQuery(); //返回受影响的行数 适用于insert/update/delete
.ExecuteScalar(); //返回第一行第一列 适用于 Count(*)/select name from table
.ExecuteTable(); //返回多行多列 与数据库断裂
.ExecuteDataSet(); //返回表的集合 与数据库断裂
.ExecuteReader(); //效率最高 直接从数据库中读取.