从服务器检索数据库内容非常慢 [英] Retrieving data base contents is very slow from server

查看:318
本文介绍了从服务器检索数据库内容非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我部署了自己的网站,但是从数据库检索数据非常慢..
这是我的代码示例


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conGet = new SqlConnection(DataBase.ConnectionString);
            try
            {
                SqlCommand cmdGet = new SqlCommand("SELECT LocalPageText FROM LocalPages WHERE (LocalPageName = ''ContactUS'')", conGet);
                SqlDataAdapter daGet = new SqlDataAdapter(cmdGet);
                DataSet dsGet = new DataSet();
                daGet.Fill(dsGet);
                ContactLiteral.Text = dsGet.Tables[0].Rows[0]["LocalPageText"].ToString();
            }
            catch (SqlException)
            {
            }
            finally
            {
                conGet.Close();
            }
        }
    }




但是这段代码运行很快...

try
{
    SqlCommand cmd = new SqlCommand();
    cmd = new SqlCommand("selectCourses", con);
    cmd.CommandType = CommandType.StoredProcedure;
    con.Open();
    SqlDataReader sqlReader = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(sqlReader);

    GridView1.DataBind();
    con.Close();
    cmd.Dispose();

}
catch
{
}
finally
{

}




您的意见是什么?

解决方案

尝试从存储过程中执行所有sql(我发现它更安全)

我还将尝试将SQL调用区域分隔为DAL命名空间和类.并使用BLL来处理数据的返回.然后,您可以将其创建为静态函数,以根据您将其作为参数发送的名称返回页面内容. />
使用第二个选项,因为存储过程是预编译的,因此它的执行时间将很快.
因此,使用存储过程是一个好习惯.....很简单.


使用存储过程,它也非常容易和简单.它也是预编译的.因此,与添加名称空间和类相比,它的执行时间将很快.
因此,使用存储过程是一个好习惯,我以前只使用SP.


i deployed my own web site, but retrieving data from DB is very slow ..
This is sample of my code


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conGet = new SqlConnection(DataBase.ConnectionString);
            try
            {
                SqlCommand cmdGet = new SqlCommand("SELECT LocalPageText FROM LocalPages WHERE (LocalPageName = ''ContactUS'')", conGet);
                SqlDataAdapter daGet = new SqlDataAdapter(cmdGet);
                DataSet dsGet = new DataSet();
                daGet.Fill(dsGet);
                ContactLiteral.Text = dsGet.Tables[0].Rows[0]["LocalPageText"].ToString();
            }
            catch (SqlException)
            {
            }
            finally
            {
                conGet.Close();
            }
        }
    }




But this code run fast ...

try
{
    SqlCommand cmd = new SqlCommand();
    cmd = new SqlCommand("selectCourses", con);
    cmd.CommandType = CommandType.StoredProcedure;
    con.Open();
    SqlDataReader sqlReader = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(sqlReader);

    GridView1.DataBind();
    con.Close();
    cmd.Dispose();

}
catch
{
}
finally
{

}




What''s your Opinion ??

解决方案

Try doing all sql from Stored Procedures (I find it more secure)

I would also try and seperate the SQL calling area to a DAL Namespace and Class. and BLL to handle the returning of the data.You could then create this as a static function to return the page content based on the name you send it as a parameter.


hi,

use second option because Stored procedures are pre-compiled so its execution time will be fast .
so it is a good habit to use stored procedures .. and it is simple...


Use Stored procedures and it is very easy and simple too. It is pre-compiled too. so its execution time will be fast, when compared to the adding a Namespace and class.
so it is a good habit to use stored procedures, i used to do by SP only.


这篇关于从服务器检索数据库内容非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆