首页 > 数据库技术 > 详细

ASP.NET 增、删、改、查——传一条SQL语句进行INSERT,UPDATE,DELETE,SELECT

时间:2019-12-19 14:24:05      阅读:133      评论:0      收藏:0      [点我收藏+]

web.config文件设置

  <connectionStrings>
    <add name="ConString" connectionString="Data Source=.;Initial Catalog=qichejieyou;User ID=sa;Password=1108" providerName="System.Data.SqlClient"/>
  </connectionStrings>
 

 

 

public static string connString = ConfigurationManager.ConnectionStrings["conString"].ToString();//数据库连接串

    #region 查询返回一个DataTable
    public static DataTable publicDatatable(string sql)
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataAdapter ad = null;
        DataTable dt = null;
        try
        {
            conn = new SqlConnection(connString);
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            ad = new SqlDataAdapter(cmd);
            dt = new DataTable();
            ad.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            conn.Close();
            ad.Dispose();
            dt.Dispose();
        }
    }
    #endregion

 

    #region //执行SQL语句,如UPDATE,INSERT等
    public static int ExcSQL(string s)
    {
        SqlConnection sqlConnection = new SqlConnection(connString);
        int i = 0;
        try
        {
            SqlCommand sqlCmd = new SqlCommand(s, sqlConnection);
            sqlConnection.Open();
            if (sqlCmd.ExecuteNonQuery() > 0)
            {
                return i = 1;
            }
            return i;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            sqlConnection.Close();
        }
    }
    #endregion


    #region 核对管理员登录
    public static int checksys(string uname, string pwd)
    {
        int i = 0;
        string sql = "SELECT * FROM [vscar] WHERE V_username=‘" + uname + "‘ and V_pwd=‘" + pwd + "‘";
        SqlConnection conn = null;
        SqlDataAdapter ad = null;
        DataSet ds = null;
        try
        {
            conn = new SqlConnection(connString);
            conn.Open();
            ad = new SqlDataAdapter(sql, conn);
            ds = new DataSet();
            ad.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return i = 1;
            }
            return i;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            conn.Close();
            ad.Dispose();
            ds.Dispose();
        }
    }
    #endregion


    #region //核对用户登录
    public static int checkuser(string uname, string pwd)
    {
        int i = 0;
        string sql = "SELECT * FROM [user] WHERE u_name=‘" + uname + "‘ and u_pwd=‘" + pwd + "‘";
        SqlConnection conn = null;
        SqlDataAdapter ad = null;
        DataSet ds = null;
        try
        {
            conn = new SqlConnection(connString);
            conn.Open();
            ad = new SqlDataAdapter(sql, conn);
            ds = new DataSet();
            ad.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return i = 1;
            }
            return i;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            conn.Close();
            ad.Dispose();
            ds.Dispose();
        }
    }
    #endregion


    #region //查看用户名是否已被注册
    public static int checkuser(string uname)
    {
        int i = 0;
        string sql = "select count(*) from vscar where V_username = ‘"+ uname +"‘";
        SqlConnection conn = null;
        SqlDataAdapter ad = null;
        DataSet ds = null;
        try
        {
            conn = new SqlConnection(connString);
            conn.Open();
            ad = new SqlDataAdapter(sql, conn);
            ds = new DataSet();
            ad.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return i = 1;
            }
            return i;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            conn.Close();
            ad.Dispose();
            ds.Dispose();
        }
    }
    #endregion

ASP.NET 增、删、改、查——传一条SQL语句进行INSERT,UPDATE,DELETE,SELECT

原文:https://www.cnblogs.com/li99/p/12067179.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!