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

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

猜你喜欢

转载自www.cnblogs.com/li99/p/12067179.html