• asp.net 常用数据库操作


        public SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
        
    public SqlCommand cmd = new SqlCommand(); 
        
    public SqlDataAdapter objadpt = new SqlDataAdapter();
        
    public String sqlStr;
        DataTable dt 
    = new DataTable();   

        
    public basedata()
        {
            
    //
            
    // TODO: 在此处添加构造函数逻辑
            
    //
        }
        
    /// <summary>
        
    /// 根据sql语句返回查询结果
        
    /// </summary>
        
    /// <param name="sqlStr"></param>
        
    /// <returns></returns>
        public DataTable gettable(string sqlStr)
        {     
            
    if(conn .State== ConnectionState.Closed)
                conn.Open();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = sqlStr;
            objadpt.SelectCommand 
    = cmd;
        dt.Clear();
            objadpt.Fill(dt);
            
    if (conn.State == ConnectionState.Open)
               conn.Close();
            
    return dt;
        }

        
    /// <summary>
        
    /// 执行查询语句,返回SqlDataReader
        
    /// </summary>
        
    /// <param name="strSQL">查询语句</param>
        
    /// <returns>SqlDataReader</returns>
        public SqlDataReader GetReader(string sqlStr)
        {
            
    if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = sqlStr;
            SqlDataReader myReader 
    = cmd.ExecuteReader();
            
    return myReader;
            
    if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        
    /// <summary>
        
    /// 执行sql语句,如delete或update
        
    /// </summary>
        
    /// <param name="sqlStr"></param>
        public void executecmd(string sqlStr)
            {
            
    if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = sqlStr;
             cmd.ExecuteNonQuery();
             
    if (conn.State == ConnectionState.Open)
                   conn.Close();
            }

        
    public string executecmdandback(string sqlStr)
        {
            
    string id;
            
    if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = sqlStr + ";SELECT SCOPE_IDENTITY()";
            id 
    = cmd.ExecuteScalar().ToString();//添加配偶并返回刚添加的ID
            if (conn.State == ConnectionState.Open)
                conn.Close();
            
    return id;
        }
        
    /// <summary>
        
    /// 按条件返回查询结果
        
    /// </summary>
        
    /// <param name="sqlStr"></param>
        
    /// <returns></returns>
        public string getresult(string sqlStr)
            {
                
    string id;
            
    if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = sqlStr;
            id 
    = Convert.ToString(cmd.ExecuteScalar());
            
    if (conn.State == ConnectionState.Open)
                conn.Close();
            
    return id;
    }
  • 相关阅读:
    CMake学习笔记
    右键添加"在此处打开命令窗口"菜单
    设置默认python模块源
    添加到附加组
    Unity宏处理
    挂载windows共享文件夹
    MacOS长按无效问题
    中文Locale
    笔记本用作无线路由器
    C# Winfrom iTextSharp 导出pdf 带二维码 表格嵌套 简单Dome
  • 原文地址:https://www.cnblogs.com/ringwang/p/1447041.html
Copyright © 2020-2023  润新知